mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 18:59:15 +00:00
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:
@@ -35,21 +35,6 @@ public class Hobbits implements WeatherObserver {
|
||||
|
||||
@Override
|
||||
public void update(WeatherType currentWeather) {
|
||||
switch (currentWeather) {
|
||||
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;
|
||||
}
|
||||
LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,21 +35,6 @@ public class Orcs implements WeatherObserver {
|
||||
|
||||
@Override
|
||||
public void update(WeatherType currentWeather) {
|
||||
switch (currentWeather) {
|
||||
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;
|
||||
}
|
||||
LOGGER.info("The orcs are facing " + currentWeather.getDescription() + " weather now");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,20 @@ package com.iluwatar.observer;
|
||||
*/
|
||||
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
|
||||
public String toString() {
|
||||
|
||||
@@ -36,21 +36,6 @@ public class GHobbits implements Race {
|
||||
|
||||
@Override
|
||||
public void update(GWeather weather, WeatherType weatherType) {
|
||||
switch (weatherType) {
|
||||
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;
|
||||
}
|
||||
LOGGER.info("The hobbits are facing " + weatherType.getDescription() + " weather now");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,21 +36,6 @@ public class GOrcs implements Race {
|
||||
|
||||
@Override
|
||||
public void update(GWeather weather, WeatherType weatherType) {
|
||||
switch (weatherType) {
|
||||
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;
|
||||
}
|
||||
LOGGER.info("The orcs are facing " + weatherType.getDescription() + " weather now");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user