Last active
December 5, 2023 21:59
-
-
Save kishankg/cdda76871e6a80313de0477976b9e041 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// State interface | |
interface TrafficLightState { | |
void display(); | |
} | |
// Concrete State: RedLightState | |
class RedLightState implements TrafficLightState { | |
@Override | |
public void display() { | |
System.out.println("Red Light - Stop"); | |
} | |
} | |
// Concrete State: YellowLightState | |
class YellowLightState implements TrafficLightState { | |
@Override | |
public void display() { | |
System.out.println("Yellow Light - Prepare to Stop or Go"); | |
} | |
} | |
// Concrete State: GreenLightState | |
class GreenLightState implements TrafficLightState { | |
@Override | |
public void display() { | |
System.out.println("Green Light - Go"); | |
} | |
} | |
// Context: TrafficLight | |
class TrafficLight { | |
private TrafficLightState currentState; | |
public TrafficLight() { | |
// Initial state: RedLightState | |
currentState = new RedLightState(); | |
} | |
public void setState(TrafficLightState state) { | |
this.currentState = state; | |
} | |
public void changeState() { | |
if (currentState instanceof RedLightState) { | |
setState(new GreenLightState()); | |
} else if (currentState instanceof GreenLightState) { | |
setState(new YellowLightState()); | |
} else if (currentState instanceof YellowLightState) { | |
setState(new RedLightState()); | |
} | |
currentState.display(); | |
} | |
} | |
// Client Code | |
public class TrafficLightExample { | |
public static void main(String[] args) { | |
TrafficLight trafficLight = new TrafficLight(); | |
// Initial state: Red | |
trafficLight.changeState(); | |
// Transition to next state: Green | |
trafficLight.changeState(); | |
// Transition to next state: Yellow | |
trafficLight.changeState(); | |
// Transition to next state: Red | |
trafficLight.changeState(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// State interface | |
interface VendingMachineState { | |
void insertCoin(); | |
void ejectCoin(); | |
void dispense(); | |
} | |
// Concrete State: NoCoinState | |
class NoCoinState implements VendingMachineState { | |
@Override | |
public void insertCoin() { | |
System.out.println("Coin inserted"); | |
} | |
@Override | |
public void ejectCoin() { | |
System.out.println("No coin to eject"); | |
} | |
@Override | |
public void dispense() { | |
System.out.println("Insert a coin first"); | |
} | |
} | |
// Concrete State: HasCoinState | |
class HasCoinState implements VendingMachineState { | |
@Override | |
public void insertCoin() { | |
System.out.println("Coin already inserted"); | |
} | |
@Override | |
public void ejectCoin() { | |
System.out.println("Coin ejected"); | |
} | |
@Override | |
public void dispense() { | |
System.out.println("Product dispensed"); | |
} | |
} | |
// Concrete State: SoldState | |
class SoldState implements VendingMachineState { | |
@Override | |
public void insertCoin() { | |
System.out.println("Please wait, product is being dispensed"); | |
} | |
@Override | |
public void ejectCoin() { | |
System.out.println("Sorry, product is already dispensed"); | |
} | |
@Override | |
public void dispense() { | |
System.out.println("Product dispensed. Enjoy!"); | |
} | |
} | |
// Context: VendingMachine | |
class VendingMachine { | |
private VendingMachineState currentState; | |
public VendingMachine() { | |
// Initial state: NoCoinState | |
currentState = new NoCoinState(); | |
} | |
public void setState(VendingMachineState state) { | |
this.currentState = state; | |
} | |
public void insertCoin() { | |
currentState.insertCoin(); | |
if (currentState instanceof NoCoinState) { | |
setState(new HasCoinState()); | |
} else if (currentState instanceof HasCoinState) { | |
setState(new SoldState()); | |
} | |
} | |
public void ejectCoin() { | |
currentState.ejectCoin(); | |
if (currentState instanceof HasCoinState) { | |
setState(new NoCoinState()); | |
} else if (currentState instanceof SoldState) { | |
setState(new NoCoinState()); | |
} | |
} | |
public void dispense() { | |
currentState.dispense(); | |
if (currentState instanceof SoldState) { | |
setState(new NoCoinState()); | |
} | |
} | |
} | |
// Client Code | |
public class StatePatternExample { | |
public static void main(String[] args) { | |
VendingMachine vendingMachine = new VendingMachine(); | |
vendingMachine.insertCoin(); | |
vendingMachine.ejectCoin(); | |
vendingMachine.insertCoin(); | |
vendingMachine.insertCoin(); | |
vendingMachine.dispense(); | |
vendingMachine.insertCoin(); | |
vendingMachine.ejectCoin(); | |
vendingMachine.dispense(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment