zoukankan      html  css  js  c++  java
  • command模式

    运用<<解耦的思维方法>>
    对照Command设计模式
    耦合的例子1

    public class Application
     {
      public Application()
      {
       Document doc=new Document();
       doc.ShowText();//输出文本

       Graphic grh= new Graphic();
       grh.DrawGraphic();//画图形
      }
     }


     public class Document
     {
      public void ShowText()
      {
      }
     }

     public class Graphic
     {
      public void DrawGraphic()
      {
      }
     }
    解耦后过程例子

    public class Application1
     {
      System.Collections.ArrayList list;
      public Application1()
      {
       foreach(ICommand cmd in list)
       {
        cmd.ExecuteCommand();
       }
      }
     }
     public class Document
     {
      public void ShowText()
      {
      }
     }

     public class Graphic
     {
      public void DrawGraphic()
      {
      }
     }

    //保持原有程序不变
     //把对象的行为抽象出来ICommand
     //把Document.ShowText()行为和Graphic.DrawGraphi()分别作为一个命令从ICommand行为对象继承
     //把(对象A======对象B的行为)依赖关系转化为(对象A=====抽象行为)

    public interface ICommand
     {
      void ExecuteCommand();
      void ReDo();
      void UnDo();
     }

     public class DocumentCommand:ICommand
     {
      private Document doc;
      public void ExecuteCommand()
      {
       this.doc.ShowText();
      }

      public void ReDo()
      {
      }

      public void UnDo()
      {
      }
     }
     public class GraphicCommand:ICommand
     {
      private Graphic graphic;
      public void ExecuteCommand()
      {
       this.graphic.DrawGraphic();
      }
      
      public void ReDo()
      {
       
      }

      public void UnDo()
      {
       
      }
     }

  • 相关阅读:
    "科林明伦杯"哈理工第九届——分布式服务(概率期望+思维)
    Nim游戏——简单博弈论
    acwing 1252搭配购买(并查集 + 01背包 )
    [LeetCode] 67. Add Binary
    [LeetCode] 415. Add Strings
    [LeetCode] 43. Multiply Strings
    [LeetCode] 412. Fizz Buzz
    [LeetCode] 201. Bitwise AND of Numbers Range
    [LeetCode] 389. Find the Difference
    [LeetCode] 326. Power of Three + 342. Power of Four
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/1200570.html
Copyright © 2011-2022 走看看