c23种设计模式11状态模式_第1页
c23种设计模式11状态模式_第2页
c23种设计模式11状态模式_第3页
c23种设计模式11状态模式_第4页
c23种设计模式11状态模式_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、状态模式(State Pattern) 对象状态影响对象行为:    对象拥有不同的状态,往往会行使不同的行为.                   动机:    在软件构建过程中,某些对象的状态如果改变以及其行为也会随之而发生变化,比如文档处于只读状态,其支持的行为和读写状态支持的行为就可能完全不同。    如何在运行时根据对象的状态来透明更改对象的行为?而不会为对象操作和状

2、态转化之间引入紧耦合?意图: 允许一个对象在其内部状态改变时改变它的行为。从而使对象看起来似乎修改了其行为。  -设计模式GOF结构图:    适用性:    1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。2.一个操作中含有庞大的多分支的等条件语句,且这些分支依赖于该对象的状态。这个状态通常用一个或多个枚举常量表示。通常,有多个操作包含这一相同的条件结构。State模式将每一个分支放入一个独立的类中。这使得你可根据对象自身的情况将对象的状态作为一个对象,这一对象可以不依赖于其他对象而独

3、立变化。代码实现:      class MainApp      static void Main()          / Open a new account      Account account = new 

4、Account("Jim Johnson");      / Apply financial transactions      account.Deposit(500.0);      account.Deposit(300.0);      account.Deposit(550.0);

5、0;     account.PayInterest();      account.Withdraw(2000.00);      account.Withdraw(1100.00);      / Wait for user      Console.Read(); 

6、;       / "State"  abstract class State      protected Account account;    protected double balance;    protected double interest;

7、    protected double lowerLimit;    protected double upperLimit;    / Properties    public Account Account          get return

8、0;account;       set account = value;         public double Balance          get return balance;      

9、60;set balance = value;         public abstract void Deposit(double amount);    public abstract void Withdraw(double amount);    public abstract void&

10、#160;PayInterest();    / "ConcreteState"  / Account is overdrawn  class RedState : State      double serviceFee;    / Constructor    pu

11、blic RedState(State state)          this.balance = state.Balance;      this.account = state.Account;      Initialize();       

12、; private void Initialize()          / Should come from a datasource      interest = 0.0;      lowerLimit = -100.0;  

13、60;   upperLimit = 0.0;      serviceFee = 15.00;        public override void Deposit(double amount)          balance +=&

14、#160;amount;      StateChangeCheck();        public override void Withdraw(double amount)          amount = amount - serviceFee;  

15、    Console.WriteLine("No funds available for withdrawal!");        public override void PayInterest()          / No interest is&#

16、160;paid        private void StateChangeCheck()          if (balance > upperLimit)              account.State

17、60;= new SilverState(this);              / "ConcreteState"  / Silver is non-interest bearing state  class SilverState : State   &#

18、160;  / Overloaded constructors    public SilverState(State state) :      this( state.Balance, state.Account)              public 

19、SilverState(double balance, Account account)          this.balance = balance;      this.account = account;      Initialize();     

20、   private void Initialize()          / Should come from a datasource      interest = 0.0;      lowerLimit = 0.0; &

21、#160;    upperLimit = 1000.0;        public override void Deposit(double amount)          balance += amount;      StateC

22、hangeCheck();        public override void Withdraw(double amount)          balance -= amount;      StateChangeCheck();     &#

23、160;  public override void PayInterest()          balance += interest * balance;      StateChangeCheck();        private void 

24、;StateChangeCheck()          if (balance < lowerLimit)              account.State = new RedState(this);       &#

25、160;    else if (balance > upperLimit)              account.State = new GoldState(this);              

26、/ "ConcreteState"  / Interest bearing state  class GoldState : State      / Overloaded constructors    public GoldState(State state)     

27、60;: this(state.Balance,state.Account)              public GoldState(double balance, Account account)          this.balance = balance;  

28、;    this.account = account;      Initialize();        private void Initialize()          / Should come from a data

29、base      interest = 0.05;      lowerLimit = 1000.0;      upperLimit = 10000000.0;        public override void Deposit(double

30、 amount)          balance += amount;      StateChangeCheck();        public override void Withdraw(double amount)     &#

31、160;    balance -= amount;      StateChangeCheck();        public override void PayInterest()          balance += interest

32、60;* balance;      StateChangeCheck();        private void StateChangeCheck()          if (balance < 0.0)       

33、       account.State = new RedState(this);            else if (balance < lowerLimit)              

34、;account.State = new SilverState(this);              / "Context"  class Account      private State state;    private str

35、ing owner;    / Constructor    public Account(string owner)          / New accounts are 'Silver' by default      this.owne

36、r = owner;      state = new SilverState(0.0, this);        / Properties    public double Balance          get 

37、return state.Balance;         public State State          get return state;       set state = value;     

38、    public void Deposit(double amount)          state.Deposit(amount);      Console.WriteLine("Deposited 0:C - ", amount);     

39、60;Console.WriteLine(" Balance = 0:C", this.Balance);      Console.WriteLine(" Status = 0n" ,        this.State.GetType().Name);      Console.WriteLine("");        public void Withdraw(double amount)          state.Withdraw

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论