日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發(fā)布時(shí)間:2023/12/9 asp.net 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式之-命令模式(Command Pattern) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

命令模式(Command Pattern)是用來實(shí)現(xiàn)在一個(gè)請(qǐng)求 - 響應(yīng)模型松耦合。在命令模式中,請(qǐng)求被發(fā)送給調(diào)用者和調(diào)用它傳遞給被封裝的命令對(duì)象。 Command對(duì)象將請(qǐng)求傳遞到接收器的適當(dāng)?shù)姆椒▉韴?zhí)行特定操作。客戶端程序創(chuàng)建接收器對(duì)象,然后將其連接到命令。然后,它會(huì)創(chuàng)建調(diào)用對(duì)象和附加命令對(duì)象執(zhí)行的操作。現(xiàn)在,當(dāng)客戶端程序執(zhí)行的操作,它是基于命令和接收對(duì)象的處理。

我們將著眼于真實(shí)的生活場(chǎng)景,我們可以實(shí)現(xiàn)Command模式。比方說,我們希望提供一個(gè)文件系統(tǒng)工具與具體操作文件的打開,寫入和關(guān)閉文件功能,它應(yīng)該支持多種操作系統(tǒng),如Windows和Unix。

為了實(shí)現(xiàn)我們的文件系統(tǒng)工具,首先我們需要?jiǎng)?chuàng)建一個(gè)接收器類實(shí)現(xiàn)以上所有的工作。由于我們?cè)贘ava依據(jù)接口設(shè)計(jì),我們可以有FileSystemReceiver接口和它的實(shí)現(xiàn)類實(shí)現(xiàn)不同的操作系統(tǒng),如Windows,UNIX的Solaris等


FileSystemReceiver.java

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

FileSystemReceiver接口定義了實(shí)現(xiàn)類實(shí)現(xiàn)規(guī)則方法,為了簡(jiǎn)單,我只創(chuàng)建了windows,linux兩個(gè)接收器。

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接口設(shè)計(jì)與實(shí)現(xiàn):

我們可以使用接口或抽象類實(shí)現(xiàn),具體實(shí)現(xiàn)取決于你的需求,這里我仍使用接口實(shí)現(xiàn)

package com.journaldev.design.command;public interface Command {void execute(); }
現(xiàn)在,我們需要?jiǎng)?chuàng)建實(shí)現(xiàn)所有不同類型的action由接收器進(jìn)行的,因?yàn)槲覀冇腥齻€(gè)acton,我們將創(chuàng)建三個(gè)命令實(shí)現(xiàn),每個(gè)命令執(zhí)行將請(qǐng)求轉(zhuǎn)發(fā)到接收器的適當(dāng)?shù)姆椒ā?

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();}}
現(xiàn)在接收器與Conmand實(shí)現(xiàn)已完成,下面實(shí)現(xiàn)調(diào)用類。

調(diào)用是封裝的命令和請(qǐng)求傳遞給命令對(duì)象來處理它一個(gè)簡(jiǎn)單的類。

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();}}}
下面創(chuàng)建客戶端類使用我們的文件系統(tǒng)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

類結(jié)構(gòu)視圖:



要點(diǎn)總結(jié):

命令是這種模式,它定義了規(guī)則執(zhí)行核心。
接收機(jī)的實(shí)現(xiàn)是由分開的命令執(zhí)行。
命令實(shí)現(xiàn)類選擇的方法來調(diào)用接收對(duì)象,每個(gè)方法的接收器會(huì)有一個(gè)命令執(zhí)行。它的工作原理是接收器和行動(dòng)方法之間的橋梁。
調(diào)用類從客戶端獲取請(qǐng)求,將命令對(duì)象轉(zhuǎn)發(fā)。
客戶端負(fù)責(zé)實(shí)例化相應(yīng)的命令并接收?qǐng)?zhí)行,然后將它們聯(lián)系到一起。
客戶端還負(fù)責(zé)調(diào)用實(shí)例化對(duì)象,并與之關(guān)聯(lián)的命令對(duì)象,并執(zhí)行該操作方法。
Command模式很容易擴(kuò)展的,我們可以在接收器中添加新的操作方法,并創(chuàng)建新的命令的實(shí)現(xiàn)在不改變客戶端代碼。
command模式的缺點(diǎn)是代碼量是巨大的,因?yàn)檫@么多的關(guān)聯(lián)和混亂有大量的動(dòng)作和方法。







轉(zhuǎn)載于:https://www.cnblogs.com/happyxiaoyu02/p/6818938.html

總結(jié)

以上是生活随笔為你收集整理的设计模式之-命令模式(Command Pattern)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。