Use enums instead os switch blocks

Its better to use enums instead of switch
blocks which makes the code longer and difficult
to maintain as and when new state appears.
This commit is contained in:
Rakesh Venkatesh 2020-08-05 15:50:05 +02:00
parent cd20e7a3f4
commit 0c83ccc2fe
12 changed files with 56 additions and 130 deletions

View File

@ -13,18 +13,18 @@ tags:
Dependents, Publish-Subscribe Dependents, Publish-Subscribe
## Intent ## Intent
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified
and updated automatically. and updated automatically.
## Explanation ## Explanation
Real world example Real world example
> In a land far away lives the races of hobbits and orcs. Both of them are mostly outdoors so they closely follow the changes in weather. One could say that they are constantly observing the weather. > In a land far away lives the races of hobbits and orcs. Both of them are mostly outdoors so they closely follow the changes in weather. One could say that they are constantly observing the weather.
In plain words In plain words
> Register as an observer to receive state changes in the object. > Register as an observer to receive state changes in the object.
Wikipedia says Wikipedia says
@ -46,22 +46,7 @@ public class Orcs implements WeatherObserver {
@Override @Override
public void update(WeatherType currentWeather) { public void update(WeatherType currentWeather) {
switch (currentWeather) { LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
case COLD:
LOGGER.info("The orcs are freezing cold.");
break;
case RAINY:
LOGGER.info("The orcs are dripping wet.");
break;
case SUNNY:
LOGGER.info("The sun hurts the orcs' eyes.");
break;
case WINDY:
LOGGER.info("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
} }
} }
@ -72,21 +57,7 @@ public class Hobbits implements WeatherObserver {
@Override @Override
public void update(WeatherType currentWeather) { public void update(WeatherType currentWeather) {
switch (currentWeather) { switch (currentWeather) {
case COLD: LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
LOGGER.info("The hobbits are shivering in the cold weather.");
break;
case RAINY:
LOGGER.info("The hobbits look for cover from the rain.");
break;
case SUNNY:
LOGGER.info("The happy hobbits bade in the warm sun.");
break;
case WINDY:
LOGGER.info("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;
}
} }
} }
``` ```
@ -141,20 +112,20 @@ Here's the full example in action.
weather.timePasses(); weather.timePasses();
// The weather changed to rainy. // The weather changed to rainy.
// The orcs are dripping wet. // The orcs are facing rainy weather now
// The hobbits look for cover from the rain. // The hobbits are facing rainy weather now
weather.timePasses(); weather.timePasses();
// The weather changed to windy. // The weather changed to windy.
// The orc smell almost vanishes in the wind. // The orcs are facing windy weather now
// The hobbits hold their hats tightly in the windy weather. // The hobbits are facing windy weather now
weather.timePasses(); weather.timePasses();
// The weather changed to cold. // The weather changed to cold.
// The orcs are freezing cold. // The orcs are facing cold weather now
// The hobbits are shivering in the cold weather. // The hobbits are facing cold weather now
weather.timePasses(); weather.timePasses();
// The weather changed to sunny. // The weather changed to sunny.
// The sun hurts the orcs' eyes. // The orcs are facing sunny weather now
// The happy hobbits bade in the warm sun. // The hobbits are facing sunny weather now
``` ```
## Class diagram ## Class diagram

View File

@ -33,7 +33,9 @@ package com.iluwatar.observer {
+ RAINY {static} + RAINY {static}
+ SUNNY {static} + SUNNY {static}
+ WINDY {static} + WINDY {static}
+ description String
+ toString() : String + toString() : String
+ getDescription() : String
+ valueOf(name : String) : WeatherType {static} + valueOf(name : String) : WeatherType {static}
+ values() : WeatherType[] {static} + values() : WeatherType[] {static}
} }
@ -71,10 +73,10 @@ package com.iluwatar.observer.generic {
Weather --> "-currentWeather" WeatherType Weather --> "-currentWeather" WeatherType
GWeather --> "-currentWeather" WeatherType GWeather --> "-currentWeather" WeatherType
Weather --> "-observers" WeatherObserver Weather --> "-observers" WeatherObserver
Hobbits ..|> WeatherObserver Hobbits ..|> WeatherObserver
Orcs ..|> WeatherObserver Orcs ..|> WeatherObserver
GHobbits ..|> Race GHobbits ..|> Race
GOrcs ..|> Race GOrcs ..|> Race
GWeather --|> Observable GWeather --|> Observable
Race --|> Observer Race --|> Observer
@enduml @enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

View File

@ -35,21 +35,6 @@ public class Hobbits implements WeatherObserver {
@Override @Override
public void update(WeatherType currentWeather) { public void update(WeatherType currentWeather) {
switch (currentWeather) { LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
case COLD:
LOGGER.info("The hobbits are shivering in the cold weather.");
break;
case RAINY:
LOGGER.info("The hobbits look for cover from the rain.");
break;
case SUNNY:
LOGGER.info("The happy hobbits bade in the warm sun.");
break;
case WINDY:
LOGGER.info("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;
}
} }
} }

View File

@ -35,21 +35,6 @@ public class Orcs implements WeatherObserver {
@Override @Override
public void update(WeatherType currentWeather) { public void update(WeatherType currentWeather) {
switch (currentWeather) { LOGGER.info("The orcs are facing " + currentWeather.getDescription() + " weather now");
case COLD:
LOGGER.info("The orcs are freezing cold.");
break;
case RAINY:
LOGGER.info("The orcs are dripping wet.");
break;
case SUNNY:
LOGGER.info("The sun hurts the orcs' eyes.");
break;
case WINDY:
LOGGER.info("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
} }
} }

View File

@ -28,7 +28,20 @@ package com.iluwatar.observer;
*/ */
public enum WeatherType { public enum WeatherType {
SUNNY, RAINY, WINDY, COLD; SUNNY("Sunny"),
RAINY("Rainy"),
WINDY("Windy"),
COLD("Cold");
private final String description;
WeatherType(String description) {
this.description = description;
}
public String getDescription() {
return this.description;
}
@Override @Override
public String toString() { public String toString() {

View File

@ -36,21 +36,6 @@ public class GHobbits implements Race {
@Override @Override
public void update(GWeather weather, WeatherType weatherType) { public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) { LOGGER.info("The hobbits are facing " + weatherType.getDescription() + " weather now");
case COLD:
LOGGER.info("The hobbits are shivering in the cold weather.");
break;
case RAINY:
LOGGER.info("The hobbits look for cover from the rain.");
break;
case SUNNY:
LOGGER.info("The happy hobbits bade in the warm sun.");
break;
case WINDY:
LOGGER.info("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;
}
} }
} }

View File

@ -36,21 +36,6 @@ public class GOrcs implements Race {
@Override @Override
public void update(GWeather weather, WeatherType weatherType) { public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) { LOGGER.info("The orcs are facing " + weatherType.getDescription() + " weather now");
case COLD:
LOGGER.info("The orcs are freezing cold.");
break;
case RAINY:
LOGGER.info("The orcs are dripping wet.");
break;
case SUNNY:
LOGGER.info("The sun hurts the orcs' eyes.");
break;
case WINDY:
LOGGER.info("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
} }
} }

View File

@ -36,10 +36,10 @@ public class HobbitsTest extends WeatherObserverTest<Hobbits> {
@Override @Override
public Collection<Object[]> dataProvider() { public Collection<Object[]> dataProvider() {
return List.of( return List.of(
new Object[]{WeatherType.SUNNY, "The happy hobbits bade in the warm sun."}, new Object[]{WeatherType.SUNNY, "The hobbits are facing Sunny weather now"},
new Object[]{WeatherType.RAINY, "The hobbits look for cover from the rain."}, new Object[]{WeatherType.RAINY, "The hobbits are facing Rainy weather now"},
new Object[]{WeatherType.WINDY, "The hobbits hold their hats tightly in the windy weather."}, new Object[]{WeatherType.WINDY, "The hobbits are facing Windy weather now"},
new Object[]{WeatherType.COLD, "The hobbits are shivering in the cold weather."}); new Object[]{WeatherType.COLD, "The hobbits are facing Cold weather now"});
} }
/** /**

View File

@ -36,10 +36,10 @@ public class OrcsTest extends WeatherObserverTest<Orcs> {
@Override @Override
public Collection<Object[]> dataProvider() { public Collection<Object[]> dataProvider() {
return List.of( return List.of(
new Object[]{WeatherType.SUNNY, "The sun hurts the orcs' eyes."}, new Object[]{WeatherType.SUNNY, "The orcs are facing Sunny weather now"},
new Object[]{WeatherType.RAINY, "The orcs are dripping wet."}, new Object[]{WeatherType.RAINY, "The orcs are facing Rainy weather now"},
new Object[]{WeatherType.WINDY, "The orc smell almost vanishes in the wind."}, new Object[]{WeatherType.WINDY, "The orcs are facing Windy weather now"},
new Object[]{WeatherType.COLD, "The orcs are freezing cold."}); new Object[]{WeatherType.COLD, "The orcs are facing Cold weather now"});
} }
/** /**

View File

@ -38,10 +38,10 @@ public class GHobbitsTest extends ObserverTest<GHobbits> {
@Override @Override
public Collection<Object[]> dataProvider() { public Collection<Object[]> dataProvider() {
return List.of( return List.of(
new Object[]{WeatherType.SUNNY, "The happy hobbits bade in the warm sun."}, new Object[]{WeatherType.SUNNY, "The hobbits are facing Sunny weather now"},
new Object[]{WeatherType.RAINY, "The hobbits look for cover from the rain."}, new Object[]{WeatherType.RAINY, "The hobbits are facing Rainy weather now"},
new Object[]{WeatherType.WINDY, "The hobbits hold their hats tightly in the windy weather."}, new Object[]{WeatherType.WINDY, "The hobbits are facing Windy weather now"},
new Object[]{WeatherType.COLD, "The hobbits are shivering in the cold weather."} new Object[]{WeatherType.COLD, "The hobbits are facing Cold weather now"}
); );
} }

View File

@ -38,10 +38,10 @@ public class OrcsTest extends ObserverTest<GOrcs> {
@Override @Override
public Collection<Object[]> dataProvider() { public Collection<Object[]> dataProvider() {
return List.of( return List.of(
new Object[]{WeatherType.SUNNY, "The sun hurts the orcs' eyes."}, new Object[]{WeatherType.SUNNY, "The orcs are facing Sunny weather now"},
new Object[]{WeatherType.RAINY, "The orcs are dripping wet."}, new Object[]{WeatherType.RAINY, "The orcs are facing Rainy weather now"},
new Object[]{WeatherType.WINDY, "The orc smell almost vanishes in the wind."}, new Object[]{WeatherType.WINDY, "The orcs are facing Windy weather now"},
new Object[]{WeatherType.COLD, "The orcs are freezing cold."} new Object[]{WeatherType.COLD, "The orcs are facing Cold weather now"}
); );
} }