Last active
October 18, 2017 08:23
-
-
Save hearimm/46de1115bc3ea227468b344fd12bc8fd to your computer and use it in GitHub Desktop.
java state pattern before / after
This file contains 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
/* | |
The CeilingFanPullChain class is now a wrapper that delegates to its m_current_state reference. | |
Each clause from the "before" case statement is now captured in a State derived class. | |
For this simple domain, the State pattern is probably over-kill. | |
*/ | |
interface State { | |
abstract void pull(CeilingFanPullChainAfter wrapper); | |
} | |
class OffState implements State { | |
@Override | |
public void pull(CeilingFanPullChainAfter wrapper) { | |
wrapper.currentState = new LowState(); | |
System.out.println("fan : OFF SPEED"); | |
} | |
} | |
class LowState implements State { | |
@Override | |
public void pull(CeilingFanPullChainAfter wrapper) { | |
wrapper.currentState = new MidState(); | |
System.out.println("fan : LOW SPEED"); | |
} | |
} | |
class MidState implements State { | |
@Override | |
public void pull(CeilingFanPullChainAfter wrapper) { | |
wrapper.currentState = new HighState(); | |
System.out.println("fan : MID SPEED"); | |
} | |
} | |
class HighState implements State { | |
@Override | |
public void pull(CeilingFanPullChainAfter wrapper) { | |
wrapper.currentState = new OffState(); | |
System.out.println("fan : HIGH SPEED"); | |
} | |
} | |
class CeilingFanPullChainAfter { | |
public State currentState; | |
CeilingFanPullChainAfter() { | |
this.currentState = new OffState(); | |
} | |
void pull() { | |
currentState.pull(this); | |
} | |
} | |
public class After { | |
public static void main(String[] args) { | |
CeilingFanPullChainAfter chain = new CeilingFanPullChainAfter(); | |
chain.pull(); | |
chain.pull(); | |
chain.pull(); | |
chain.pull(); | |
} | |
} | |
/* Result | |
* fan : LOW SPEED | |
* fan : MID SPEED | |
* fan : HIGH SPEED | |
* fan : OFF SPEED | |
*/ |
This file contains 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
// Not good: unwieldy "case" statement | |
class CeilingFanPullChain { | |
private int currentState; | |
public CeilingFanPullChain() { | |
currentState = 0; | |
} | |
public void pull() { | |
if (currentState == 0) { | |
currentState = 1; | |
System.out.println("fan : LOW SPEED"); | |
} else if (currentState == 1) { | |
currentState = 2; | |
System.out.println("fan : MID SPEED"); | |
} else if (currentState == 2) { | |
currentState = 3; | |
System.out.println("fan : HIGH SPEED"); | |
} else { | |
currentState = 0; | |
System.out.println("fan : OFF SPEED"); | |
} | |
} | |
} | |
public class Before { | |
public static void main(String[] args) { | |
CeilingFanPullChain chain = new CeilingFanPullChain(); | |
chain.pull(); | |
chain.pull(); | |
chain.pull(); | |
chain.pull(); | |
} | |
} | |
/* Result | |
* fan : LOW SPEED | |
* fan : MID SPEED | |
* fan : HIGH SPEED | |
* fan : OFF SPEED | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment