1. 程式人生 > >java設計模式之命令模式

java設計模式之命令模式

int aud 按鍵 設計 oid 定義 bsp class 命令

命令模式:

  對命令的封裝,把發出命令的責任和執行命令的責任分割開,委派給不同的對象。

命令模式涉及到五個角色:

  • 客戶端(CommandMain)角色:創建一個具體命令並確定接收者(觸發錄音機按鍵者)
  • 命令(Command)角色:聲明一個給所有具體命令類的抽象接口(定義一個命令接口)
  • 具體命令(PlayCommand、RewindCommand、StopCommand)角色:實現命令接口,調用錄音機的相應操作;
  • 按鍵(Keypad)角色:負責調用相應命令對象
  • 錄音機(AudioPlayer)角色:負責具體實施和執行一個請求。

錄音機類:

/**
 * 具體工作的類(錄音機)
 
*/ public class AudioPlayer { public void play() { System.out.println("播放..."); } public void rewind() { System.out.println("倒帶..."); } public void stop() { System.out.println("停止播放..."); } }

命令接口:

/**
 * 執行錄音機的命令
 */
public interface
Command { public void execute(); }

播放命令:

/**
 * 播放命令
 */
public class PlayCommand implements Command {
    private AudioPlayer audioPlayer;

    public PlayCommand(AudioPlayer audioPlayer) {
        this.audioPlayer = audioPlayer;
    }

    @Override
    public void execute() {
        audioPlayer.play();
    }
}

倒帶命令:

/**
 * 倒帶命令
 */
public class RewindCommand implements Command {

    private AudioPlayer audioPlayer;

    public RewindCommand(AudioPlayer audioPlayer) {
        this.audioPlayer = audioPlayer;
    }

    @Override
    public void execute() {
        audioPlayer.rewind();
    }
}

停止命令:

/**
 * 停止命令
 */
public class StopCommand implements Command {
    private AudioPlayer audioPlayer;

    public StopCommand(AudioPlayer audioPlayer) {
        this.audioPlayer = audioPlayer;
    }

    @Override
    public void execute() {
        audioPlayer.stop();
    }
}

按鍵類:

**
 * 按鍵類
 */
public class Keypad {

    private Command playCommand;
    private Command rewindCommand;
    private Command stopCommand;

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

    public void setRewindCommand(Command rewindCommand) {
        this.rewindCommand = rewindCommand;
    }

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

    /**
     * 執行播放
     */
    public void pay() {
        playCommand.execute();
    }

    /**
     * 執行倒帶
     */
    public void rewind() {
        rewindCommand.execute();
    }

    /**
     * 執行停止
     */
    public void stop() {
        stopCommand.execute();
    }
}

錄音機操作類:

/**
 * 操作按鈕-》發送命令給錄音機執行具體操作
 */
public class CommandMain {

    public static void main(String[] args) {
        //new 一個錄音機
        AudioPlayer audioPlayer = new AudioPlayer();
        //創建播放命令對象
        Command playCommand = new PlayCommand(audioPlayer);
        //創建倒帶命令對象
        Command rewindCommand = new RewindCommand(audioPlayer);
        //創建停止命令對象
        Command stopCommand = new StopCommand(audioPlayer);
        //創建按鍵對象
        Keypad keypad = new Keypad();
        //將播放命令綁定在按鍵上
        keypad.setPlayCommand(playCommand);
        //將倒帶命令綁定在按鍵上
        keypad.setRewindCommand(rewindCommand);
        //將停止命令綁定在按鍵上
        keypad.setStopCommand(stopCommand);
        //播放
        keypad.pay();
        //倒帶
        keypad.rewind();
        //停止
        keypad.stop();

        keypad.pay();
        keypad.stop();

    }
}

運行結果:

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

java設計模式之命令模式