命令(Command)

2017-06-26  本文已影响0人  非空白

意图

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

结构

命令模式结构图

以及它们之间的交互关系:

命令模式协作图

动机

有时必须向某个对象提交请求,但并不知道被请求的操作或请求的接收者的任何信息。命令模式通过将请求本身变成一个对象使得可以向未指定的应用对象提出请求。这个对象可以被存储并像其他对象一样被传递。

适用性

优点



示例

模拟一个图形用户界面的菜单栏,它们执行请求响应用户输入(打开文档、黏贴等操作)。

实现(C#)

命令模式示例结构图
using System;
using System.Collections.Generic;

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

// 文档粘贴的命令
public class PasteCommand : Command
{
    private Document document;

    public PasteCommand(Document document)
    {
        this.document = document;
    }

    public override void Execute()
    {
        this.document.Paste();
    }
}

// 打开文档的命令
public class OpenCommand : Command
{
    private Application application;

    public OpenCommand(Application application)
    {
        this.application = application;
    }

    public override void Execute()
    {
        string name = AskUser();
        Document document = new Document(name);
        this.application.Add(document);
        document.Open();
    }

    private string AskUser()
    {
        return "New Document";
    }
}

// 应用程序
public class Application
{
    private List<Document> documents = new List<Document>();
    private Menu menu = new Menu();


    public void Add(Document document)
    {
        this.documents.Add(document);
    }

    public Menu Menu { get { return this.menu; } }
    public List<Document> Documents { get { return this.documents; } }
}

// 文档
public class Document
{
    private string name;

    public Document(string name)
    {
        this.name = name;
    }

    public void Open()
    {
        Console.WriteLine("打开「{0}」文档", this.name);
    }

    public void Close()
    {
        Console.WriteLine("关闭「{0}」文档", this.name);
    }

    public void Cut()
    {
        Console.WriteLine("剪贴「{0}」文档", this.name);
    }

    public void Paste()
    {
        Console.WriteLine("粘贴「{0}」文档", this.name);
    }

    public string Name { get { return this.name; } }
}

// 命令菜单
public class Menu
{
    private Dictionary<string,MenuItem> items = new Dictionary<string, MenuItem>();

    public void Add(MenuItem item)
    {
        this.items.Add(item.Name, item);
    }

    public MenuItem this[string name]
    {
        get { return this.items[name]; }
    }

}


// 命令菜单项
public class MenuItem
{
    private Command command;
    private string name;

    public MenuItem(string name)
    {
        this.name = name;
    }

    public void SetCommand(Command command)
    {
        this.command = command;
    }

    public void Clicked()
    {
        this.command.Execute();
    }

    public string Name { get { return this.name; } }
}


// 测试应用程序
public class App
{
    public static void Main(string[] args)
    {
        Application application = new Application();

        application.Menu.Add(new MenuItem("Open"));
        application.Menu.Add(new MenuItem("Paste"));

        // 模拟触发"Open"菜单事件,将Open命令传送给Open菜单项
        application.Menu["Open"].SetCommand(new OpenCommand(application));
        application.Menu["Open"].Clicked();

        // 模拟触发"Paste"菜单事件,将Paste命令传送给Paste菜单项
        application.Menu["Paste"].SetCommand(new PasteCommand(application.Documents[0]));
        application.Menu["Paste"].Clicked();
    }
}

// 控制台输出:
//  打开「New Document」文档
//  粘贴「New Document」文档
上一篇下一篇

猜你喜欢

热点阅读