command模式
对照Command设计模式
耦合的例子1
public class Application
{
public Application()
{
Document doc=new Document();
doc.ShowText();//输出文本
Graphic grh= new Graphic();
grh.DrawGraphic();//画图形
}
}
public class Document
{
public void ShowText()
{
}
}
public class Graphic
{
public void DrawGraphic()
{
}
}
解耦后过程例子
public class Application1
{
System.Collections.ArrayList list;
public Application1()
{
foreach(ICommand cmd in list)
{
cmd.ExecuteCommand();
}
}
}
public class Document
{
public void ShowText()
{
}
}
public class Graphic
{
public void DrawGraphic()
{
}
}
//保持原有程序不变
//把对象的行为抽象出来ICommand
//把Document.ShowText()行为和Graphic.DrawGraphi()分别作为一个命令从ICommand行为对象继承
//把(对象A======对象B的行为)依赖关系转化为(对象A=====抽象行为)
public interface ICommand
{
void ExecuteCommand();
void ReDo();
void UnDo();
}
public class DocumentCommand:ICommand
{
private Document doc;
public void ExecuteCommand()
{
this.doc.ShowText();
}
public void ReDo()
{
}
public void UnDo()
{
}
}
public class GraphicCommand:ICommand
{
private Graphic graphic;
public void ExecuteCommand()
{
this.graphic.DrawGraphic();
}
public void ReDo()
{
}
public void UnDo()
{
}
}