<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 命令設計模式 > 原文: [https://howtodoinjava.com/design-patterns/behavioral/command-pattern/](https://howtodoinjava.com/design-patterns/behavioral/command-pattern/) **命令模式**是一種[行為型設計模式](//howtodoinjava.com/gang-of-four-java-design-patterns/),可用于**將業務邏輯抽象為離散的動作**,我們將其稱為*命令*。 此命令對象有助于松散耦合兩個類之間的關系,其中一個類(調用者)應調用另一類(接收者)上的方法來執行業務操作。 讓我們學習命令如何幫助將調用者與接收者解耦。 ```java Table of Contents Introduction Design Participants Problem Statement Command Pattern Implementation Demo When to Use Command Pattern Popular Implementations Summary ``` ## 介紹 在面向對象的編程中,命令模式是一種行為型設計模式,其中一個對象用于[封裝](//howtodoinjava.com/object-oriented/encapsulation-in-java-and-its-relation-with-abstraction/)執行動作,業務操作或觸發事件所需的所有信息。 方法名稱,接收方對象引用和方法參數值(如果有)。 該對象稱為*命令*。 類似的方法也適用于[責任鏈](//howtodoinjava.com/design-patterns/behavioral/chain-of-responsibility-design-pattern/)模式。 唯一的區別是命令中只有一個請求處理器,而在責任鏈中單個請求對象可以有許多處理器。 ## 設計參與者 命令設計模式的參與者包括: * **命令接口** – 用于聲明操作。 * **具體命令類** – 擴展了`Command`接口,并具有用于在接收方上調用業務操作方法的執行方法。 它在內部具有命令接收者的參考。 * **調用者** – 被賦予執行操作的命令對象。 * **接收器** – 執行操作。 在命令模式中,調用者與接收者執行的動作分離。 調用者不知道接收者。 調用者調用一個命令,然后該命令執行接收方的相應操作。 因此,調用者可以在不知道要執行的動作的細節的情況下調用命令。 此外,這種脫鉤意味著對接收者動作的更改不會直接影響動作的調用。 ## 問題陳述 假設我們需要為家庭自動化系統構建一個遙控器,該遙控器應控制房屋的不同照明/電氣單元。 遙控器中的單個按鈕可能能夠在類似設備上執行相同的操作,例如,電視開/關按鈕可用于打開/關閉不同房間中的不同電視機。 在這里,此遙控器將是一個可編程的遙控器,它將用于打開和關閉各種燈/風扇等。 首先,讓我們看看如何使用任何設計方法解決問題。 她的遙控器代碼可能看起來像: ```java If(buttonName.equals(“Light”)) { //Logic to turn on that light } else If(buttonName.equals(“Fan”)) { //Logic to turn on that Fan } ``` 但是上述解決方案顯然存在許多明顯的問題,例如: * 任何新項目(例如`TubeLight`)都需要更改遙控器的代碼。 您將需要添加更多的`if-else`。 * 如果我們想為其他目的更改按鈕,那么我們也需要更改代碼。 * 最重要的是,如果家里有很多項目,代碼的復雜性和可維護性將會增加。 * 最后,代碼不干凈且緊密耦合,我們未遵循*為接口編碼*等最佳實踐。 ## 命令模式實現 讓我們用命令設計模式解決上述家庭自動化問題,并一次設計一個組件。 * `ICommand`接口是**命令接口** * `Light`是**接收器**組件之一。 它可以接受與`Light`有關的多個命令,例如打開和關閉 * `Fan`也是**接收器**組件的另一種類型。 它可以接受與風扇相關的多個命令,例如打開和關閉 * `HomeAutomationRemote`是**調用者**對象,它要求命令執行請求。 這里風扇開/關,燈開/關。 * `StartFanCommand`,`StopFanCommand`,`TurnOffLightCommand`,`TurnOnLightCommand`等是**命令實現**的不同類型。 #### 類圖 ![Class Diagram](https://img.kancloud.cn/5a/89/5a89581b702f9288701c99633760acad_1216x483.png) 命令模式類圖 讓我們看一下每個類和接口的 java 源代碼。 `ICommand.java` ```java package com.howtodoinjava.designpattern.command.homeautomation; /** * Command Interface which will be implemented by the exact commands. * */ @FunctionalInterface public interface ICommand { public void execute(); } ``` `Light.java` ```java package com.howtodoinjava.designpattern.command.homeautomation.light; /** * Light is a Receiver component in command pattern terminology. * */ public class Light { public void turnOn() { System.out.println("Light is on"); } public void turnOff() { System.out.println("Light is off"); } } ``` `Fan.java` ```java package com.howtodoinjava.designpattern.command.homeautomation.fan; /** * Fan class is a Receiver component in command pattern terminology. * */ public class Fan { void start() { System.out.println("Fan Started.."); } void stop() { System.out.println("Fan stopped.."); } } ``` `TurnOffLightCommand.java` ```java package com.howtodoinjava.designpattern.command.homeautomation.light; import com.howtodoinjava.designpattern.command.homeautomation.ICommand; /** * Light Start Command where we are encapsulating both Object[light] and the * operation[turnOn] together as command. This is the essence of the command. * */ public class TurnOffLightCommand implements ICommand { Light light; public TurnOffLightCommand(Light light) { super(); this.light = light; } public void execute() { System.out.println("Turning off light."); light.turnOff(); } } ``` `TurnOnLightCommand.java` ```java package com.howtodoinjava.designpattern.command.homeautomation.light; import com.howtodoinjava.designpattern.command.homeautomation.ICommand; /** * Light stop Command where we are encapsulating both Object[light] and the * operation[turnOff] together as command. This is the essence of the command. * */ public class TurnOnLightCommand implements ICommand { Light light; public TurnOnLightCommand(Light light) { super(); this.light = light; } public void execute() { System.out.println("Turning on light."); light.turnOn(); } } ``` `StartFanCommand.java` ```java package com.howtodoinjava.designpattern.command.homeautomation.fan; import com.howtodoinjava.designpattern.command.homeautomation.ICommand; /** * Fan Start Command where we are encapsulating both Object[fan] and the * operation[start] together as command. This is the essence of the command. * */ public class StartFanCommand implements ICommand { Fan fan; public StartFanCommand(Fan fan) { super(); this.fan = fan; } public void execute() { System.out.println("starting Fan."); fan.start(); } } ``` `StopFanCommand.java` ```java package com.howtodoinjava.designpattern.command.homeautomation.fan; import com.howtodoinjava.designpattern.command.homeautomation.ICommand; /** * Fan stop Command where we are encapsulating both Object[fan] and the * operation[stop] together as command. This is the essence of the command. * */ public class StopFanCommand implements ICommand { Fan fan; public StopFanCommand(Fan fan) { super(); this.fan = fan; } public void execute() { System.out.println("stopping Fan."); fan.stop(); } } ``` `HomeAutomationRemote.java` ```java package com.howtodoinjava.designpattern.command.homeautomation; /** * Remote Control for Home automation where it will accept the command and * execute. This is the invoker in terms of command pattern terminology */ public class HomeAutomationRemote { //Command Holder ICommand command; //Set the command in runtime to trigger. public void setCommand(ICommand command) { this.command = command; } //Will call the command interface method so that particular command can be invoked. public void buttonPressed() { command.execute(); } } ``` ## 演示 讓我們編寫代碼并執行客戶端代碼,以查看命令的執行方式。 ```java package com.howtodoinjava.designpattern.command.homeautomation; import com.howtodoinjava.designpattern.command.homeautomation.fan.Fan; import com.howtodoinjava.designpattern.command.homeautomation.fan.StartFanCommand; import com.howtodoinjava.designpattern.command.homeautomation.fan.StopFanCommand; import com.howtodoinjava.designpattern.command.homeautomation.light.Light; import com.howtodoinjava.designpattern.command.homeautomation.light.TurnOnLightCommand; /** * Demo class for HomeAutomation * */ public class Demo //client { public static void main(String[] args) { Light livingRoomLight = new Light(); //receiver 1 Fan livingRoomFan = new Fan(); //receiver 2 Light bedRoomLight = new Light(); //receiver 3 Fan bedRoomFan = new Fan(); //receiver 4 HomeAutomationRemote remote = new HomeAutomationRemote(); //Invoker remote.setCommand(new TurnOnLightCommand( livingRoomLight )); remote.buttonPressed(); remote.setCommand(new TurnOnLightCommand( bedRoomLight )); remote.buttonPressed(); remote.setCommand(new StartFanCommand( livingRoomFan )); remote.buttonPressed(); remote.setCommand(new StopFanCommand( livingRoomFan )); remote.buttonPressed(); remote.setCommand(new StartFanCommand( bedRoomFan )); remote.buttonPressed(); remote.setCommand(new StopFanCommand( bedRoomFan )); remote.buttonPressed(); } } ``` 輸出: ```java Turning on light. Light is on Turning on light. Light is on starting Fan. Fan Started.. stopping Fan. Fan stopped.. starting Fan. Fan Started.. stopping Fan. Fan stopped.. ``` ## 何時使用命令模式 您可以使用命令模式來解決許多設計問題,例如: * 處理 Java 菜單項和按鈕的動作。 * 提供對宏的支持(宏的記錄和播放)。 * 提供“撤消”支持。 * 進度條實現。 * 創建多步驟向導。 ## 流行的命令模式實現 這些是命令模式實現的一些實際示例: * 可運行接口(`java.lang.Runnable`) * Swing `Action`(`javax.swing.Action`)使用命令模式 * `ActionServlet`調用 Struts `Action`類在內部使用命令模式。 ## 總結 1. 命令模式是一種行為型設計模式。 2. 在命令模式中,對象用于封裝在任何時間執行操作或觸發事件所需的所有信息,從而使開發人員能夠分散業務邏輯。 它稱為命令。 3. 客戶端與調用者對話并傳遞命令對象。 4. 每個命令對象都有對其接收者的引用。 5. `Command`的`execute()`方法調用接收器中定義的實際業務操作。 6. 接收者執行業務操作。 這是此模式的一些利弊。 它可以幫助您就使用命令模式做出正確的決定。 #### 優點 * 使我們的代碼具有可伸縮性,因為我們可以添加新命令而無需更改現有代碼。 * 使用命令對象增加調用者和接收者之間的松散耦合。 #### 缺點 * 在不同的視圖中,增加每個單獨命令的類數。 在某些特定情況下,它可能不是首選。 這就是*命令設計模式*的全部。 將我的問題放在評論部分。 [下載源碼](//howtodoinjava.com/wp-content/downloads/Command-Design-Pattern.zip) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看