设计模式之状态模式(一)
我原本以为在对乡村的一切事物都很容易,但是每次我一回头就有更多变更的请求纷至沓来。我快崩溃了!----当小编读到这段话的时候,甚是感慨,设计模式的世界真的不是那么容易弄懂的,好在我还在学习。
基本常识:策略模式和状态模式是双胞胎,在出生时才分开。策略模式是围绕可以互换的算法来创建成功业务的。然而,状态走的是更崇高的路,它通过改变对象内部的状态来帮助对象控制自己的行为。
我们先来看下对象村的大家,这次又想出了什么。他们发现只要在糖果机里装上cpu,就可以增加销量、通过网络监测库存,并且能准确地得知客户的满意度。所有,就有了下面的这个设想:
状态机101
我们如何从状态图得到真正的代码呢?下面是一个实现状态机的简单介绍
- 首先,找出所有的状态:
我们有:没有25分钱、有25分钱、糖果售罄、售出糖果四个状态
- 接下来,创建一个实例变量来持有目前的状态,然后定义每个状态的值:
final static int sold_out = 0; final static int no_quarter = 1; final static int has_quarter = 2; final static int sold = 3; int state = sold_out;
- 现在,我们将所有系统中可以发生的动作整合起来:
投入25分钱、退回25分钱、转动曲柄、发放糖果
- 现在,我们创建一个类,它的作用就像是一个状态机。对每一个动作,我们都创建了一个对应的方法,这些方法利用条件语句来决定在每个状态内什么行为是恰当的。比如对“投入25分钱”这个动作来说,我们可以把对应方法写成下面的样子:
public void insertquarter() { if (state == has_quarter) { system.out.println("you can't insert another quarter"); } else if (state == no_quarter) { state = has_quarter; system.out.println("you inserted a quarter"); } else if (state == sold_out) { system.out.println("you can't insert a quarter, the machine is old out"); } else if (state == sold) { system.out.println("please wait, we're already giving you a gumball"); } }
写下代码
现在我们来实现糖果机。我们知道要利用实例变量持有当前的状态,然后需要处理所有可能发生的动作、行为和状态的转换。我们需要实现的动过包括:投入25分钱、退回25分钱、转动曲柄和发放糖果;也要检查糖果是否售罄。
public class gumballmachine { final static int sold_out = 0; final static int no_quarter = 1; final static int has_quarter = 2; final static int sold = 3; int state = sold_out; int count = 0; public gumballmachine(int count) { this.count = count; if (count > 0) { state = no_quarter; } } // 当有25分钱投进来,就会执行这里 public void insertquarter() { if (state == has_quarter) { system.out.println("you can't insert another quarter"); } else if (state == no_quarter) { state = has_quarter; system.out.println("you inserted a quarter"); } else if (state == sold_out) { system.out.println("you can't insert a quarter, the machine is sold out"); } else if (state == sold) { system.out.println("please wait, we're already giving you a gumball"); } } // 现在,如果顾客试着退回25分钱 public void ejectquarter() { if (state == has_quarter) { system.out.println("quarter returned"); state = no_quarter; } else if (state == no_quarter) { system.out.println("you haven't inserted a quarter"); } else if (state == sold) { system.out.println("sorry, you already turned the crank"); } else if (state == sold_out) { system.out.println("you can't eject, you haven't inserted a quarter yet"); } } // 顾客试着转动曲柄 public void turncrank() { if (state == sold) { system.out.println("turning twice doesn't get you another gumball!"); } else if (state == no_quarter) { system.out.println("you turned but there's no quarter"); } else if (state == sold_out) { system.out.println("you turned, but there are no gumballs"); } else if (state == has_quarter) { system.out.println("you turned..."); state = sold; dispense(); } } // 调用此方法,发放糖果 private void dispense() { if (state == sold) { system.out.println("a gumball comes rolling out the slot"); count = count - 1; if (count == 0) { system.out.println("oops, out of gumballs!"); state = sold_out; } else { state = no_quarter; } } else if (state == no_quarter) { system.out.println("you need to pay first"); } else if (state == sold_out) { system.out.println("no gumball dispensed"); } else if (state == has_quarter) { system.out.println("no gumball dispensed"); } } public void refill(int numgumballs) { this.count = numgumballs; state = no_quarter; } public string tostring() { stringbuffer result = new stringbuffer(); result.append("\nmighty gumball, inc."); result.append("\njava-enabled standing gumball model #2004\n"); result.append("inventory: " + count + " gumball"); if (count != 1) { result.append("s"); } result.append("\nmachine is "); if (state == sold_out) { result.append("sold out"); } else if (state == no_quarter) { result.append("waiting for quarter"); } else if (state == has_quarter) { result.append("waiting for turn of crank"); } else if (state == sold) { result.append("delivering a gumball"); } result.append("\n"); return result.tostring(); } }
现在,让我们进入一个小小的内部测试,看下是否符合了要求。
public class winnerstate implements state { gumballmachine gumballmachine; public winnerstate(gumballmachine gumballmachine) { this.gumballmachine = gumballmachine; } public void insertquarter() { system.out.println("please wait, we're already giving you a gumball"); } public void ejectquarter() { system.out.println("please wait, we're already giving you a gumball"); } public void turncrank() { system.out.println("turning again doesn't get you another gumball!"); } public void dispense() { gumballmachine.releaseball(); if (gumballmachine.getcount() == 0) { gumballmachine.setstate(gumballmachine.getsoldoutstate()); } else { gumballmachine.releaseball(); system.out.println("you're a winner! you got two gumballs for your quarter"); if (gumballmachine.getcount() > 0) { gumballmachine.setstate(gumballmachine.getnoquarterstate()); } else { system.out.println("oops, out of gumballs!"); gumballmachine.setstate(gumballmachine.getsoldoutstate()); } } } public void refill() { } public string tostring() { return "despensing two gumballs for your quarter, because you're a winner!"; } }
运行结果比较长,在这里就不进行打印啦,需要的朋友请自行根据代码进行执行。
变更需求来了,需要新设计
现在,客户要求当曲柄转动时,有10%的几率掉下来的是两颗糖果(多送你一颗),那你应该怎么做呢?
我们的计划是这样的:不要维护我们现有的代码,我们重写它以便于将状态对象封装在各自的类中,然后在动作发生时委托给当前状态。所以,我们要做的事情就是:
- 首先,我们定义一个state接口。在这个接口中,糖果机的每个动作都有一个对应的方法
- 然后为机器中的每个状态实现状态类。这些将负责在对应的状态下进行机器的行为
- 最后,我们要摆脱旧的条件代码,取而代之的方式是,将动作委托到状态类
你将会看到,我们不仅遵守了设计原则,实际上我们还实现了状态模式。接下来,我们定义状态接口和类,如下:
好了,到现在为止,我们下一步肯定是要去实现怎么改造了,对吧。在这里呢,给大家先留着,先去思考思考,如何进行改造。思路有了,类图有了,就看你怎么想啦。我们下一次揭晓。