状态模式
2016-09-12 本文已影响28人
Kwee
策略模式和状态模式是双胞胎,在出生时才分开。
你已经知道,策略模式是围绕可以互换的算法来创建成功业务的,
然而,状态走的是更崇高的路,它通过改变对象内部的状态来帮助对象控制自己的行为。
状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。


主函数代码:
public class test {
public static void main(String[] args) {
GumballMachine gumballMachine = new GumballMachine(10);
System.out.println(gumballMachine.getCount());
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine.getCount());
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
......
}
}