设计模式之-命令模式(Command Pattern)
命令模式(Command Pattern)是用來實現在一個請求 - 響應模型松耦合。在命令模式中,請求被發送給調用者和調用它傳遞給被封裝的命令對象。 Command對象將請求傳遞到接收器的適當的方法來執行特定操作??蛻舳顺绦騽摻ń邮掌鲗ο?#xff0c;然后將其連接到命令。然后,它會創建調用對象和附加命令對象執行的操作?,F在,當客戶端程序執行的操作,它是基于命令和接收對象的處理。
我們將著眼于真實的生活場景,我們可以實現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 {@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.fileSystem=fs;}@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.fileSystem=fs;}@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.fileSystem=fs;}@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.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 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
總結
以上是生活随笔為你收集整理的设计模式之-命令模式(Command Pattern)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Unity 游戏设计的元素]
- 下一篇: C++ 标准模板库