标签:context out 属性 turn one 出现 href info ext
? Context: 环境类
? State: 抽象状态类
? ConcreteState: 具体状态类
1 /** 2 * State 3 * 抽象状态类 4 */ 5 public interface State { 6 public void doAction(Context context); 7 } 8 9 /** 10 * ConcreteState 11 * 具体状态类 12 */ 13 public class StartState implements State { 14 15 @Override 16 public void doAction(Context context) { 17 System.out.println("Player is in start state"); 18 context.setState(this); 19 } 20 21 @Override 22 public String toString(){ 23 return "Start State"; 24 } 25 } 26 27 /** 28 * ConcreteState 29 * 具体状态类 30 */ 31 public class StopState implements State { 32 33 @Override 34 public void doAction(Context context) { 35 System.out.println("Player is in stop state"); 36 context.setState(this); 37 } 38 39 @Override 40 public String toString(){ 41 return "Stop State"; 42 } 43 } 44 45 /** 46 * Context 47 * 环境类 48 */ 49 public class Context { 50 private State state; 51 52 public Context(){ 53 state = null; 54 } 55 56 public void setState(State state){ 57 this.state = state; 58 } 59 60 public State getState(){ 61 return state; 62 } 63 } 64 65 /** 66 * client 67 */ 68 public class Client { 69 70 public static void main(String[] args) { 71 Context context = new Context(); 72 73 StartState startState = new StartState(); 74 startState.doAction(context); 75 76 System.out.println(context.getState().toString()); 77 78 StopState stopState = new StopState(); 79 stopState.doAction(context); 80 81 System.out.println(context.getState().toString()); 82 } 83 }
标签:context out 属性 turn one 出现 href info ext
原文地址:https://www.cnblogs.com/756623607-zhang/p/9251194.html