惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Vercel News
Vercel News
月光博客
月光博客
S
SegmentFault 最新的问题
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
小众软件
小众软件
WordPress大学
WordPress大学
G
Google Developers Blog
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 【当耐特】
I
InfoQ

博客园 - Charly

我的电影记录 再去听他的演唱会 学友12月29日广州演唱会 张学友十大粤语&十大国语金曲赏析 妈妈的第二次重生 随便谈谈最近参与的2个项目 中奖的幸运与不幸 设计模式学习笔记二十五——总结 Moss2007搜索服务配置,没有索引器和搜索配置页面报错问题解决 设计模式学习笔记二十四——Visitor模式 设计模式学习笔记二十三——TemplateMethod模式 设计模式学习笔记二十二——Strategy模式 设计模式学习笔记二十一——State模式 设计模式学习笔记二十——Memento模式 设计模式学习笔记十九——Observer模式 在MOSS 2007中调试WebPart 设计模式学习笔记十八——Mediator模式 设计模式学习笔记十七——Iterator模式 设计模式学习笔记十六——Interpreter模式
设计模式学习笔记十五——Command模式
Charly · 2007-07-11 · via 博客园 - Charly

Command模式结构图

代码实现

/*
 * 行为实现者
*/

namespace DesignPattern.Command
{
    
public class Application
    
{
        
public void OpenDocument()
        
{
        }

    }

}

/*
 * 行为实现者
*/

namespace DesignPattern.Command
{
    
public class Document
    
{
        
string path;
        
public Document(string docPath)
        
{
            path 
= docPath;
        }


        
public void Copy()
        
{
        }


        
public void Cut()
        
{
        }


        
public void Paste()
        
{
        }

    }

}

/*
 * 行为对象
*/

namespace DesignPattern.Command
{
    
// 定义行为接口
    public interface ICommand
    
{
        
void Execute();
    }


    
// 打开文档
    public class OpenCommand : ICommand
    
{
        
// 使用应用程序打开文档
        Application app;

        
public OpenCommand(Application application)
        
{
            app 
= application;
        }


        
public void Execute()
        
{
            app.OpenDocument();
        }

    }


    
public class CopyCommand : ICommand
    
{
        Document doc;

        
public CopyCommand(Document document)
        
{
            doc 
= document;
        }


        
public void Execute()
        
{
            doc.Copy();
        }

    }


    
public class CutCommand : ICommand
    
{
        Document doc;

        
public CutCommand(Document document)
        
{
            doc 
= document;
        }


        
public void Execute()
        
{
            doc.Cut();
        }

    }


    
public class PasteCommand : ICommand
    
{
        Document doc;

        
public PasteCommand(Document document)
        
{
            doc 
= document;
        }


        
public void Execute()
        
{
            doc.Paste();
        }

    }

}

/*
 * 行为请求者
*/

namespace DesignPattern.Command
{
    
public class Client
    
{
        
public void DealDocument()
        
{
            Document doc 
= new Document("DocumentPath");
            Command command
= new CopyCommand(doc);
            command .Execute();
        }

    }

}

要点
      1、本模式的基本目的在于将行为请求者和实现者解耦,常见的实现方法是将行为抽象为对象。
      2、实现行为的具体对象有时候根据需要可能会保存一些额外的状态信息,比如实现撤销、重做时。
      3、可以和Composite模式结合,实现多个命令的组合成复杂命令。
      4、本模式和Delegate有些类似。但两者定义行为接口的规范有所区别:本模式以面向对象中“接口-实现”来定义行为接口规范,更严格,更符合抽象原语;Delegate以函数签名来定义行为接口规范,更灵活,但抽象能力比较弱。