• 命令模式(2)-命令接口中的撤销方法


    通过封装请求的execute()方法以及撤销请求的undo()方法:

    接受者:
    package DesignPatterns.CommandMode1;
    
    import java.lang.reflect.Array;
    import java.util.ArrayList;
    
    public class StringReceiver {
        public void printString(ArrayList<String> arrayList)
        {
            System.out.println("输出后");
            for (int i=0;i<arrayList.size();i++)
            {
                System.out.println(arrayList.get(i));
            }
        }
        public void cancelString(ArrayList<String> arrayList)
        {
            System.out.println("取消输出后");
            for (int i=0;i<arrayList.size();i++)
            {
                System.out.println(arrayList.get(i));
            }
    
        }
    }
    
    命令接口:
    
    package DesignPatterns.CommandMode1;
    
    import java.util.ArrayList;
    
    public interface StringCommand {
        void execute(String name);
        void undo(String name);
    }
    
    
    具体命令:
    
    package DesignPatterns.CommandMode1;
    
    import java.util.ArrayList;
    
    public class StringConcreteCommand  implements StringCommand{
        ArrayList<String> arrayList;
        StringReceiver stringReceiver;
        public StringConcreteCommand(StringReceiver stringReceiver)
        {
            this.arrayList=new ArrayList<>();
            this.stringReceiver=stringReceiver;
        }
        @Override
        public void execute(String name) {
            arrayList.add(name);
            stringReceiver.printString(arrayList);
        }
    
        @Override
        public void undo(String name) {
            arrayList.remove(name);
            stringReceiver.cancelString(arrayList);
    
        }
    }
    
    
    请求者:
    
    package DesignPatterns.CommandMode1;
    
    import DesignPatterns.CommandMode.Command;
    
    public class StringInvoker {
        StringCommand stringCommand;
        String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setStringCommand(StringCommand stringCommand)
        {
            this.stringCommand=stringCommand;
        }
        public void executeStringCommand()
        {
            stringCommand.execute(name);
        }
        public void undoStringCommand()
        {
            stringCommand.undo(name);
        }
    }
    
    
    测试类:
    
    package DesignPatterns.CommandMode1;
    
    public class Application {
        public static void main(String[] args) {
            StringReceiver stringReceiver=new StringReceiver();
            StringCommand stringCommand=new StringConcreteCommand(stringReceiver);
            StringInvoker stringInvoker=new StringInvoker();
            stringInvoker.setStringCommand(stringCommand);
            stringInvoker.setName("xujinfeng");
            stringInvoker.executeStringCommand();
            stringInvoker.undoStringCommand();
        }
    }

  • 相关阅读:
    删除docker thin 空间解决文件满不能拉起docker问题
    kafka服务端和客户端均无法消费
    prometheus 配置支持consul动态拉取
    spring cloud consul配置
    使用arthas分析慢查询
    nginx日志格式配置
    spring secrity添加和去掉x-frame-options deny安全头
    java POI解析word为文本内容
    sublime text 3 插入当前时间
    CUnit 安装笔记
  • 原文地址:https://www.cnblogs.com/dloading/p/10753200.html
Copyright © 2020-2023  润新知