记忆潜处
With great power comes great responsibility
博客园
首页
社区
新随笔
联系
订阅
管理
随笔-42 评论-128 文章-0 trackbacks-4
Core Design Patterns(14) State 状态模式
VS 2008
应
应用程序中,常常会有这样一些有状态的对象,它们可能有很多种状态,在不同状态下,对象表现出的行为截然不同。在一般情况下,我们可能会写一些状态切换的if .. else ..语句块,往往造成代码丑陋,维护困难。
这时候,可以考虑使用状态模式。
1. 模式UML图
2. 应用
现在程序中需要频繁造作一类对象,我们称之为案件(Task),一个案件的生命周期是一个流程,从受理、派遣、处理、到最终完成,经历四种状态(其实还有更多,这里为了示例,我把它简化了)。因此很容易想到使用状态模式。
静态类图:
现在来看代码,Very simple
Task.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.State.BLL
{
public
class
Task
{
/**/
///
<summary>
///
案件唯一编号
///
</summary>
public
string
TaskId
{
get
;
set
; }
/**/
///
<summary>
///
状态
///
</summary>
public
ITaskState State
{
get
;
set
;}
public
Task(
string
taskId, ITaskState state)
{
this
.TaskId
=
taskId;
this
.State
=
state;
}
public
void
Execute()
{
this
.State.Execute(
this
);
}
}
}
ITaskState.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.State.BLL
{
public
interface
ITaskState
{
void
Execute(Task task);
}
}
AcceptingState.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.State.BLL
{
public
class
AcceptingState : ITaskState
{
ITaskState Members
#region
ITaskState Members
public
void
Execute(Task task)
{
Console.WriteLine(
"
{0} accepted
"
, task.TaskId);
//
执行完转到下一个状态
task.State
=
new
DispatchingState();
}
#endregion
}
}
DispatchingState.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.State.BLL
{
public
class
DispatchingState : ITaskState
{
ITaskState Members
#region
ITaskState Members
public
void
Execute(Task task)
{
Console.WriteLine(
"
{0} dispatched
"
, task.TaskId);
task.State
=
new
SolvingState();
}
#endregion
}
}
SolvingState.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.State.BLL
{
public
class
SolvingState : ITaskState
{
ITaskState Members
#region
ITaskState Members
public
void
Execute(Task task)
{
Console.WriteLine(
"
{0} solved
"
, task.TaskId);
task.State
=
new
FinishedState();
}
#endregion
}
}
FinishedState.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.State.BLL
{
public
class
FinishedState : ITaskState
{
ITaskState Members
#region
ITaskState Members
public
void
Execute(Task task)
{
Console.WriteLine(
"
{0} has been finished
"
, task.TaskId);
}
#endregion
}
}
Client
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
DesignPattern.State.BLL;
namespace
DesignPattern.State
{
class
Program
{
static
void
Main(
string
[] args)
{
Task t
=
new
Task(
"
0803A0007007
"
,
new
AcceptingState());
t.Execute();
t.Execute();
t.Execute();
t.Execute();
}
}
}
Output
posted on 2008-03-20 20:05
Tristan(GuoZhijian)
阅读(2254)
评论(8)
编辑
收藏
所属分类:
Design Pattern
评论:
#1楼
2008-03-21 09:02 |
Kingthy
这个模式好像看不出好处在哪..
假如用户不清楚里面有"task.State = new XXState();",下面这段代码让人看起来感到很迷惑:为什么要调用这么多次的Execute??
t.Execute();
t.Execute();
t.Execute();
t.Execute();
为什么不直接在"task.State = new XXState();"那里直接Execute呢?如:
public void Execute(Task task) {
Console.WriteLine("{0} solved", task.TaskId);
new FinishedState(Task);
}
这样某个ITaskState只对自己和它的下一级状态负责?这样不是更清晰吗?
回复
引用
查看
#2楼
[
楼主
] 2008-03-21 09:16 |
Tristan(Guozhijian)
@Kingthy
Client代码只是为了展示效果,可能没有实际意义,不多说了。
然后你说的第二个涉及两个问题
第一是关于什么时候传递context对象。
在new State构造函数里传递还是作为接口定义的方法参数传递都可以。
只不过用接口定义参数更严格,不是吗?
第二是否new XXState()哪里直接Execute,如果说是,那是不是不够灵活,如果我将来还要给各个XXState增加行为,那是不是改动很大?
:)
回复
引用
查看
#3楼
2008-03-21 10:13 |
John Rambo
IState实例相当于工作流中的节点。至于那四个连续的execute,是因为简便的缘故。。。很灵活,可以方便的扩展工作流
回复
引用
查看
#4楼
[
楼主
] 2008-03-21 11:00 |
Tristan(Guozhijian)
@John Rambo
that is it.
回复
引用
查看
#5楼
2008-03-21 11:19 |
SMan [未注册用户]
不是很明白,比较郁闷,估计基础还不行
回复
引用
#6楼
2008-03-21 11:49 |
辨色龙
如果能讲状态模式与策略模式的区别在哪里?就更好了
回复
引用
查看
#7楼
[
楼主
] 2008-03-21 12:57 |
Tristan(Guozhijian)
@辨色龙
Strategy:封装算法
State:封装基于状态的行为
回复
引用
查看
#8楼
2008-03-28 15:36 |
wenwen5g [未注册用户]
楼主写得很认真啊。
很有收获哦。加油。
回复
引用
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关文章:
小菜编程成长记(十三 设计模式不能戏说!设计模式怎就不能戏说?)
控件之ViewState
.NET设计模式系列文章
.NET设计模式(2):单件模式(Singleton Pattern)
新一代处理器将以Core i7之名正式登场
英特尔Nehalem处理器正式名称为Core i7
状态模式
VS2008里的网页,中文一编辑就变成了数字
XUnit Test Patterns这本书值得看看
推荐一些技术书籍
相关链接:
所属分类的其他文章:
Core Design Patterns(16) Chain of Responsibility 职责链模式
Core Design Patterns(15) Template Method 模版方法模式
Core Design Patterns(14) State 状态模式
Core Design Patterns(13) Strategy 策略模式
Core Design Patterns(12) Builder 建造者模式
Core Design Patterns(11) Abstract Factory 抽象工厂模式
Core Design Patterns(10) Singleton 单例模式
Core Design Patterns(9) Factory Method 工厂方法模式
Core Design Patterns(8) Prototype 原型模式
Core Design Patterns(7) Facade 外观模式
最新IT新闻:
Google股价跌破329美元 61%员工期权价值归零
十年祭:昔日明星软件今何在?
六大可能出售IT企业名单:SUN领头或被猎走
Linux内核2.6.27正式到来
《星际争霸2》一分为三 各种族依次登场
Tristan obtained a master's degree of mathematics at DongHua university in Mar. 2006. Now he works at Cognizant co. as a software engineer. He is currently working with c# mostly in web application development.He has a good understanding of CLR, asp.net, javascript, ajax, xml, xslt, OOP, design pattern, etc.
You can contact him
by email: promiss#live.cn
<
2008年3月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(3)
给我留言
查看留言
我参加的小组
博客园上海俱乐部
我参与的团队
上海.NET俱乐部(0/2248)
随笔分类
(42)
Asp.Net(5)
Asp.Net Ajax(14)
C#(3)
Design Pattern(16)
SqlServer2005(2)
Windows Forms App(2)
随笔档案
(42)
2008年4月 (1)
2008年3月 (12)
2008年2月 (18)
2008年1月 (7)
2007年12月 (3)
2007年11月 (1)
.Net Professionals
Ian Griffiths' Weblog
积分与排名
积分 - 35600
排名 - 1213
最新随笔
1. Core Design Patterns(16) Chain of Responsibility 职责链模式
2. Core Design Patterns(15) Template Method 模版方法模式
3. Core Design Patterns(14) State 状态模式
4. Core Design Patterns(13) Strategy 策略模式
5. Core Design Patterns(12) Builder 建造者模式
6. Core Design Patterns(11) Abstract Factory 抽象工厂模式
7. Core Design Patterns(10) Singleton 单例模式
8. Core Design Patterns(9) Factory Method 工厂方法模式
9. Core Design Patterns(8) Prototype 原型模式
10. Core Design Patterns(7) Facade 外观模式
11. Core Design Patterns(6) Adapter 适配器模式
12. Core Design Patterns(5) Flyweight 享元模式
13. Core Design Patterns(4) Composite 组合模式
14. Core Design Patterns(3) Bridge 桥接模式
15. Core Design Patterns(2) Proxy 代理模式
16. Core Design Patterns(1) Decorator 装饰模式
17. 老调重弹:插件式框架开发的一个简单应用
18. Behavior模型应用:可拖动的div容器
19. Microsoft Asp.Net Ajax框架入门(13) PageRequestManager
20. Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
最新评论
1. re: Microsoft Asp.Net Ajax框架入门(3) 操作DOM元素
怎么操作DOM的添加和删除
--kog
2. re: 浅述WinForm多线程编程与Control.Invoke的应用
多谢,多谢,帮我解决了个问题。
--Hecate_Eos
3. re: sqlserver2005数据库还原脚本
在management studio中使用restore时,生成的脚本如下: RESTORE DATABASE IAC FILE = N'IAC' FROM DISK = 'l:\IACSyst...
--小市民
4. re: Behavior模型应用:可拖动的div容器
哥们!加油,这几篇关于ASP.NET AJAX 的我都看了,不错。
--GoodGoodStudy
阅读排行榜
1. 浅述WinForm多线程编程与Control.Invoke的应用(3583)
2. 我的WinForm App自动更新(Live Update)架构(2356)
3. Core Design Patterns(14) State 状态模式(2254)
4. 通过继承ConfigurationSection,在web.config中增加自定义配置(1899)
5. 老调重弹:插件式框架开发的一个简单应用(1805)
评论排行榜
1. 我的WinForm App自动更新(Live Update)架构(36)
2. 浅述WinForm多线程编程与Control.Invoke的应用(24)
3. Microsoft Asp.Net Ajax框架入门(7) 名称空间、类、继承、接口(10)
4. 老调重弹:插件式框架开发的一个简单应用(8)
5. Core Design Patterns(14) State 状态模式(8)