Skip to content

Commit d6cc250

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

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

FiniteStateMachine/src/main/java/com/iluwatar/Main.java renamed to FiniteStateMachine/src/main/java/com/iluwatar/TrafficLightFsm.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
public class TrafficLightFSM {
2-
// State Interface
1+
package com.iluwatar;
2+
3+
/**
4+
* The TrafficLightFsm class demonstrates a Finite State Machine (FSM)
5+
* implementation using a traffic light system.
6+
*/
7+
public class TrafficLightFsm {
8+
/**
9+
* State interface for traffic light states.
10+
*/
311
interface TrafficLightState {
412
void handleEvent(TrafficLightContext context);
513
}
614

7-
// Concrete States
15+
/**
16+
* Concrete state representing the Red Light.
17+
*/
818
static class RedLightState implements TrafficLightState {
919
@Override
1020
public void handleEvent(TrafficLightContext context) {
@@ -13,6 +23,9 @@ public void handleEvent(TrafficLightContext context) {
1323
}
1424
}
1525

26+
/**
27+
* Concrete state representing the Green Light.
28+
*/
1629
static class GreenLightState implements TrafficLightState {
1730
@Override
1831
public void handleEvent(TrafficLightContext context) {
@@ -21,6 +34,9 @@ public void handleEvent(TrafficLightContext context) {
2134
}
2235
}
2336

37+
/**
38+
* Concrete state representing the Yellow Light.
39+
*/
2440
static class YellowLightState implements TrafficLightState {
2541
@Override
2642
public void handleEvent(TrafficLightContext context) {
@@ -29,24 +45,43 @@ public void handleEvent(TrafficLightContext context) {
2945
}
3046
}
3147

32-
// Context Class
48+
/**
49+
* Context class for managing the current state and transitions.
50+
*/
3351
static class TrafficLightContext {
3452
private TrafficLightState currentState;
3553

54+
/**
55+
* Initializes the context with the given initial state.
56+
*
57+
* @param initialState the initial state of the traffic light
58+
*/
3659
public TrafficLightContext(TrafficLightState initialState) {
3760
this.currentState = initialState;
3861
}
3962

63+
/**
64+
* Updates the current state of the traffic light.
65+
*
66+
* @param newState the new state to transition to
67+
*/
4068
public void setState(TrafficLightState newState) {
4169
this.currentState = newState;
4270
}
4371

72+
/**
73+
* Handles the current state's event and transitions to the next state.
74+
*/
4475
public void handleEvent() {
4576
currentState.handleEvent(this);
4677
}
4778
}
4879

49-
// Main Method
80+
/**
81+
* Main method to simulate the traffic light FSM.
82+
*
83+
* @param args the command-line arguments
84+
*/
5085
public static void main(String[] args) {
5186
// Initialize the traffic light with the Red state
5287
TrafficLightContext trafficLight = new TrafficLightContext(new RedLightState());
@@ -57,3 +92,5 @@ public static void main(String[] args) {
5792
}
5893
}
5994
}
95+
96+

0 commit comments

Comments
 (0)