读书笔记13:状态模式

1、概念

    当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。主要解决,当一个对象状态转换的条件表达式过于复杂时把状态的判断逻辑转移到表示不同状态的一系列类当中。将复杂的逻辑判断简化。
2、模型
    有三个角色,
    State——抽象状态类,封装一个与特定状态相关的行为。
    CreateState——具体状态,实现一个特定状态的行为。
    ConText——维护一个CreateState实例,该实例是根据状态传入的。
 程序实现:

[csharp]  view plain copy print ?
  1. public abstract class State  
  2. {  
  3.   public abstract void Behavior(ConText conText);  
  4. }  
  5.   
  6. public class CreateStateFirst:State  
  7. {  
  8.   public override Behavior(ConText conText)  
  9.   {   
  10.     conText.state = new CreateStateSecond();  
  11.   }  
  12. }  
  13.   
  14. public class CreateStateSecond:State  
  15. {  
  16.   public override Behavior(ConText conText)  
  17.   {   
  18.     conText.state = new CreateStateFirst();  
  19.   }  
  20. }  
  21.   
  22. public class ConText  
  23. {  
  24.    private State state;  
  25.      
  26.    public ConText(State diffrentState)  
  27.    {  
  28.      state=diffrentState;  
  29.    }  
  30.      
  31.    public State CurretState  
  32.    {  
  33.      set{state=value;}  
  34.      get{return state;}  
  35.    }  
  36.      
  37.    public void DealWith()  
  38.    {  
  39.      state.Behavior(this);  
  40.    }  
  41. }  
  42.   
  43. static void Main(String[] args)  
  44. {  
  45.   Context conText = new ConText(new CreateStateFirst());  
  46.     
  47.   conText.DealWith();  
  48. }  

  这样将各个状态对应的行为分开来(CreateStateFirst,CreateStateSecond),它们由ConText负责维护。
 
 下面以一个项目中的小例子看一下状态模式的应用。需求:根据每天不同的时间切换软件的皮肤颜色,以模型表示。
 
 颜色抽象状态类:

[csharp]  view plain copy print ?
  1. public abstract class ColorState  
  2. {  
  3.   public abstract void ShowColorSkinFile(SoftWare softWare);  
  4. }  
[csharp]  view plain copy print ?
  1. public class MorningColor:ColorState  
  2. {  
  3.   public override ShowColorSkinFile(SoftWare msoftWare)  
  4.   {   
  5.     if(msoftWare.time<12)  
  6.     {  
  7.        ConSole.writeLine("早晨的软件皮肤");  
  8.     }  
  9.     else   
  10.     {  
  11.       msoftWare.SetColorState(new AfterNoonColor());  
  12.     }  
  13.   }  
  14. }  
[csharp]  view plain copy print ?
  1. public class AfterNoonColor:ColorState  
  2.   
  3.  public override ShowColorSkinFile(SoftWare msoftWare)  
  4.  {   
  5.    if(msoftWare.time<18)  
  6.    {  
  7.       ConSole.writeLine("下午的软件皮肤");  
  8.    }  
  9.     else   
  10.    {  
  11.      msoftWare.SetColorState(new EveningNoonColor());  
  12.    }  
  13.  }  


 

[csharp]  view plain copy print ?
  1. public class EveningNoonColor:ColorState  
  2. {  
  3.   public override ShowColorSkinFile(SoftWare msoftWare)  
  4.   {   
  5.     if(msoftWare.time<24)  
  6.     {  
  7.        ConSole.writeLine("晚上的软件皮肤");  
  8.     }  
  9.      else   
  10.     {  
  11.       msoftWare.SetColorState(new MorningColor());  
  12.     }  
  13.   }  
  14. }  


 

[csharp]  view plain copy print ?
  1.  public class SoftWare  
  2. {  
  3.    private ColorState state;  
  4.      
  5.    public SoftWare()  
  6.    {  
  7.      state=new CommonColor();  
  8.    }  
  9.      
  10.    public int Time  
  11.    {  
  12.      set;  
  13.      get;  
  14.    }  
  15.    public void SetColorState(ColorState cState)  
  16.    {  
  17.      state=cState;  
  18.    }  
  19.    public void Show()  
  20.    {  
  21.      state.ShowColorSkinFile(this);  
  22.    }  
  23. }  

 
 

[csharp]  view plain copy print ?
  1. static void Main(String[] args)  
  2. {  
  3.   SoftWare softe = new SoftWare();  
  4.     
  5.   softe.Time =8;  
  6.     
  7.   softe.Show();  
  8.     
  9.   softe.Time =14;  
  10.     
  11.   softe.Show();  
  12.     
  13.   softe.Time =20;  
  14.     
  15.   softe.Show();  
  16. }  

 
 分别显示:早中晚的颜色。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值