設計モード-コマンドモード(Command Pattern)


コマンドモード(Command Pattern)は、要求応答モデルの緩い結合を実現するために使用されます.コマンドモードでは、要求が呼び出し元に送信され、呼び出されてカプセル化されたコマンドオブジェクトに渡されます.Commandオブジェクトは、要求を受信機に渡す適切な方法で特定の操作を実行します.クライアント・プログラムは、受信機オブジェクトを作成し、コマンドに接続します.その後、作成されます.オブジェクトと追加のコマンドオブジェクトが実行するアクションを呼び出します.これで、クライアント・プログラムが実行する操作は、コマンドおよび受信オブジェクトに基づく処理である.
私たちは実際の生活シーンに着目し、Commandモードを実現することができます.たとえば、WindowsやUnixなど、さまざまなオペレーティングシステムをサポートするファイルシステムツールと、特定のオペレーティングファイルの開く、書く、閉じる機能を提供したいと考えています.
ファイルシステムツールを実装するには、まず、以上のすべての作業を実装する受信機クラスを作成する必要があります.Javaではインタフェースに基づいて設計されているため、Windows、UNIXのSolarisなどの異なるオペレーティングシステムを実装するFileSystemReceiverインタフェースとその実装クラスを持つことができます.
FileSystemReceiver.java
package com.journaldev.design.command;
 
public interface FileSystemReceiver {
 
    void openFile();
    void writeFile();
    void closeFile();
}

FileSystemReceiverインタフェースは実装クラス実装ルールメソッドを定義し、簡単にするためにwindows、linuxの2つの受信機しか作成しませんでした.
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();
}

今、私たちは
作成が必要
インプリメンテーション
すべてが違う
タイプのアクション
受信機
進行中
,
僕らは
3つのacton
を選択します.
3つのコマンド
インプリメンテーション
、各
コマンド#コマンド#
実行
リクエストの転送先
レシーバ
適切な方法.
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モードの欠点はコード量が巨大であることであり,こんなに多くの関連と混乱には大量の動作と方法があるからである.