C#设计模式(15)命令模式(Command Pattern)

命令模式(Command Pattern)

命令模式是一种数据驱动的设计模式,属于行为型模式类别。请求被包装在一个对象中作为命令,并传递给调用对象。调用对象寻找可以处理该命令的合适对象,并将命令传递给相应的对象,该对象执行命令。

实现
  1. 步骤 1:创建一个命令接口。

    public interface IOrder {
      void Execute();
    }
    
    
  2. 步骤 2:创建一个请求类。

public class Stock {
  private string name = "ABC";
  private int quantity = 10;
  public void Buy(){
    Console.WriteLine($"Stock [ Name: {name}, Quantity: {quantity} ] bought");
  }
  public void Sell(){
    Console.WriteLine($"Stock [ Name: {name}, Quantity: {quantity} ] sold");
  }
}

  1. 步骤 3:创建实现 IOrder 接口的具体类。
public class BuyStock : IOrder {
  private Stock abcStock;
  public BuyStock(Stock abcStock){
    this.abcStock = abcStock;
  }
  public void Execute() {
    abcStock.Buy();
  }
}

public class SellStock : IOrder {
  private Stock abcStock;
  public SellStock(Stock abcStock){
    this.abcStock = abcStock;
  }
  public void Execute() {
    abcStock.Sell();
  }
}

  1. 步骤 4:创建命令调用类。
public class Broker {
  private List<IOrder> orderList = new List<IOrder>(); 
  public void TakeOrder(IOrder order){
    orderList.Add(order);    
  }
  public void PlaceOrders(){
    foreach (IOrder order in orderList) {
      order.Execute();
    }
    orderList.Clear();
  }
}

  1. 步骤 5:使用 Broker 类接受并执行命令。
public class CommandPatternDemo {
  public static void Main(string[] args) {
    Stock abcStock = new Stock();
    BuyStock buyStockOrder = new BuyStock(abcStock);
    SellStock sellStockOrder = new SellStock(abcStock);
    Broker broker = new Broker();
    broker.TakeOrder(buyStockOrder);
    broker.TakeOrder(sellStockOrder);
    broker.PlaceOrders();
  }
}

  1. 步骤 6:验证输出。
Stock [ Name: ABC, Quantity: 10 ] bought
Stock [ Name: ABC, Quantity: 10 ] sold

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern) 17. 解释器模式(Interpreter Pattern) 18. 中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern) 工程结构 ├─01.Singleton │ ├─html │ └─MySingleton │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─02.ChainOfResponsibility │ ├─html │ ├─My2ChainOfResponsibility │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─MyChainOfResponsibility │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ ├─Refactor │ │ └─TempPE │ └─Properties ├─03.FactoryMethodMode │ ├─FactoryMethodMode │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─04.AbstractFactory │ ├─04.1.SimpleFactory │ │ ├─html │ │ └─SimpleFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─AbstractFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─05.BuilderPattern │ ├─html │ └─MyBuilderPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─06.PrototypePattern │ ├─html │ │ └─C#设计模式(6)——原型模式(Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.AdapterPattern │ ├─html │ └─MyAdapterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─08.BridgePattern │ ├─html │ └─MyBridgePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─09.DecoratorPattern │ ├─html │ └─MyDecoratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─10.CompositePattern │ ├─html │ └─MyCompositePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─11.FacadePattern │ ├─html │ └─MyFacadePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─12.FlyweightPattern │ ├─html │ └─MyFlyweightPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─13.ProxyPattern │ ├─html │ └─MyProxyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─14.TemplateMethod │ ├─html │ └─MyTemplateMethod │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─15.VisitorPattern │ ├─html │ └─MyVisitorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─16.StrategyPattern │ ├─html │ └─MyStrategyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─17.StatePattern │ ├─html │ └─StatePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─18.MementoPattern │ ├─html │ └─MementoPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─19.MediatorPattern │ ├─html │ └─MediatorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─20.OberverPattern │ ├─CatOberverPattern │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─html │ └─MyOberverPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─21.IteratorPattern │ ├─html │ └─IteratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─22.InterpreterPattern │ ├─html │ └─MyInterpreterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties └─23.CommandPattern ├─html └─MyCommandPattern ├─bin │ └─Debug ├─obj │ └─Debug │ └─TempPE └─Properties
C#设计模式(1) 一、 C# 面向对象程序设计复习 二、 设计模式举例 三、 先有鸡还是先有蛋? 四、 大瓶子套小瓶子还是小瓶子套大瓶子? 五、 .net本质 C#设计模式(2) 一、 "开放-封闭"原则(OCP) 二、 里氏代换原则(LSP) C#设计模式(3) 三、 依赖倒置原则(DIP) 四、 接口隔离原则(ISP) 五、 合成/聚合复用原则(CARP) 六、 迪米特法则(LoD) C#设计模式(4)-Simple Factory Pattern 一、 简单工厂(Simple Factory)模式 二、 Simple Factory模式角色与结构: 三、 程序举例: 四、 Simple Factory模式演化 五、 优点与缺点: C#设计模式(5)-Factory Method Pattern 一、 工厂方法(Factory Method)模式 二、 Factory Method模式角色与结构: 三、 程序举例: 四、 工厂方法模式与简单工厂模式 五、 Factory Method模式演化 六、 Factory Method模式与其它模式的关系 七、 另外一个例子 C#设计模式(6)-Abstract Factory Pattern 一、 抽象工厂(Abstract Factory)模式 二、 Abstract Factory模式的结构: 三、 程序举例: 四、 在什么情形下使用抽象工厂模式: 五、 抽象工厂的起源 六、 Abstract Factory模式在实际系统中的实现 七、 "开放-封闭"原则 C#设计模式(7)-Singleton Pattern 一、 单例(Singleton)模式 二、 Singleton模式的结构: 三、 程序举例: 四、 在什么情形下使用单例模式: 五、 Singleton模式在实际系统中的实现 六、 C#中的Singleton模式 C#设计模式(8)-Builder Pattern 一、 建造者(Builder)模式 二、 Builder模式的结构: 三、 程序举例: 四、 建造者模式的活动序列: 五、 建造者模式的实现: 六、 建造者模式的演化 七、 在什么情况下使用建造者模式 C#设计模式(9)-Prototype Pattern 一、 原型(Prototype)模式 二、 Prototype模式的结构: 三、 程序举例: 四、 带Prototype Manager的原型模式 五、 浅拷贝与深拷贝 六、 Prototype模式的优点与缺点 C#设计模式(10)-Adapter Pattern 一、 适配器(Adapter)模式 二、 类的Adapter模式的结构: 三、 类的Adapter模式示意性实现: 四、 对象的Adapter模式的结构: 五、 对象的Adapter模式示意性实现: 六、 在什么情况下使用适配器模式 七、 一个实际应用Adapter模式的例子 八、 关于Adapter模式的讨论 C#设计模式(11)-Composite Pattern 一、 合成(Composite)模式 二、 合成模式概述 三、 安全式的合成模式的结构 四、 安全式的合成模式实现 五、 透明式的合成模式结构 六、 透明式的合成模式实现 七、 使用合成模式时考虑的几个问题 八、 和尚的故事 九、 一个实际应用Composite模式的例子 C#设计模式(12)-Decorator Pattern 一、 装饰(Decorator)模式 二、 装饰模式的结构 三、 装饰模式示例性代码 四、 装饰模式应当在什么情况下使用 五、 装饰模式实际应用的例子 六、 使用装饰模式的优点和缺点 七、 模式实现的讨论 八、 透明性的要求 九、 装饰模式在.NET中的应用 C#设计模式(13)-Proxy Pattern 一、 代理(Proxy)模式 二、 代理的种类 三、 远程代理的例子 四、 代理模式的结构 五、 代理模式示例性代码 六、 高老庄悟空降八戒 七、 不同类型的代理模式 八、 代理模式实际应用的例子 设计模式(14)-Flyweight Pattern 一、 享元(Flyweight)模式 二、 单纯享元模式的结构 三、 单纯享元模式的示意性源代码 四、 复合享元模式的结构 五、 一个咖啡摊的例子 六、 咖啡屋的例子 七、 享元模式应当在什么情况下使用 八、 享元模式的优点和缺点 设计模式15)-Facade Pattern 一、 门面(Facade)模式 二、 门面模式的结构 三、 门面模式的实现 四、 在什么情况下使用门面模式 五、 一个例子 六、 使用门面模式的设计 设计模式(16)-Bridge Pattern 一、 桥梁(Bridge)模式 二、 桥梁模式的结构 三、 桥梁模式的示意性源代码 四、 调制解调器问题 五、 另外一个实际应用Bridge模式的例子 六、 在什么情况下应当使用桥梁模式 设计模式(17)-Chain of Responsibility Pattern 一、 职责链(Chain of Responsibility)模式 二、 责任链模式的结构 三、 责任链模式的示意性源代码 四、 纯的与不纯的责任链模式 五、 责任链模式的实际应用案例 六、 责任链模式的实现 设计模式(18)-Command Pattern 一、 命令Command模式 二、 命令模式的结构 三、 命令模式的示意性源代码 四、 玉帝传美猴王上天 五、 命令模式的实现 六、 命令模式的实际应用案例 七、 在什么情况下应当使用命令模式 八、 使用命令模式的优点和缺点 设计模式(19)-Observer Pattern 一、 观察者(Observer)模式 二、 观察者模式的结构 三、 观察者模式的示意性源代码 四、 C#中的Delegate与Event 五、 一个实际应用观察者模式的例子 六、 观察者模式的优缺点 设计模式(20)-Visitor Pattern 一、 访问者(Visitor)模式 二、 访问者模式的结构 三、 示意性源代码 四、 一个实际应用Visitor模式的例子 五、 在什么情况下应当使用访问者模式 六、 使用访问者模式的优点和缺点 设计模式(21)-Template Method Pattern 一、 模板方法(Template Method)模式 二、 模版方法模式的结构 三、 模板方法模式的示意性代码 四、 继承作为复用的工具 五、 一个实际应用模板方法的例子 六、 模版方法模式中的方法 七、 重构的原则 设计模式(22)-Strategy Pattern 一、 策略(Strategy)模式 二、 策略模式的结构 三、 示意性源代码 四、 何时使用何种具体策略角色 五、 一个实际应用策略模式的例子 六、 在什么情况下应当使用策略模式 七、 策略模式的优点和缺点 八、 其它
原型模式是一种创建型设计模式,它允许你通过复制一个已经存在的对象来创建新的对象,而不是通过实例化来创建。这种方式可以大大减少对象的创建时间和内存消耗。 在C#中,原型模式的实现需要满足两个条件: 1. 实现ICloneable接口,该接口只有一个方法Clone(),用于复制对象。 2. 对象必须是可复制的,即必须是浅复制或深复制。 浅复制是指复制一个对象,但是不复制对象中的引用类型成员变量,这意味着复制的对象和原始对象共享相同的引用类型成员变量。深复制则是复制一个对象及其引用类型成员变量,这意味着复制的对象和原始对象不共享相同的引用类型成员变量。 下面是一个使用原型模式的示例代码: ```csharp using System; namespace PrototypePattern { // 实现ICloneable接口 public class Person : ICloneable { public string Name { get; set; } public int Age { get; set; } public Address Address { get; set; } public object Clone() { // 深复制 return new Person { Name = this.Name, Age = this.Age, Address = new Address { Street = this.Address.Street, City = this.Address.City, State = this.Address.State } }; } public override string ToString() { return $"Name: {Name}, Age: {Age}, Address: {Address}"; } } public class Address { public string Street { get; set; } public string City { get; set; } public string State { get; set; } public override string ToString() { return $"{Street}, {City}, {State}"; } } class Program { static void Main(string[] args) { var person1 = new Person { Name = "Tom", Age = 20, Address = new Address { Street = "123 Main St", City = "New York", State = "NY" } }; // 使用Clone方法复制对象 var person2 = (Person)person1.Clone(); person2.Name = "Jerry"; person2.Address.Street = "456 Elm St"; Console.WriteLine(person1); Console.WriteLine(person2); } } } ``` 输出结果: ``` Name: Tom, Age: 20, Address: 123 Main St, New York, NY Name: Jerry, Age: 20, Address: 456 Elm St, New York, NY ``` 可以看到,使用原型模式可以方便地创建新的对象,而且不需要关心对象的创建过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金士顿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值