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

推荐订阅源

月光博客
月光博客
博客园_首页
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
量子位
H
Help Net Security
D
Docker
小众软件
小众软件
Google DeepMind News
Google DeepMind News
U
Unit 42
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
S
SegmentFault 最新的问题
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
Martin Fowler
Martin Fowler
D
DataBreaches.Net
AI
AI
SecWiki News
SecWiki News
V
Visual Studio Blog
Google Online Security Blog
Google Online Security Blog
腾讯CDC
J
Java Code Geeks
Jina AI
Jina AI
O
OpenAI News
N
News | PayPal Newsroom
Help Net Security
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cloudbric
Cloudbric
S
Secure Thoughts
V
V2EX
N
News and Events Feed by Topic
F
Full Disclosure
MyScale Blog
MyScale Blog
The Cloudflare Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Forbes - Security
Forbes - Security
T
Troy Hunt's Blog
WordPress大学
WordPress大学
H
Hacker News: Front Page
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - Cameo

解决VSTO项目重定向到.NET Framework 4的编译和运行时错误 Excel 应用程序如何创建数据透视表 值类型与引用类型 Visual studio tools for office体系结构 C#类型转换 VSTO开发团队成立 14. Template Method模板方法(行为型模式) 13. Proxy代理(结构型模式) 12. Flyweight享元(结构型模式) 11. Facade外观[门面模式](结构型模式) 10. Decorator 装饰(结构型模式) 9. Composite 组合(结构型模式) 8. Bridge 桥接(结构型模式) 7. Adapter 适配器(结构型模式) 6. Prototype 原型(创建型模式) 5. Factory Method 工厂方法(创建型模式的基础) 4.Builder 建造者(生成器)(创建型模式) 书籍收藏 3. Abstract Factory 抽象工厂(创建型模式)
15. Command 命令(行为型模式)
Cameo · 2008-08-01 · via 博客园 - Cameo

耦合与变化
     耦合是软件不能抵御变化灾难的根本性原因。不仅实体对象与实体对象之间存在耦合关系,实体对象与行为操作之间也存在耦合关系。

动机(Motivation)
     在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合——比如需要对行为进行“记录、撤销/重做(undo/redo)、
事务”等处理,这种无法抵御变化的紧耦合是不合适的。
     在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,可以实现二者之间的松耦合

意图(Intent)
     将一个请求封装为一个对象,从而使你可用不同的请求对客户[行为请求者]进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
                                                                                                            ——《设计模式》GoF

 结构

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Command
{

    
Command 抽象接口

    
//表示一个行为
    ConcreteCommand

    
Receiver 低层模块

    
Client Application  高层模块
}

Command模式的几个要点
     • Command模式的根本目的在于将“行为请求者”与“行为实现者” 解耦,在面向对象语言中,常见的实现手段是“将行为抽象为对象”。
     • 实现Command接口的具体命令对象ConcreteCommand有时候根据需要可能会保存一些额外的状态信息
     • 通过使用Composite模式,可以将多个“命令”封装为一个“复合命令”MacroCommand
     • Command模式与C#中的Delegate有些类似。但两者定义行为接口的规范有所区别:Command以面向对象中的“接口-实现”来定义行为接口规范更严格,更符合抽象原则;Delegate以函数签名来定义行为接口规范,更灵活,但抽象能力比较弱。