命令模式

代码
using System;
using System.Collections;
using System.Collections.Generic;

public class Document
{
    
public string strContent;
    
public Document()
    {
        strContent
="";
    }
}

public abstract class Command
{
    
public Command()
    {}
    
public abstract void Excute();
}

public class WriteCommand:Command
{
    Document doc;
    ArrayList ObjectState;
    
public WriteCommand(Document doc,ArrayList state)
    {
        
this.doc=doc;
        
this.ObjectState=state;
    }
    
public override void Excute()
    {
        doc.strContent
+=Console.ReadLine();
        ObjectState.Add(doc,strContent);
    }
}

public class DeleteCommand:Command
{
    Document doc;
    ArrayList ObjectState;
    
public DeleteCommand(Document doc,ArrayList state)
    {
        
this.doc=doc;
        
this.ObjectState=state;
    }
    
public override void Excute()
    {
        doc.strContent
=doc.strContent.Substring(0,doc.strContent.Length-1);
        ObjectState.Add(doc,strContent);
    }
}

public class UnDoCommand:Command
{
    Document doc;
    ArrayList ObjectState;
    
public UnDoCommand(Document doc,ArrayList state)
    {
        
this.doc=doc;
        
this.ObjectState=state;
    }
    
public override void Excute()
    {
        doc.strContent
=(string)ObjectState[ObjectState.Count-2];
        ObjectState.Add(doc,strContent);
    }
}

public class MyClass
{
    
public static void Main()
    {
        Document doc
=new Document();
        Console.WriteLine(
"Please input Command:");
        
string strOperation=Console.ReadLine();
        Command com
=null;
        ArrayList mylist
=new ArrayList();
        
while(strOperation!="Exit")
        {
            
switch(strOperation.ToLower())
            {
                
case "write":
                    com
=new WriteCommand(doc,mylist);
                    com.Excute();
                    Console.WriteLine(
"Write Operation:"+doc.strContent);
                    
break;
                
case "del":
                    com
=new DeleteCommand(doc,mylist);
                    com.Excute();
                    Console.WriteLine(
"Delete Operation:"+doc.strContent);
                    
break;
                
case "undo":
                    com
=new UnDoCommand(doc,mylist);
                    com.Excute();
                    Console.WriteLine(
"Undo Operation:"+doc.strContent);
                    
break;
                
default:
                    Console.WriteLine(
"Wrong Operation");
                    
break;
            }
            Console.WriteLine(
"Please input next Operation!!");
            strOperation
=Console.ReadLine();
        }
    }
}

Command命令模式介绍:

Command命令模式是一种对象行为型模 式,它主要解决的问题是:在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”的问题。如下图:

有时我们必须向某对象提交请求,但并不知道关于被请求的操作或请求的接受者的任何信息,此时无法抵 御变化的紧耦合是不合适的。如:需要对行为进行“记录、撤销/重做、事务”等处理。我们所要做的是将依赖关系转化,将紧耦合变为松耦合。则上图的形式转化为如下形 式:



      

       Command模式通 过将请求本身变成一个对象来使行为请求者可向未指定的应用对象提出请求。

       GoF《设计模式》中 说道:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。

Command命令模式结构:

      


 

定义场景:

       现在来看一个场景:对 于notepad大 家都很熟悉,在我们使用notepad打开一个文档之后,往往做一些操作,如;输入字符(Write)、删除前一个字符(Delete)、撤销刚才的操作(UnDo)。现在我们就用Console程序模拟这个过程。

原文地址:https://www.cnblogs.com/mikechang/p/1721881.html