zoukankan      html  css  js  c++  java
  • java设计模式之命令模式

    命令模式:

      对命令的封装,把发出命令的责任和执行命令的责任分割开,委派给不同的对象。

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

    • 客户端(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();
    
        }
    }

    运行结果:

  • 相关阅读:
    11.ForkJoinPool 分支/合并框架 (工作窃取)
    10.线程池_线程调度
    9.线程八锁
    8.读写锁ReadWriteLock
    7.生产者消费者 案例 (使用Lock 同步锁 方式,使用Condition完成线程之间的通信)
    ScrollView嵌套子View的getDrawingCache为空的解决方法
    装箱与拆箱
    Java核心技术卷一基础知识-第11章-异常、断言、日志和调试-读书笔记
    Java核心技术卷一基础知识-第9章-Swing用户界面组件-读书笔记
    Java核心技术卷一基础知识-第8章-事件处理-读书笔记
  • 原文地址:https://www.cnblogs.com/hujinshui/p/9451984.html
Copyright © 2011-2022 走看看