Add all 23 GoF design pattern implementations
This commit is contained in:
11
03-behavioral/state/GreenState.java
Normal file
11
03-behavioral/state/GreenState.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package state;
|
||||
|
||||
public class GreenState implements TrafficLightState {
|
||||
@Override public void onEnter(TrafficLight light) {
|
||||
System.out.println(" [GREEN] Go. Traffic flows.");
|
||||
}
|
||||
@Override public void next(TrafficLight light) {
|
||||
light.setState(new YellowState());
|
||||
}
|
||||
@Override public String getColor() { return "GREEN"; }
|
||||
}
|
||||
26
03-behavioral/state/Main.java
Normal file
26
03-behavioral/state/Main.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package state;
|
||||
|
||||
/**
|
||||
* State Design Pattern — Runnable Demo
|
||||
* Run: javac state/*.java -d out/state && java -cp out/state state.Main
|
||||
* Article: https://ankurm.com/state-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== State Design Pattern Demo ===\n");
|
||||
|
||||
TrafficLight light = new TrafficLight();
|
||||
System.out.println("Starting state: " + light.getColor());
|
||||
|
||||
System.out.println("\n-- Cycling through states --");
|
||||
for (int i = 0; i < 6; i++) {
|
||||
light.next();
|
||||
}
|
||||
|
||||
System.out.println("\n-- Without State pattern: a single class with if/else --");
|
||||
System.out.println(" Every new state adds to every method's if-else chain.");
|
||||
System.out.println(" With State: add one new class, touch nothing else.");
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
33
03-behavioral/state/README.md
Normal file
33
03-behavioral/state/README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# State Design Pattern — Java Example
|
||||
|
||||
**Pattern:** Behavioral → State
|
||||
**Article:** https://ankurm.com/state-design-pattern-java/
|
||||
|
||||
## What this example shows
|
||||
|
||||
A traffic light with zero if-else state machines. `TrafficLightState` declares what every state must do: handle entry (`onEnter`), handle the transition trigger (`next`), and report its own color. `TrafficLight` is the context — it holds a reference to the current state and delegates every call to it. `RedState`, `GreenState`, and `YellowState` each know only their own entry message and their own next state (Red→Green→Yellow→Red); none of them knows about the full cycle. `Main` creates the light, cycles through six transitions, and prints why this beats a single class full of if-else branches.
|
||||
|
||||
## How to run
|
||||
|
||||
```bash
|
||||
javac state/*.java -d out/state
|
||||
java -cp out/state state.Main
|
||||
```
|
||||
|
||||
Requires Java 25.
|
||||
|
||||
## Post Section ↔ File Mapping
|
||||
|
||||
| Post Section | File(s) |
|
||||
|---|---|
|
||||
| With State: Traffic Light — the State interface | `TrafficLightState.java` |
|
||||
| With State: Traffic Light — the Context | `TrafficLight.java` |
|
||||
| With State: Traffic Light — RedState | `RedState.java` |
|
||||
| With State: Traffic Light — GreenState | `GreenState.java` |
|
||||
| With State: Traffic Light — YellowState | `YellowState.java` |
|
||||
| With State: Traffic Light — wiring it together | `Main.java` |
|
||||
|
||||
Note: the "Without State: The if-else Machine" snippet (the fragile `TrafficLight` with a `String state` field) is illustrative only — it is not part of this repository's runnable example.
|
||||
|
||||
Article: https://ankurm.com/state-design-pattern-java/
|
||||
All patterns: https://ankurm.com/design-patterns-java/
|
||||
11
03-behavioral/state/RedState.java
Normal file
11
03-behavioral/state/RedState.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package state;
|
||||
|
||||
public class RedState implements TrafficLightState {
|
||||
@Override public void onEnter(TrafficLight light) {
|
||||
System.out.println(" [RED] Stop. Pedestrians cross.");
|
||||
}
|
||||
@Override public void next(TrafficLight light) {
|
||||
light.setState(new GreenState());
|
||||
}
|
||||
@Override public String getColor() { return "RED"; }
|
||||
}
|
||||
21
03-behavioral/state/TrafficLight.java
Normal file
21
03-behavioral/state/TrafficLight.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package state;
|
||||
|
||||
/** Context — holds the current state and delegates behaviour to it */
|
||||
public class TrafficLight {
|
||||
private TrafficLightState state;
|
||||
|
||||
public TrafficLight() {
|
||||
setState(new RedState());
|
||||
}
|
||||
|
||||
public void setState(TrafficLightState newState) {
|
||||
this.state = newState;
|
||||
state.onEnter(this);
|
||||
}
|
||||
|
||||
public void next() {
|
||||
state.next(this);
|
||||
}
|
||||
|
||||
public String getColor() { return state.getColor(); }
|
||||
}
|
||||
8
03-behavioral/state/TrafficLightState.java
Normal file
8
03-behavioral/state/TrafficLightState.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package state;
|
||||
|
||||
/** State interface — each concrete state handles events differently */
|
||||
public interface TrafficLightState {
|
||||
void onEnter(TrafficLight light);
|
||||
void next(TrafficLight light);
|
||||
String getColor();
|
||||
}
|
||||
11
03-behavioral/state/YellowState.java
Normal file
11
03-behavioral/state/YellowState.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package state;
|
||||
|
||||
public class YellowState implements TrafficLightState {
|
||||
@Override public void onEnter(TrafficLight light) {
|
||||
System.out.println(" [YELLOW] Slow down. Prepare to stop.");
|
||||
}
|
||||
@Override public void next(TrafficLight light) {
|
||||
light.setState(new RedState());
|
||||
}
|
||||
@Override public String getColor() { return "YELLOW"; }
|
||||
}
|
||||
Reference in New Issue
Block a user