大话设计模式之状态模式

转载地址:

http://www.cnblogs.com/xyz168/archive/2011/06/26/2090651.html


本文主要包括四部分:1、状态模式的基本定义;2、适用场景;3、类结构图;4、代码示例

一、定义

State:允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它所属的类

二、适用场景:

1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。

2.一个操作中含有庞大的分支结构,并且这些分支决定于对象的状态。

三、类结构图:

clip_image002

四、示例

一个人考试成绩单,不同成绩的不同表现。

五、代码

创建学生对象:

View Code
1  using  System;
2    using  System.Collections.Generic;
3    using  System.Linq;
4    using  System.Text;
5 
6  namespace  y.StatePatternExample
7  {
8  internal   class  Student
9  {
10  int  grade  =   0 ;
11  GradeReport objGradeReport  =   null ;
12  string  name  =   string .Empty;
13  private  Student()
14  { }
15 
16  internal  Student( string  name, GradeReport gradeReport)
17  {
18  this .name  =  name;
19  this .objGradeReport  =  gradeReport;
20  }
21 
22  internal   int  Grade
23  {
24  get  {  return  grade; }
25  set  { grade  =  value; }
26  }
27 
28  internal   string  Name
29  {
30  get  {  return  name; }
31  set  { name  =  value; }
32  }
33 
34  internal   void  SetGradeReport(GradeReport objGradeReport)
35  {
36  this .objGradeReport  =  objGradeReport;
37  }
38 
39  internal   void  ShowMark()
40  {
41  objGradeReport.ShowGrade( this );
42  }
43 
44  }
45  }

不同成绩类:

View Code
1  using  System;
2  using  System.Collections.Generic;
3  using  System.Linq;
4  using  System.Text;
5 
6  namespace  y.StatePatternExample
7  {
8  abstract   class  GradeReport
9  {
10  abstract   internal   void  ShowGrade(Student student);
11  }
12  }
View Code
1  using  System;
2  using  System.Collections.Generic;
3  using  System.Linq;
4  using  System.Text;
5 
6  namespace  y.StatePatternExample
7  {
8  internal   class  FailReport:GradeReport
9  {
10  internal   override   void  ShowGrade(Student student)
11  {
12  if  (student.Grade  <   60 )
13  {
14  Console.WriteLine( " 成绩单:不及格。需要勤家努力 " );
15  }
16  else
17  {
18  student.SetGradeReport( new  CommonReport());
19  student.ShowMark();
20  }
21  }
22  }
23  }
View Code
1  using  System;
2  using  System.Collections.Generic;
3  using  System.Linq;
4  using  System.Text;
5 
6  namespace  y.StatePatternExample
7  {
8  internal   class  GoodReport:GradeReport
9  {
10  internal   override   void  ShowGrade(Student student)
11  {
12 
13  Console.WriteLine( " 成绩单:良好。可以睡觉了 " );
14 
15  }
16  }
17  }
View Code
1  using  System;
2  using  System.Collections.Generic;
3  using  System.Linq;
4  using  System.Text;
5 
6  namespace  y.StatePatternExample
7  {
8  internal   class  CommonReport:GradeReport
9  {
10  internal   override   void  ShowGrade(Student student)
11  {
12  if  (student.Grade  <   80 )
13  {
14  Console.WriteLine( " 成绩单:一般。继续玩玩打打 " );
15  }
16  else
17  {
18  student.SetGradeReport( new  GoodReport());
19  student.ShowMark();
20  }
21  }
22  }
23  }

主程序:

View Code
1  using  System;
2  using  System.Collections.Generic;
3  using  System.Linq;
4  using  System.Text;
5 
6  namespace  y.StatePatternExample
7  {
8  class  Program
9  {
10  static   void  Main( string [] args)
11  {
12  Console.Title  =   " 成绩显示结果 " ;
13  Console.WriteLine( " Press any key to exit program.\r\n " );
14  Student student  =   new  Student( " 诸葛亮 " , new  FailReport());
15 
16  Console.WriteLine( " {0}参加英语考试:\r\n " , student.Name);
17  student.Grade  =   30 ;
18  Console.WriteLine( " 第一次考试成绩:{0} " , student.Grade);
19  student.ShowMark();
20  student.Grade  =   70 ;
21  Console.WriteLine( " 第二次考试成绩:{0} " , student.Grade);
22  student.ShowMark();
23  student.Grade  =   90 ;
24  Console.WriteLine( " 第三次考试成绩:{0} " , student.Grade);
25  student.ShowMark();
26 
27  Console.ReadKey();
28  }
29  }
30  }

参考文献:

http://baike.baidu.com/view/3589977.htm

《大话设计模式》


状态模式是一种行为型设计模式,它允许对象在内部状态改变时改变它的行为。在状态模式中,我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的context对象。 在C++中实现状态模式,可以按照以下步骤进行: 1. 创建一个抽象状态类,其中包含一个纯虚函数,该函数将在具体状态类中实现。 2. 创建具体状态类,这些类继承自抽象状态类,并实现其纯虚函数。 3. 创建context类,该类包含一个指向抽象状态类的指针,并在其内部维护状态。 4. 在context类中实现一些操作,这些操作将根据当前状态调用不同的具体状态类中的方法。 下面是一个简单的示例代码,演示了如何在C++中实现状态模式: ```cpp #include <iostream> using namespace std; // 抽象状态类 class State { public: virtual void handle() = 0; }; // 具体状态类A class ConcreteStateA : public State { public: void handle() { cout << "State A handled." << endl; } }; // 具体状态类B class ConcreteStateB : public State { public: void handle() { cout << "State B handled." << endl; } }; // context类 class Context { private: State* state; public: Context(State* s) { state = s; } void setState(State* s) { state = s; } void request() { state->handle(); } }; // 示例代码 int main() { State* stateA = new ConcreteStateA(); State* stateB = new ConcreteStateB(); Context* context = new Context(stateA); context->request(); // 输出:State A handled. context->setState(stateB); context->request(); // 输出:State B handled. return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值