《Head First设计模式》阅读笔记.第十章1.状态(State)模式部分*设计谜题----------------------------------
《Head First设计模式》阅读笔记.第十章
1.状态(State)模式部分
*设计谜题
---------------------------------------------
状态转换图不变,在售出糖果时,如果剩余糖果数大于1,有10%的几率掉下2个。
---------------------------------------------
Sharpen your pencil
---------------------------------------------
A、B、C、D、E、F
---------------------------------------------
Sharpen your pencil
---------------------------------------------public class SoldOutState implements State { GumballMachine gumballMachine; public SoldOutState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter(){ System.out.println("Sorry,the gumballs are sold out."); } public void ejectQuarter(){ System.out.println("Sorry,you haven't insert a quarter."); } public void turnCrank(){ System.out.println("Sorry,you haven't insert a quarter"); } public void dispense(){ //do nothing }}---------------------------------------------
*运用状态模式给糖果机设计带来的益处是:
让每一个状态“对修改关闭”,让糖果机“对扩展开放”,这样可以方便地加入新的状态类。
状态(State)模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
Sharpen your pencil
---------------------------------------------public void refill(int count){ this.count = count; if(count>0){ this.state = noQuarterState; }}---------------------------------------------
连连看
/** * 钢笔 * * @author zangweiren * */public class FountainPen implements FountainPenState {FountainPenState fullState;FountainPenState halfFullState;FountainPenState emptyState;FountainPenState state;int inkLevel = 0;public FountainPen() {fullState = new FullState(this);halfFullState = new HalfFullState(this);emptyState = new EmptyState(this);state = emptyState;}@Overridepublic void empty() {state.empty();}@Overridepublic void fill() {state.fill();}@Overridepublic void write() {state.write();}void setState(FountainPenState state) {this.state = state;}FountainPenState getFullState() {return fullState;}FountainPenState getHalfFullState() {return halfFullState;}FountainPenState getEmptyState() {return emptyState;}}/** * 钢笔状态接口 * * @author zangweiren * */public interface FountainPenState {void fill();void write();void empty();}/** * 满 * * @author zangweiren * */public class FullState implements FountainPenState {FountainPen pen;public FullState(FountainPen pen) {this.pen = pen;}@Overridepublic void empty() {pen.inkLevel = 0;pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty now.");}@Overridepublic void fill() {System.out.println("Sorry,the fountain pen is already full.");}@Overridepublic void write() {pen.inkLevel = pen.inkLevel - 1;if (pen.inkLevel == 0) {pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty.");} else {pen.setState(pen.getHalfFullState());System.out.println("The fountain pen is half full.");}}}/** * 半满 * * @author zangweiren * */public class HalfFullState implements FountainPenState {FountainPen pen;public HalfFullState(FountainPen pen) {this.pen = pen;}@Overridepublic void empty() {pen.inkLevel = 0;pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty now.");}@Overridepublic void fill() {pen.inkLevel = 100;pen.setState(pen.getFullState());System.out.println("Congratulations,the fountain pen is full now.");}@Overridepublic void write() {pen.inkLevel = pen.inkLevel - 1;if (pen.inkLevel == 0) {pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty.");} else {System.out.println("The fountain pen is half full.");}}}/** * 空 * * @author zangweiren * */public class EmptyState implements FountainPenState {FountainPen pen;public EmptyState(FountainPen pen) {this.pen = pen;}@Overridepublic void empty() {System.out.println("Sorry,the fountain pen is already empty.");}@Overridepublic void fill() {pen.inkLevel = 100;pen.setState(pen.getFullState());System.out.println("Congratulations,the fountain pen is full now.");}@Overridepublic void write() {System.out.println("Sorry,the fountain pen is empty.");}}
测试程序:
---------------------------------------------
public class TestFountainPen {public static void main(String[] args) {FountainPen pen = new FountainPen();pen.fill();int count = 5;while (count > 0) {pen.write();count--;}pen.empty();pen.write();pen.fill();pen.write();pen.empty();}}
---------------------------------------------
测试结果:
---------------------------------------------
引用Congratulations,the fountain pen is full now.
The fountain pen is half full.
The fountain pen is half full.
The fountain pen is half full.
The fountain pen is half full.
The fountain pen is half full.
The fountain pen is empty now.
Sorry,the fountain pen is empty.
Congratulations,the fountain pen is full now.
The fountain pen is half full.
The fountain pen is empty now.