打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
《Head First设计模式》阅读笔记.第十章
1.状态(State)模式部分

*设计谜题
---------------------------------------------
状态转换图不变,在售出糖果时,如果剩余糖果数大于1,有10%的几率掉下2个。
---------------------------------------------

Sharpen your pencil
---------------------------------------------
A、B、C、D、E、F
---------------------------------------------

Sharpen your pencil
引用
---------------------------------------------
NoQuarterState:
turnCrank--告诉顾客投入硬币
dispense--什么都不做

HasQuarterState:
insertQuarter--告诉顾客已经有硬币
ejectQuarter--退回硬币,进入NoQuarterState
dispense--掉下1颗糖果,检查剩余糖果数量,大于0,进入NoQuarterState,等于0,进入SoldOutState(更好的方式:转入SoldState处理)

SoldState:
ejectQuarter--告诉顾客正在售出糖果
turnCrank--告诉顾客您没有投入硬币

SoldOutState:
insertQuarter--告诉顾客已售罄
ejectQuarter--告诉顾客您没有投入硬币
dispense--什么都不做

WinnerState:
insertQuarter--告诉顾客正在售出糖果
ejectQuarter--告诉顾客您没有投入硬币
turnCrank--告诉顾客您没有投入硬币
dispense--掉下2颗糖果,检查剩余糖果数量,大于0,进入NoQuarterState,等于0,进入SoldOutState
---------------------------------------------


Brain Power
---------------------------------------------
将调用dispense()方法的代码放到State子类中去实现。
---------------------------------------------

Sharpen your pencil
Java代码
  1. ---------------------------------------------   
  2. public class SoldOutState implements State {   
  3.   GumballMachine gumballMachine;   
  4.      
  5.   public SoldOutState(GumballMachine gumballMachine) {   
  6.     this.gumballMachine = gumballMachine;   
  7.   }   
  8.      
  9.   public void insertQuarter(){   
  10.     System.out.println("Sorry,the gumballs are sold out.");   
  11.   }   
  12.      
  13.   public void ejectQuarter(){   
  14.     System.out.println("Sorry,you haven't insert a quarter.");   
  15.   }   
  16.      
  17.   public void turnCrank(){   
  18.     System.out.println("Sorry,you haven't insert a quarter");   
  19.   }   
  20.      
  21.   public void dispense(){   
  22.     //do nothing   
  23.   }   
  24. }   
  25. ---------------------------------------------  


*运用状态模式给糖果机设计带来的益处是:
让每一个状态“对修改关闭”,让糖果机“对扩展开放”,这样可以方便地加入新的状态类。

状态(State)模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。

Sharpen your pencil
Java代码
  1. ---------------------------------------------   
  2. public void refill(int count){   
  3.   this.count = count;   
  4.   if(count>0){   
  5.     this.state = noQuarterState;   
  6.   }   
  7. }   
  8. ---------------------------------------------  


连连看
引用
---------------------------------------------
状态--封装基于状态的行为,并将行为委托到当前状态
策略--将可以互换的行为封装起来,然后使用委托的方法,决定使用哪一个行为
模板方法--由子类决定如何实现算法中的某些步骤
---------------------------------------------


2.状态(State)模式小结

*状态允许一个对象基于内部状态而拥有不同的行为。

*和程序状态机(PSM)不同,状态模式用类代表状态。

*Context会将行为委托给当前状态对象。

*通过把每个状态封装进一个类,我们把以后要做的任何改变局部化了。

*状态(State)模式和策略(Strategy)模式类图相同,但是它们有不同的意图。

*策略(Strategy)模式通常会用行为或算法来配置Context类。

*状态(State)模式允许Context随着状态的改变而改变行为。

*状态转换可以由State类或Context类来控制。

*使用状态模式通常会导致设计中类的数目大量增加。

*状态可以被多个Context实例共享。

3.状态(State)模式实例

Java代码
  1. /**  
  2.  * 钢笔  
  3.  *   
  4.  * @author zangweiren  
  5.  *   
  6.  */  
  7. public class FountainPen implements FountainPenState {   
  8.     FountainPenState fullState;   
  9.     FountainPenState halfFullState;   
  10.     FountainPenState emptyState;   
  11.   
  12.     FountainPenState state;   
  13.     int inkLevel = 0;   
  14.   
  15.     public FountainPen() {   
  16.         fullState = new FullState(this);   
  17.         halfFullState = new HalfFullState(this);   
  18.         emptyState = new EmptyState(this);   
  19.         state = emptyState;   
  20.     }   
  21.   
  22.     @Override  
  23.     public void empty() {   
  24.         state.empty();   
  25.     }   
  26.   
  27.     @Override  
  28.     public void fill() {   
  29.         state.fill();   
  30.     }   
  31.   
  32.     @Override  
  33.     public void write() {   
  34.         state.write();   
  35.     }   
  36.   
  37.     void setState(FountainPenState state) {   
  38.         this.state = state;   
  39.     }   
  40.   
  41.     FountainPenState getFullState() {   
  42.         return fullState;   
  43.     }   
  44.   
  45.     FountainPenState getHalfFullState() {   
  46.         return halfFullState;   
  47.     }   
  48.   
  49.     FountainPenState getEmptyState() {   
  50.         return emptyState;   
  51.     }   
  52.   
  53. }   
  54.   
  55. /**  
  56.  * 钢笔状态接口  
  57.  *   
  58.  * @author zangweiren  
  59.  *   
  60.  */  
  61. public interface FountainPenState {   
  62.     void fill();   
  63.   
  64.     void write();   
  65.   
  66.     void empty();   
  67. }   
  68.   
  69. /**  
  70.  * 满  
  71.  *   
  72.  * @author zangweiren  
  73.  *   
  74.  */  
  75. public class FullState implements FountainPenState {   
  76.     FountainPen pen;   
  77.   
  78.     public FullState(FountainPen pen) {   
  79.         this.pen = pen;   
  80.     }   
  81.   
  82.     @Override  
  83.     public void empty() {   
  84.         pen.inkLevel = 0;   
  85.         pen.setState(pen.getEmptyState());   
  86.         System.out.println("The fountain pen is empty now.");   
  87.     }   
  88.   
  89.     @Override  
  90.     public void fill() {   
  91.         System.out.println("Sorry,the fountain pen is already full.");   
  92.     }   
  93.   
  94.     @Override  
  95.     public void write() {   
  96.         pen.inkLevel = pen.inkLevel - 1;   
  97.         if (pen.inkLevel == 0) {   
  98.             pen.setState(pen.getEmptyState());   
  99.             System.out.println("The fountain pen is empty.");   
  100.         } else {   
  101.             pen.setState(pen.getHalfFullState());   
  102.             System.out.println("The fountain pen is half full.");   
  103.         }   
  104.     }   
  105.   
  106. }   
  107.   
  108. /**  
  109.  * 半满  
  110.  *   
  111.  * @author zangweiren  
  112.  *   
  113.  */  
  114. public class HalfFullState implements FountainPenState {   
  115.     FountainPen pen;   
  116.   
  117.     public HalfFullState(FountainPen pen) {   
  118.         this.pen = pen;   
  119.     }   
  120.   
  121.     @Override  
  122.     public void empty() {   
  123.         pen.inkLevel = 0;   
  124.         pen.setState(pen.getEmptyState());   
  125.         System.out.println("The fountain pen is empty now.");   
  126.     }   
  127.   
  128.     @Override  
  129.     public void fill() {   
  130.         pen.inkLevel = 100;   
  131.         pen.setState(pen.getFullState());   
  132.         System.out.println("Congratulations,the fountain pen is full now.");   
  133.     }   
  134.   
  135.     @Override  
  136.     public void write() {   
  137.         pen.inkLevel = pen.inkLevel - 1;   
  138.         if (pen.inkLevel == 0) {   
  139.             pen.setState(pen.getEmptyState());   
  140.             System.out.println("The fountain pen is empty.");   
  141.         } else {   
  142.             System.out.println("The fountain pen is half full.");   
  143.         }   
  144.     }   
  145.   
  146. }   
  147.   
  148. /**  
  149.  * 空  
  150.  *   
  151.  * @author zangweiren  
  152.  *   
  153.  */  
  154. public class EmptyState implements FountainPenState {   
  155.     FountainPen pen;   
  156.   
  157.     public EmptyState(FountainPen pen) {   
  158.         this.pen = pen;   
  159.     }   
  160.   
  161.     @Override  
  162.     public void empty() {   
  163.         System.out.println("Sorry,the fountain pen is already empty.");   
  164.     }   
  165.   
  166.     @Override  
  167.     public void fill() {   
  168.         pen.inkLevel = 100;   
  169.         pen.setState(pen.getFullState());   
  170.         System.out.println("Congratulations,the fountain pen is full now.");   
  171.     }   
  172.   
  173.     @Override  
  174.     public void write() {   
  175.         System.out.println("Sorry,the fountain pen is empty.");   
  176.     }   
  177.   
  178. }  


测试程序:
---------------------------------------------
Java代码
  1. public class TestFountainPen {   
  2.     public static void main(String[] args) {   
  3.         FountainPen pen = new FountainPen();   
  4.         pen.fill();   
  5.         int count = 5;   
  6.         while (count > 0) {   
  7.             pen.write();   
  8.             count--;   
  9.         }   
  10.         pen.empty();   
  11.         pen.write();   
  12.         pen.fill();   
  13.         pen.write();   
  14.         pen.empty();   
  15.     }   
  16.   
  17. }  

---------------------------------------------

测试结果:
---------------------------------------------
引用
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.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JDK 6 探秘之三:Java.lang包的新特性
Java8
数组上的Java递归
Spring入门基础(2)
求!!关于用java判断输入3个数组成什么三角形
Artisan specialized in fountain pen repairing in Beijing
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服