命令模式

描述

????命令模式(Command Pattern)是一種數(shù)據(jù)驅動的設計模式,它屬于行為型模式。請求以命令的形式包裹在對象中,并傳給調用對象。調用對象尋找可以處理該命令的合適的對象,并把該命令傳給相應的對象,該對象執(zhí)行命令。

簡介

????命令模式是對命令的封裝。命令模式把發(fā)出命令的責任和執(zhí)行命令的責任分割開,委派給不同的對象。
????每一個命令都是一個操作:請求的一方發(fā)出請求要求執(zhí)行一個操作;接收的一方收到請求,并執(zhí)行操作。命令模式允許請求的一方和接收的一方獨立開來,使得請求的一方不必知道接收請求的一方的接口,更不必知道請求是怎么被接收,以及操作是否被執(zhí)行、何時被執(zhí)行,以及是怎么被執(zhí)行的。


命令模式類圖

角色

  • 客戶端(Client)角色:創(chuàng)建一個具體命令(ConcreteCommand)對象并確定其接收者。
  • 命令(Command)角色:聲明了一個給所有具體命令類的抽象接口。
  • 具體命令(ConcreteCommand)角色:定義一個接收者和行為之間的弱耦合;實現(xiàn)execute()方法,負責調用接收者的相應操作。execute()方法通常叫做執(zhí)行方法。
  • 請求者(Invoker)角色:負責調用命令對象執(zhí)行請求,相關的方法叫做行動方法。
  • 接收者(Receiver)角色:負責具體實施和執(zhí)行一個請求。任何一個類都可以成為接收者,實施和執(zhí)行請求的方法叫做行動方法。

優(yōu)缺點

優(yōu)點

  • 降低系統(tǒng)的耦合度。命令模式能將調用操作的對象與實現(xiàn)該操作的對象解耦。
  • 增加或刪除命令非常方便。采用命令模式增加與刪除命令不會影響其他類,它滿足“開閉原則”,對擴展比較靈活。
  • 可以實現(xiàn)宏命令。命令模式中的命令對象能夠很容易地組合成復合命令,也就是宏命令,從而使系統(tǒng)操作更簡單,功能更強大。

缺點

  • 可能產生大量具體命令類。因為計對每一個具體操作都需要設計一個具體命令類,這將增加系統(tǒng)的復雜性。

使用場景

  • 當系統(tǒng)需要將請求調用者與請求接收者解耦時,命令模式使得調用者和接收者不直接交互。
  • 當系統(tǒng)需要隨機請求命令或經常增加或刪除命令時,命令模式比較方便實現(xiàn)這些功能。
  • 當系統(tǒng)需要執(zhí)行一組操作時,命令模式可以定義宏命令來實現(xiàn)該功能。

示例

/**
* 接收者角色
**/
public class Receiver {
    public void operation(){
        System.out.println("執(zhí)行操作!");
    }
}
/**
* 抽象命令角色
**/
public interface Command {
    /**
     * 執(zhí)行方法
     */
    void execute();
}
/**
* 具體命令角色
**/
public class ConcreteCommand implements Command {
    private Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    /**
     * 執(zhí)行方法
     */
    @Override
    public void execute() {
        receiver.operation();
    }
}
/**
* 請求者角色
**/
public class Invoker {
    private Command command;

    public Invoker(Command command) {
        this.command = command;
    }

    public void operation() {
        command.execute();
    }
}
/**
* 客戶端角色
**/
public class Client {
    public static void main(String[] args) {
        //創(chuàng)建接收者
        Receiver receiver = new Receiver();
        //創(chuàng)建命令對象,設定它的接收者
        Command command = new ConcreteCommand(receiver);
        //創(chuàng)建請求者,把命令對象設置進去
        Invoker invoker = new Invoker(command);
        //執(zhí)行方法
        invoker.operation();
    }
}

播放視頻的示例

/**
* 接收者角色
**/
public class VideoPlayer {
    public void play() {
        System.out.println("播放");
    }

    public void stop() {
        System.out.println("暫停");
    }

    public void fastForward() {
        System.out.println("快進");
    }
}
/**
* 抽象命令角色
**/
public interface Command {
    /**
     * 執(zhí)行方法
     */
    void execute();
}
/**
* 具體命令角色
**/
public class PlayCommand implements Command {
    private VideoPlayer videoPlayer;

    public PlayCommand(VideoPlayer videoPlayer) {
        this.videoPlayer = videoPlayer;
    }

    /**
     * 執(zhí)行方法
     */
    @Override
    public void execute() {
        videoPlayer.play();
    }
}
/**
* 具體命令角色
**/
public class StopCommand implements Command {
    private VideoPlayer videoPlayer;

    public StopCommand(VideoPlayer videoPlayer) {
        this.videoPlayer = videoPlayer;
    }

    /**
     * 執(zhí)行方法
     */
    @Override
    public void execute() {
        videoPlayer.stop();
    }
}
/**
* 具體命令角色
**/
public class FastForwardCommand implements Command {
    private VideoPlayer videoPlayer;

    public FastForwardCommand(VideoPlayer videoPlayer) {
        this.videoPlayer = videoPlayer;
    }

    /**
     * 執(zhí)行方法
     */
    @Override
    public void execute() {
        videoPlayer.fastForward();
    }
}
/**
* 請求者角色
**/
public class Mouse {
    private Command playCommand;
    private Command stopCommand;
    private Command fastForwardCommand;

    public void play() {
        playCommand.execute();
    }

    public void stop() {
        stopCommand.execute();
    }

    public void fastForward() {
        fastForwardCommand.execute();
    }

    public Command getPlayCommand() {
        return playCommand;
    }

    public void setPlayCommand(Command playCommand) {
        this.playCommand = playCommand;
    }

    public Command getStopCommand() {
        return stopCommand;
    }

    public void setStopCommand(Command stopCommand) {
        this.stopCommand = stopCommand;
    }

    public Command getFastForwardCommand() {
        return fastForwardCommand;
    }

    public void setFastForwardCommand(Command fastForwardCommand) {
        this.fastForwardCommand = fastForwardCommand;
    }
}
/**
* 客戶端角色
**/
public class Client {
    public static void main(String[] args) {
        VideoPlayer videoPlayer = new VideoPlayer();

        Command playCommand = new PlayCommand(videoPlayer);
        Command stopCommand = new StopCommand(videoPlayer);
        Command fastForwardCommand = new FastForwardCommand(videoPlayer);

        Mouse mouse = new Mouse();
        mouse.setPlayCommand(playCommand);
        mouse.setStopCommand(stopCommand);
        mouse.setFastForwardCommand(fastForwardCommand);

        mouse.play();
        mouse.stop();
        mouse.fastForward();
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容