标签:== 状态改变 重复 ace 简单 流程 步骤 条件 实例化
使用场景
1 public class CandyMachine{ 2 private final int SOLD_OUT = 0;//售罄 3 private final int NO_QUARTZ = 1;//没有25分钱 4 private final int HAS_QUARTZ = 2;//有25分钱 5 private final int SOLD = 3;//售出 6 7 private int state = SOLD_OUT;//初始化状态值state为售罄状态 8 private int count = 0;//糖果数目 9 10 public CandyMachine(int count){ 11 this.count = count; 12 if(count > 0){ 13 this.state = NO_QUARTZ; 14 } 15 } 16 17 //投入25分钱 18 public void insertQuartz(){ 19 if(state == HAS_QUARTZ){ 20 System.out.println("已经投了钱,不能重复投"); 21 }else if(state == NO_QUARTZ){ 22 state = HAS_QUARTZ; 23 System.out.println("投币成功"); 24 }else if(state == SOLD_OUT){ 25 System.out.println("不能投币,糖果已售罄"); 26 }else if(state == SOLD) { 27 System.out.println("请等待,正在发放糖果"); 28 } 29 } 30 31 //退回25分钱 32 public void ejectQuartz(){ 33 if(state == HAS_QUARTZ){ 34 System.out.println("正在退钱"); 35 }else if(state == NO_QUARTZ){ 36 System.out.println("未投钱,不能退钱"); 37 }else if(state == SOLD_OUT){ 38 System.out.println("不能退币,糖果已售罄"); 39 }else if(state == SOLD) { 40 System.out.println("已经转动曲柄,不能退钱"); 41 } 42 } 43 44 //转动曲柄 45 public void turnCrank(){ 46 if(state == SOLD){ 47 System.out.println("重复转动不能得到两次糖果"); 48 }else if(state == NO_QUARTZ){ 49 System.out.println("未投币,没有糖果发放"); 50 }else if(state == SOLD_OUT){ 51 System.out.println("糖果售罄,没有糖果发放"); 52 }else if(state == HAS_QUARTZ){ 53 System.out.println("已转动曲柄"); 54 state = SOLD; 55 dispense(); 56 } 57 } 58 59 //发放糖果 60 public void dispense(){ 61 if(state == SOLD){ 62 System.out.println("糖果即将发放"); 63 count--; 64 if(count == 0){ 65 System.out.println("这是最后一颗糖果,已售罄"); 66 state == SOLD_OUT; 67 }else{ 68 state = NO_QUARTZ; 69 } 70 }else if(state == NO_QUARTZ){ 71 System.out.println("需要先付钱"); 72 }else if(state == SOLD_OUT){ 73 System.out.println("没有糖果发放"); 74 }else if(state == HAS_QUARTZ){ 75 System.out.println("没有糖果发放"); 76 } 77 } 78 }
1 public interface State { 2 //投入25分钱 3 void insertQuartz(); 4 //退回25分钱 5 void ejectQuartz(); 6 //转动曲柄 7 void turnCrank(); 8 //发放糖果 9 void dispense(); 10 }
1 /** 2 * 出售状态 3 */ 4 public class SoldState implements State{ 5 private CandyMachine candyMachine; 6 public SoldState(CandyMachine candyMachine) { 7 this.candyMachine = candyMachine; 8 } 9 @Override 10 public void insertQuartz() { 11 System.out.println("请等待,正在发放糖果"); 12 } 13 @Override 14 public void ejectQuartz() { 15 System.out.println("已经转动曲柄,不能退钱"); 16 } 17 @Override 18 public void turnCrank() { 19 System.out.println("重复转动不能得到两次糖果"); 20 } 21 @Override 22 public void dispense() { 23 candyMachine.releaseBall(); 24 if(candyMachine.getCount() > 0){ 25 candyMachine.setState(candyMachine.getNoQuartzState()); 26 }else{ 27 candyMachine.setState(candyMachine.getSoldOutState()); 28 } 29 } 30 }
1 public class CandyMachine { 2 private int count = 0; 3 private State soldState; 4 private State noQuartzState; 5 private State hasQuartzState; 6 private State soldOutState; 7 private State state = soldOutState; 8 public CandyMachine(int count) { 9 this.count = count; 10 this.soldState = new SoldState(this); 11 this.noQuartzState = new SoldOutState(this); 12 this.hasQuartzState = new HasQuartzState(this); 13 this.soldOutState = new NoQuartzState(this); 14 if(count > 0){ 15 state = noQuartzState; 16 } 17 } 18 public void insertQuartz() { 19 state.insertQuartz(); 20 } 21 public void ejectQuartz() { 22 state.ejectQuartz(); 23 } 24 public void turnCrank() { 25 state.turnCrank(); 26 state.dispense(); 27 } 28 //发放糖果 29 public void releaseBall(){ 30 System.out.println("糖果即将发放"); 31 if(count != 0){ 32 count--; 33 } 34 } 35 }
1 public class WinnerState implements State { 2 private CandyMachine candyMachine; 3 public WinnerState(CandyMachine candyMachine) { 4 this.candyMachine = candyMachine; 5 } 6 @Override 7 public void insertQuartz() {} 8 @Override 9 public void ejectQuartz() {} 10 @Override 11 public void turnCrank() {} 12 @Override 13 public void dispense() { 14 candyMachine.releaseBall(); 15 //这里处理逻辑。。。 16 } 17 }
标签:== 状态改变 重复 ace 简单 流程 步骤 条件 实例化
原文地址:http://www.cnblogs.com/uodut/p/6953765.html