• 设计模式之-命令模式(Command Pattern)


    命令模式(Command Pattern)是用来实现在一个请求 - 响应模型松耦合命令模式中,请求被发送给调用者调用它传递给被封装的命令对象 Command对象将请求传递到接收器适当的方法来执行特定操作客户端程序创建接收器对象,然后将其连接到命令然后,它会创建调用对象附加命令对象执行的操作。现在,当客户端程序执行的操作它是基于命令和接收对象的处理。

    我们将着眼于真实的生活场景,我们可以实现Command模式比方说,我们希望提供一个文件系统工具与具体操作文件的打开写入和关闭文件功能,它应该支持多种操作系统,如Windows和Unix

    为了实现我们的文件系统工具首先我们需要创建一个接收器实现以上所有的工作由于我们Java依据接口设计,我们可以有FileSystemReceiver接口和它的实现类实现不同的操作系统,如Windows,UNIX的Solaris


    FileSystemReceiver.java

    package com.journaldev.design.command;
     
    public interface FileSystemReceiver {
     
        void openFile();
        void writeFile();
        void closeFile();
    }

    FileSystemReceiver接口定义了实现类实现规则方法,为了简单,我只创建了windows,linux两个接收器。

    UnixFileSystemReceiver.java

    package com.journaldev.design.command;
     
    public class UnixFileSystemReceiver implements FileSystemReceiver {
     
        @Override
        public void openFile() {
            System.out.println("Opening file in unix OS");
        }
     
        @Override
        public void writeFile() {
            System.out.println("Writing file in unix OS");
        }
     
        @Override
        public void closeFile() {
            System.out.println("Closing file in unix OS");
        }
     
    }

    WindowsFileSystemReceiver.java

    package com.journaldev.design.command;
     
    public class WindowsFileSystemReceiver implements FileSystemReceiver {
     
        @Override
        public void openFile() {
            System.out.println("Opening file in Windows OS");
             
        }
     
        @Override
        public void writeFile() {
            System.out.println("Writing file in Windows OS");
        }
     
        @Override
        public void closeFile() {
            System.out.println("Closing file in Windows OS");
        }
     
    }

    conmand接口设计与实现:

    我们可以使用接口或抽象类实现,具体实现取决于你的需求,这里我仍使用接口实现

    package com.journaldev.design.command;
    
    public interface Command {
    
    	void execute();
    }

    现在,我们需要创建实现所有不同类型的action由接收器进行的因为我们有三个acton,我们将创建三个命令实现,每个命令执行将请求转发到接收器的适当的方法。

    OpenFileCommand.java

    package com.journaldev.design.command;
     
    public class OpenFileCommand implements Command {
     
        private FileSystemReceiver fileSystem;
         
        public OpenFileCommand(FileSystemReceiver fs){
            this.fileSystem=fs;
        }
        @Override
        public void execute() {
            //open command is forwarding request to openFile method
            this.fileSystem.openFile();
        }
     
    }

    CloseFileCommand.java

    package com.journaldev.design.command;
     
    public class CloseFileCommand implements Command {
     
        private FileSystemReceiver fileSystem;
         
        public CloseFileCommand(FileSystemReceiver fs){
            this.fileSystem=fs;
        }
        @Override
        public void execute() {
            this.fileSystem.closeFile();
        }
     
    }

    WriteFileCommand.java

    package com.journaldev.design.command;
     
    public class WriteFileCommand implements Command {
     
        private FileSystemReceiver fileSystem;
         
        public WriteFileCommand(FileSystemReceiver fs){
            this.fileSystem=fs;
        }
        @Override
        public void execute() {
            this.fileSystem.writeFile();
        }
     
    }

    现在接收器与Conmand实现已完成,下面实现调用类。

    调用是封装命令请求传递给命令对象来处理它一个简单的类

    FileInvoker.java

    package com.journaldev.design.command;
     
    public class FileInvoker {
     
        public Command command;
         
        public FileInvoker(Command c){
            this.command=c;
        }
         
        public void execute(){
            this.command.execute();
        }
    }

    FileSystemReceiverUtil.java

    package com.journaldev.design.command;
     
    public class FileSystemReceiverUtil {
         
        public static FileSystemReceiver getUnderlyingFileSystem(){
             String osName = System.getProperty("os.name");
             System.out.println("Underlying OS is:"+osName);
             if(osName.contains("Windows")){
                 return new WindowsFileSystemReceiver();
             }else{
                 return new UnixFileSystemReceiver();
             }
        }
         
    }

    下面创建客户端类使用我们的文件系统Util

    FileSystemClient.java

    package com.journaldev.design.command;
     
    public class FileSystemClient {
     
        public static void main(String[] args) {
            //Creating the receiver object
            FileSystemReceiver fs = FileSystemReceiverUtil.getUnderlyingFileSystem();
             
            //creating command and associating with receiver
            OpenFileCommand openFileCommand = new OpenFileCommand(fs);
             
            //Creating invoker and associating with Command
            FileInvoker file = new FileInvoker(openFileCommand);
             
            //perform action on invoker object
            file.execute();
             
            WriteFileCommand writeFileCommand = new WriteFileCommand(fs);
            file = new FileInvoker(writeFileCommand);
            file.execute();
             
            CloseFileCommand closeFileCommand = new CloseFileCommand(fs);
            file = new FileInvoker(closeFileCommand);
            file.execute();
        }
     
    }

    程序输出:

    Underlying OS is:Mac OS X
    Opening file in unix OS
    Writing file in unix OS
    Closing file in unix OS

    类结构视图:



    要点总结:

    命令这种模式,它定义了规则执行核心
    接收机的实现是由分开的命令执行
    命令实现类选择的方法来调用接收对象,每个方法接收器会有一个命令执行它的工作原理接收器和行动方法之间的桥梁。
    调用客户端获取请求,将命令对象转发
    客户端负责实例化相应的命令接收执行,然后将它们联系到一起
    客户端还负责调用实例化对象,并与之关联的命令对象,并执行该操作方法。
    Command模式很容易扩展的,我们可以在接收器中添加新的操作方法,并创建新的命令的实现在不改变客户端代码。
    command模式的缺点是代码量是巨大的,因为这么多的关联和混乱大量的动作方法







  • 相关阅读:
    6-4.粗体标签
    [Unity3D] 如何实现点击按钮退出游戏
    [Unity3D] 载入游戏地图时背景图片随机切换 & 数字百分比进度条
    [Unity3D] 鼠标点击图片移动效果
    [3DMAX]如何将骨骼与模型绑定在一起(蒙皮) & 如何实现自动化人物模型蒙皮
    [Unity 3D]用鼠标滚轮实现镜头放大和缩放,并添加距离限制
    [Unity3D] 如何实现围绕旋转
    [Unity3D] 如何实现注视旋转
    Css 图片自适应
    Scss 定义内层class的简单写法
  • 原文地址:https://www.cnblogs.com/happyxiaoyu02/p/6818938.html
Copyright © 2020-2023  润新知