设计模式--Command--行为型
来源:互联网 发布:淘宝麦兜足球装备 编辑:程序博客网 时间:2024/05/16 00:24
耦合与变化
耦合是软件不能抵御变化灾难的根本性原因。不仅
实体对象与实体对象之间存在耦合关系,实体对象
与行为操作之间也存在耦合关系。
动机(Motivation)
在软件构建过程中,“行为请求者”与“行为实现者”
通常呈现一种“紧耦合”。但在某些场合——比如需
要对行为进行“记录、撤销/重做(undo/redo)、事
务”等处理,这种无法抵御变化的紧耦合是不合适
的。
在这种情况下,如何将“行为请求者”与“行为实现者”
解耦?将一组行为抽象为对象,可以实现二者之间
的松耦合。
意图(Intent)
将一个请求封装为一个对象,从而使你可用不同的请
求对客户进行参数化;对请求排队或记录请求日志,
以及支持可撤销的操作。
——《设计模式》GoF
结构(Structure)
没有command时候
class Application
...{
public void Show()
...{
Document doc=new Document();
doc.ShowText();
Graphics graph=new Graphics();
graph.ShowGraphics();
}
}
class Document
...{
public void ShowText()
...{
//....
}
}
class Graphics
...{
public void ShowGraphics()
...{
}
}
/**/////////////////////////////////
public interface class Command //是一些行为
...{
public void Show();
public void Undo();
public void Redo();
}
class Document:Command //即是文档对象又是行为对象
...{
public virtual void Show()
...{
//....
}
}
class Graphics :Command
...{
public virtual void Show()
...{
//...
}
}
class Application
...{
ArrayList<Command> stack;
ArrayList<Command> UndoList;
class Application
...{
public void Show()
...{
Document doc=new Document();
doc.ShowText();
Graphics graph=new Graphics();
graph.ShowGraphics();
}
}
class Document
...{
public void ShowText()
...{
//....
}
}
class Graphics
...{
public void ShowGraphics()
...{
}
}
/**/////////////////////////////////
public interface class Command //是一些行为
...{
public void Show();
public void Undo();
public void Redo();
}
class Document:Command //即是文档对象又是行为对象
...{
public virtual void Show()
...{
//....
}
}
class Graphics :Command
...{
public virtual void Show()
...{
//...
}
}
class Application
...{
ArrayList<Command> stack;
ArrayList<Command> UndoList;
....
..
public void Show()
...{
foreach(Command c in list)
...{
c.Show();
}
}
public void Undo()
...{
if(canUndo)
...{Command command=stack.pop();
command.Undo();
undoLIst.Add(command);
}
}
public void Redo()
...{
if(canRedo)
...{
Command command=undoList.pop();
command.Redo();
}
}
}
/**//////////////////////////////////
/////////////////////////////////
上面会带来一些问题呀,就是Document 即当行为对象又
public void Show()
...{
foreach(Command c in list)
...{
c.Show();
}
}
public void Undo()
...{
if(canUndo)
...{Command command=stack.pop();
command.Undo();
undoLIst.Add(command);
}
}
public void Redo()
...{
if(canRedo)
...{
Command command=undoList.pop();
command.Redo();
}
}
}
/**//////////////////////////////////
/////////////////////////////////
上面会带来一些问题呀,就是Document 即当行为对象又
当原本文档的抽象,这样在对象的思想会又出路
下面这个方案可以分离行为和本身的
//已经存在的,实现细节,底层实现
class Document
...{
public void ShowText()
...{
//....
}
}
class Graphics
...{
public void ShowGraphics()
...{
}
}
//实现Command设计模式
public interface class Command //是一些行为
...{
public void Show();
public void Undo();
public void Redo();
}
//具体化的命令对-从抽象意义来讲,DocumentCommand 表示
public DocumentCommand :Command
...{
Document document;
public DocumentCommand(Document doc)
...{ document=doc;
}
public void Show()
...{
document.ShowText();
//...
}
}
public GraphicsCommand:Command
...{
Graphics g;
public GraphicsCommand(Graphic g)
...{ this.g=g;
}
public void Show()
...{
g.ShowGraphics();
//...
}
}
//已经存在的,实现细节,底层实现
class Document
...{
public void ShowText()
...{
//....
}
}
class Graphics
...{
public void ShowGraphics()
...{
}
}
//实现Command设计模式
public interface class Command //是一些行为
...{
public void Show();
public void Undo();
public void Redo();
}
//具体化的命令对-从抽象意义来讲,DocumentCommand 表示
public DocumentCommand :Command
...{
Document document;
public DocumentCommand(Document doc)
...{ document=doc;
}
public void Show()
...{
document.ShowText();
//...
}
}
public GraphicsCommand:Command
...{
Graphics g;
public GraphicsCommand(Graphic g)
...{ this.g=g;
}
public void Show()
...{
g.ShowGraphics();
//...
}
}
结果 图
Command模式的几个要点
• Command模式的根本目的在于将“行为请求者”与“行为实
现者” 解耦,在面向对象语言中,常见的实现手段是“将行
为抽象为对象”。
• 实现Command接口的具体命令对象ConcreteCommand
有时候根据需要可能会保存一些额外的状态信息。
• 通过使用Composite模式,可以将多个“命令”封装为一个
“复合命令”MacroCommand。
• Command模式与C#中的Delegate有些类似。但两者定义
行为接口的规范有所区别:Command以面向对象中的“接
口-实现”来定义行为接口规范,更严格,更符合抽象原
则;Delegate以函数签名来定义行为接口规范,更灵活,
但抽象能力比较弱。
- 设计模式--Command--行为型
- 设计模式-行为模式(Command)
- [设计模式-行为型]命令模式(Command)
- Command--------行为型设计模式之二
- 【设计模式攻略】行为型模式之Command模式
- 设计模式(14)-行为型模式-command模式
- 设计模式深入学习--Command 命令模式(行为型模式)
- 设计模式之命令(Command)---对象行为型模式
- 设计模式笔记--行为型模式之二--Command
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 重学设计模式 - Command(命令)模式 - 行为型
- 设计模式(14)-行为型-命令模式(Command)
- 设计模式--命令模式Command(行为型)
- 设计模式(行为型)之命令模式(Command Pattern)
- JAVA设计模式(18) —<行为型>命令模式(Command)
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 2005岁末BLOG程序大评点
- 以前几篇哦
- 2006最酷小说《上上床,谈谈心》
- 我的博客
- 第三次上机实验的程序设计思路分析及解答
- 设计模式--Command--行为型
- linux下JAVA环境变量的设置
- 只因是VB程序员被未来岳父轰出门
- 等待
- SQL语言查询基础:连接查询 联合查询
- 还有10分钟九点
- spring学习之一:了解spring
- 19宽屏在FC5 linux下的设置
- 很多关注QT,留了许多话,感谢感谢