网站如何做ins链接分享,网站网上预定功能怎么做,网站系统里不能打印,江西网站建设价位命令模式#xff08;Command Pattern#xff09;是用来实现在一个请求 - 响应模型松耦合。在命令模式中#xff0c;请求被发送给调用者和调用它传递给被封装的命令对象。 Command对象将请求传递到接收器的适当的方法来执行特定操作。客户端程序创建接收器对象#xff0c;然… 命令模式Command Pattern是用来实现在一个请求 - 响应模型松耦合。在命令模式中请求被发送给调用者和调用它传递给被封装的命令对象。 Command对象将请求传递到接收器的适当的方法来执行特定操作。客户端程序创建接收器对象然后将其连接到命令。然后它会创建调用对象和附加命令对象执行的操作。现在当客户端程序执行的操作它是基于命令和接收对象的处理。 我们将着眼于真实的生活场景我们可以实现Command模式。比方说我们希望提供一个文件系统工具与具体操作文件的打开写入和关闭文件功能它应该支持多种操作系统如Windows和Unix。 为了实现我们的文件系统工具首先我们需要创建一个接收器类实现以上所有的工作。由于我们在Java依据接口设计我们可以有FileSystemReceiver接口和它的实现类实现不同的操作系统如WindowsUNIX的Solaris等 FileSystemReceiver.java package com.journaldev.design.command;public interface FileSystemReceiver {void openFile();void writeFile();void closeFile();
} FileSystemReceiver接口定义了实现类实现规则方法为了简单我只创建了windowslinux两个接收器。 UnixFileSystemReceiver.java package com.journaldev.design.command;public class UnixFileSystemReceiver implements FileSystemReceiver {Overridepublic void openFile() {System.out.println(Opening file in unix OS);}Overridepublic void writeFile() {System.out.println(Writing file in unix OS);}Overridepublic void closeFile() {System.out.println(Closing file in unix OS);}} WindowsFileSystemReceiver.java package com.journaldev.design.command;public class WindowsFileSystemReceiver implements FileSystemReceiver {Overridepublic void openFile() {System.out.println(Opening file in Windows OS);}Overridepublic void writeFile() {System.out.println(Writing file in Windows OS);}Overridepublic 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.fileSystemfs;}Overridepublic void execute() {//open command is forwarding request to openFile methodthis.fileSystem.openFile();}} CloseFileCommand.java package com.journaldev.design.command;public class CloseFileCommand implements Command {private FileSystemReceiver fileSystem;public CloseFileCommand(FileSystemReceiver fs){this.fileSystemfs;}Overridepublic 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.fileSystemfs;}Overridepublic void execute() {this.fileSystem.writeFile();}} 现在接收器与Conmand实现已完成下面实现调用类。 调用是封装的命令和请求传递给命令对象来处理它一个简单的类。 FileInvoker.java package com.journaldev.design.command;public class FileInvoker {public Command command;public FileInvoker(Command c){this.commandc;}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 objectFileSystemReceiver fs FileSystemReceiverUtil.getUnderlyingFileSystem();//creating command and associating with receiverOpenFileCommand openFileCommand new OpenFileCommand(fs);//Creating invoker and associating with CommandFileInvoker file new FileInvoker(openFileCommand);//perform action on invoker objectfile.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模式的缺点是代码量是巨大的因为这么多的关联和混乱有大量的动作和方法。 转载于:https://www.cnblogs.com/happyxiaoyu02/p/6818938.html