では、交通信号(TrafficLight)とポイント計算(ShopPoints) も、同じ形式で ステップ別教材+UML図 にまとめます。
交通信号(TrafficLight)戦略パターン教材
1. 目的の理解
元の switch 文:
switch (light) {
case "RED":
System.out.println("止まれ");
break;
case "YELLOW":
System.out.println("注意");
break;
case "GREEN":
System.out.println("進め");
break;
}
Java問題点:
- 条件が増えると可読性が低下
- 処理の流れが main コードに依存
戦略パターンを使うと:
- 条件ごとの処理をクラス化
- mainコードがシンプルに
- 拡張性と保守性が向上
2. ステップ1:インターフェース作成
interface TrafficLightStrategy {
void execute();
}
Java3. ステップ2:条件ごとのクラス作成
class RedLight implements TrafficLightStrategy {
public void execute() { System.out.println("止まれ"); }
}
class YellowLight implements TrafficLightStrategy {
public void execute() { System.out.println("注意"); }
}
class GreenLight implements TrafficLightStrategy {
public void execute() { System.out.println("進め"); }
}
Java4. ステップ3:ファクトリでオブジェクト生成
class TrafficLightFactory {
static TrafficLightStrategy getStrategy(String light) {
switch (light) {
case "RED": return new RedLight();
case "YELLOW": return new YellowLight();
case "GREEN": return new GreenLight();
default: throw new IllegalArgumentException("不明な信号");
}
}
}
Java5. ステップ4:mainコード
public class MainTraffic {
public static void main(String[] args) {
String light = "YELLOW";
TrafficLightStrategy strategy = TrafficLightFactory.getStrategy(light);
strategy.execute();
}
}
Java6. UML図(ASCII版)
+----------------------+
| TrafficLightStrategy | <<interface>>
+----------------------+
| + execute() |
+----------------------+
^
|
---------------------------------
| | |
+---------+ +-----------+ +----------+
| RedLight| | YellowLight| | GreenLight|
+---------+ +-----------+ +----------+
| +execute()| | +execute()| | +execute()|
+---------+ +-----------+ +----------+
^
|
+-----------------------+
| TrafficLightFactory |
+-----------------------+
| +getStrategy(light) |
+-----------------------+
ポイント計算(ShopPoints)戦略パターン教材
1. 目的の理解
元の switch 文:
switch (rank) {
case "GOLD":
System.out.println(purchase * 0.05);
break;
case "SILVER":
System.out.println(purchase * 0.03);
break;
case "BRONZE":
System.out.println(purchase * 0.01);
break;
}
Java問題点:
- ランクが増えると switch が複雑
- mainコードに計算ロジックが混ざる
戦略パターンを使うと:
- 条件ごとの計算ロジックをクラスに分離
- mainコードが簡潔
- 新しいランク追加も簡単
2. ステップ1:インターフェース作成
interface PointStrategy {
double calculate(double purchase);
}
Java3. ステップ2:条件ごとのクラス作成
class GoldPoint implements PointStrategy {
public double calculate(double purchase) { return purchase * 0.05; }
}
class SilverPoint implements PointStrategy {
public double calculate(double purchase) { return purchase * 0.03; }
}
class BronzePoint implements PointStrategy {
public double calculate(double purchase) { return purchase * 0.01; }
}
Java4. ステップ3:ファクトリでオブジェクト生成
class PointFactory {
static PointStrategy getStrategy(String rank) {
switch (rank) {
case "GOLD": return new GoldPoint();
case "SILVER": return new SilverPoint();
case "BRONZE": return new BronzePoint();
default: throw new IllegalArgumentException("不明なランク");
}
}
}
Java5. ステップ4:mainコード
public class MainPoint {
public static void main(String[] args) {
String rank = "SILVER";
double purchase = 1000;
PointStrategy strategy = PointFactory.getStrategy(rank);
double point = strategy.calculate(purchase);
System.out.println("加算ポイント:" + point);
}
}
Java6. UML図(ASCII版)
+----------------+
| PointStrategy | <<interface>>
+----------------+
| +calculate() |
+----------------+
^
|
---------------------------------
| | |
+----------+ +-----------+ +-----------+
| GoldPoint| | SilverPoint| | BronzePoint|
+----------+ +-----------+ +-----------+
| +calculate()| | +calculate()| | +calculate()|
+----------+ +-----------+ +-----------+
^
|
+----------------+
| PointFactory |
+----------------+
| +getStrategy() |
+----------------+
ポイント(共通)
- インターフェース中心:共通メソッドを統一
- 条件ごとのクラス化:処理を分離して可読性向上
- ファクトリ使用:mainから switch を消す
- mainコード簡潔:戦略オブジェクトの呼び出しだけで完結
- 拡張性:新しい条件や処理を追加しやすい
💡 この教材セットで、switch → 戦略パターンの理解から実装、UML図での整理まで一気通貫で学習できます。
