zoukankan      html  css  js  c++  java
  • 命令模式

    代码
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class Document
    {
        
    public string strContent;
        
    public Document()
        {
            strContent
    ="";
        }
    }

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

    public class WriteCommand:Command
    {
        Document doc;
        ArrayList ObjectState;
        
    public WriteCommand(Document doc,ArrayList state)
        {
            
    this.doc=doc;
            
    this.ObjectState=state;
        }
        
    public override void Excute()
        {
            doc.strContent
    +=Console.ReadLine();
            ObjectState.Add(doc,strContent);
        }
    }

    public class DeleteCommand:Command
    {
        Document doc;
        ArrayList ObjectState;
        
    public DeleteCommand(Document doc,ArrayList state)
        {
            
    this.doc=doc;
            
    this.ObjectState=state;
        }
        
    public override void Excute()
        {
            doc.strContent
    =doc.strContent.Substring(0,doc.strContent.Length-1);
            ObjectState.Add(doc,strContent);
        }
    }

    public class UnDoCommand:Command
    {
        Document doc;
        ArrayList ObjectState;
        
    public UnDoCommand(Document doc,ArrayList state)
        {
            
    this.doc=doc;
            
    this.ObjectState=state;
        }
        
    public override void Excute()
        {
            doc.strContent
    =(string)ObjectState[ObjectState.Count-2];
            ObjectState.Add(doc,strContent);
        }
    }

    public class MyClass
    {
        
    public static void Main()
        {
            Document doc
    =new Document();
            Console.WriteLine(
    "Please input Command:");
            
    string strOperation=Console.ReadLine();
            Command com
    =null;
            ArrayList mylist
    =new ArrayList();
            
    while(strOperation!="Exit")
            {
                
    switch(strOperation.ToLower())
                {
                    
    case "write":
                        com
    =new WriteCommand(doc,mylist);
                        com.Excute();
                        Console.WriteLine(
    "Write Operation:"+doc.strContent);
                        
    break;
                    
    case "del":
                        com
    =new DeleteCommand(doc,mylist);
                        com.Excute();
                        Console.WriteLine(
    "Delete Operation:"+doc.strContent);
                        
    break;
                    
    case "undo":
                        com
    =new UnDoCommand(doc,mylist);
                        com.Excute();
                        Console.WriteLine(
    "Undo Operation:"+doc.strContent);
                        
    break;
                    
    default:
                        Console.WriteLine(
    "Wrong Operation");
                        
    break;
                }
                Console.WriteLine(
    "Please input next Operation!!");
                strOperation
    =Console.ReadLine();
            }
        }
    }

    Command命令模式介绍:

    Command命令模式是一种对象行为型模 式,它主要解决的问题是:在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”的问题。如下图:

    有时我们必须向某对象提交请求,但并不知道关于被请求的操作或请求的接受者的任何信息,此时无法抵 御变化的紧耦合是不合适的。如:需要对行为进行“记录、撤销/重做、事务”等处理。我们所要做的是将依赖关系转化,将紧耦合变为松耦合。则上图的形式转化为如下形 式:



          

           Command模式通 过将请求本身变成一个对象来使行为请求者可向未指定的应用对象提出请求。

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

    Command命令模式结构:

          


     

    定义场景:

           现在来看一个场景:对 于notepad大 家都很熟悉,在我们使用notepad打开一个文档之后,往往做一些操作,如;输入字符(Write)、删除前一个字符(Delete)、撤销刚才的操作(UnDo)。现在我们就用Console程序模拟这个过程。

  • 相关阅读:
    实例属性 类属性 实例域 类域
    研究数据集
    static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符
    accessor mothod mutator mothod 更改器方法 访问器方法 类的方法可以访问类的任何一个对象的私有域!
    上钻 下钻 切片 转轴 降采样
    识别会话
    Performance Tuning Using Linux Process Management Commands
    Secure Hash Algorithm 3
    grouped differently across partitions
    spark 划分stage Wide vs Narrow Dependencies 窄依赖 宽依赖 解析 作业 job stage 阶段 RDD有向无环图拆分 任务 Task 网络传输和计算开销 任务集 taskset
  • 原文地址:https://www.cnblogs.com/mikechang/p/1721881.html
Copyright © 2011-2022 走看看