本文主要包括四部分:1、状态模式的基本定义;2、适用场景;3、类结构图;4、代码示例
一、定义
State:允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它所属的类
二、适用场景:
1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。
2.一个操作中含有庞大的分支结构,并且这些分支决定于对象的状态。
三、类结构图:
四、示例
一个人考试成绩单,不同成绩的不同表现。
五、代码
创建学生对象:
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 }
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 }
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 }
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 }
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 }
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 }
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
《大话设计模式》