Skip to content

Commit a53c561

Browse files
committed
Implemented FSM design pattern issue #203
1 parent b375919 commit a53c561

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

FiniteStateMachine/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.iluwatar</groupId>
8+
<artifactId>java-design-patterns</artifactId>
9+
<version>1.26.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>FiniteStateMachine</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
public class TrafficLightFSM {
2+
// State Interface
3+
interface TrafficLightState {
4+
void handleEvent(TrafficLightContext context);
5+
}
6+
7+
// Concrete States
8+
static class RedLightState implements TrafficLightState {
9+
@Override
10+
public void handleEvent(TrafficLightContext context) {
11+
System.out.println("Red Light: Stop!");
12+
context.setState(new GreenLightState());
13+
}
14+
}
15+
16+
static class GreenLightState implements TrafficLightState {
17+
@Override
18+
public void handleEvent(TrafficLightContext context) {
19+
System.out.println("Green Light: Go!");
20+
context.setState(new YellowLightState());
21+
}
22+
}
23+
24+
static class YellowLightState implements TrafficLightState {
25+
@Override
26+
public void handleEvent(TrafficLightContext context) {
27+
System.out.println("Yellow Light: Caution!");
28+
context.setState(new RedLightState());
29+
}
30+
}
31+
32+
// Context Class
33+
static class TrafficLightContext {
34+
private TrafficLightState currentState;
35+
36+
public TrafficLightContext(TrafficLightState initialState) {
37+
this.currentState = initialState;
38+
}
39+
40+
public void setState(TrafficLightState newState) {
41+
this.currentState = newState;
42+
}
43+
44+
public void handleEvent() {
45+
currentState.handleEvent(this);
46+
}
47+
}
48+
49+
// Main Method
50+
public static void main(String[] args) {
51+
// Initialize the traffic light with the Red state
52+
TrafficLightContext trafficLight = new TrafficLightContext(new RedLightState());
53+
54+
// Simulate events
55+
for (int i = 0; i < 6; i++) {
56+
trafficLight.handleEvent();
57+
}
58+
}
59+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
<module>function-composition</module>
219219
<module>microservices-distributed-tracing</module>
220220
<module>microservices-idempotent-consumer</module>
221+
<module>FiniteStateMachine</module>
221222
</modules>
222223
<repositories>
223224
<repository>

0 commit comments

Comments
 (0)