|
| 1 | +class Context { |
| 2 | + private final AirConditioningState open = |
| 3 | + new OpenState(); |
| 4 | + private final AirConditioningState close = |
| 5 | + new CloseState(); |
| 6 | + private final AirConditioningState cool = |
| 7 | + new CoolState(); |
| 8 | + private final AirConditioningState heat = |
| 9 | + new HeatState(); |
| 10 | + private AirConditioningState currentState = close; |
| 11 | + |
| 12 | + public void toOpen() { |
| 13 | + if (currentState.open()) |
| 14 | + currentState = open; |
| 15 | + } |
| 16 | + |
| 17 | + public void toClose() { |
| 18 | + if (currentState.close()) |
| 19 | + currentState = close; |
| 20 | + } |
| 21 | + |
| 22 | + public void toCool() { |
| 23 | + if (currentState.cool()) |
| 24 | + currentState = cool; |
| 25 | + } |
| 26 | + |
| 27 | + public void toHeat() { |
| 28 | + if (currentState.heat()) |
| 29 | + currentState = heat; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +abstract class AirConditioningState { |
| 34 | + public boolean open() { |
| 35 | + System.out.println("illegal state, unable to open"); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + public boolean close() { |
| 40 | + System.out.println("illegal state, unable to close"); |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + public boolean cool() { |
| 45 | + System.out.println("illegal state, unable to cool"); |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + public boolean heat() { |
| 50 | + System.out.println("illegal state, unable to heat"); |
| 51 | + return false; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class OpenState extends AirConditioningState { |
| 56 | + public boolean close() { |
| 57 | + System.out.println("change to state: `close`"); |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + public boolean cool() { |
| 62 | + System.out.println("change to state: `cool`"); |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + public boolean heat() { |
| 67 | + System.out.println("change to state: `heat`"); |
| 68 | + return true; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class CloseState extends AirConditioningState { |
| 73 | + public boolean open() { |
| 74 | + System.out.println("change to state: `open`"); |
| 75 | + return true; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class CoolState extends AirConditioningState { |
| 80 | + public boolean close() { |
| 81 | + System.out.println("change to state: `close`"); |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + public boolean heat() { |
| 86 | + System.out.println("change to state: `heat`"); |
| 87 | + return true; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +class HeatState extends AirConditioningState { |
| 92 | + public boolean close() { |
| 93 | + System.out.println("change to state: `close`"); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + public boolean cool() { |
| 98 | + System.out.println("change to state: `cool`"); |
| 99 | + return true; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +public class StatePattern { |
| 105 | + public static void main(String[] args) { |
| 106 | + Context context = new Context(); |
| 107 | + context.toHeat(); |
| 108 | + context.toOpen(); |
| 109 | + context.toHeat(); |
| 110 | + context.toCool(); |
| 111 | + context.toClose(); |
| 112 | + context.toClose(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments