版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 黑龙江省绥化市2026年重点学校高一入学英语分班考试试题及答案
- 轮胎维修汽车轮胎动平衡调试技师(初级)考试试卷及答案
- 老年书法考级指导师岗位招聘考试试卷及答案
- 办公设备维修技师试卷及答案
- 肿瘤异质性研究进展
- 水利发电设备运行与维护规范(标准版)
- 能源行业安全管理指南(标准版)
- 旅游景点服务规范与质量管理指南(标准版)
- 物业管理服务流程与管理指南(标准版)
- 旅游景区应急预案与处置指南
- Intouch培训课件资料
- 2025年全球骨科创新年度白皮书-
- 2026年寒假德育实践作业竹马行新岁飒沓少年游【课件】
- 职业技术学校教师能力标准及评价
- 2026年皖西卫生职业学院单招职业适应性测试题库附答案解析
- 注意缺陷多动障碍(ADHD)远程认知行为干预方案
- Unit 1 A new start 核心素质评价(含答案)2025-2026学年外研版(2024)七年级英语上册
- 2025年自然博物馆招聘面试模拟试题集
- 《汽车空气动力学》课件(共八章)
- 《常见机械机构原理与应用》课件
- 2025年哈尔滨职业技术学院高职单招(数学)历年真题考点含答案解析
评论
0/150
提交评论