美文网首页
设计模式之命令模式

设计模式之命令模式

作者: smallstrong | 来源:发表于2017-05-02 14:55 被阅读13次

    命令模式

    命令模式UML.png
    class GameBoy{
        public void toLeft(){
            //左移
        }
        public void toRight(){
            //右移
        }
        public void toTop(){
            //上移
        }
        public void toBottom(){
            //下移
        }
        public void toA(){
            //A键出拳
        }
        public void toB(){
            //B键踢腿
        }
    }
    

    GameBoy,游戏机类

    interface Command{
        void execute();
    }
    

    按钮接口

    class LeftCommand implements Command{
        private GameBoy gameBoy;
        public LeftCommand(GameBoy gameBoy){
            this.gameBoy = gameBoy;
        }
        public void execute(){
            gameBoy.toLeft();
        }
    }
    
    class RightCommand implements Command{
        private GameBoy gameBoy;
        public RightCommand(GameBoy gameBoy){
            this.gameBoy = gameBoy;
        }
        public void execute(){
            gameBoy.toRight();
        }
    }   
    
    class TopCommand implements Command{
        private GameBoy gameBoy;
        public TopCommand(GameBoy gameBoy){
            this.gameBoy = gameBoy;
        }
        public void execute(){
            gameBoy.toTop();
        }
    }   
    
    class BottomCommand implements Command{
        private GameBoy gameBoy;
        public BottomCommand(GameBoy gameBoy){
            this.gameBoy = gameBoy;
        }
        public void execute(){
            gameBoy.toBottom();
        }
    }   
    
    class ACommand implements Command{
        private GameBoy gameBoy;
        public ACommand(GameBoy gameBoy){
            this.gameBoy = gameBoy;
        }
        public void execute(){
            gameBoy.toA();
        }
    }   
    
    
    class BCommand implements Command{
        private GameBoy gameBoy;
        public BCommand(GameBoy gameBoy){
            this.gameBoy = gameBoy;
        }
        public void execute(){
            gameBoy.toB();
        }
    }   
    

    上述六个类是具体的按键 上下左右AB键

    class Buttons{
        private Command leftCommand,rightCommand,topCommand,bottomCommand,aCommand,bCommand;
        public Buttons(Command leftCommand, Command rightCommand, Command topCommand, Command bottomCommand, Command aCommand, Command bCommand){
            this.leftCommand = leftCommand;
            this.rightCommand = rightCommand;
            this.topCommand = topCommand;
            this.bottomCommand = bottomCommand;
            this.aCommand = aCommand;
            this.bCommand = bCommand;
        }
        public void toLeft(){
            leftCommand.execute();
            //左移
        }
        public void toRight(){
            rightCommand.execute();
            //右移
        }
        public void toTop(){
            topCommand.execute();
            //上移
        }
        public void toBottom(){
            bottomCommand.execute();
            //下移
        }
        public void toA(){
            ACommand.execute();
            //A键出拳
        }
        public void toB(){
            BCommand.execute();
            //B键踢腿
        }
        
    }
    

    键盘按钮类,开关类

    public class Client{
        public static void main(String[] args){
            GameBoy gameBoy = new GameBoy();//游戏机先创建出来
            // 上下左右AB命令
            Command leftCommand,rightCommand,topCommand,bottomCommand,aCommand,bCommand;
            leftCommand = new LeftCommand(gameBoy);
            rightCommand = new RightCommand(gameBoy);
            topCommand = new TopCommand(gameBoy);
            bottomCommand = new BottomCommand(gameBoy);
            aCommand = new ACommand(gameBoy);
            bCommand = new BCommand(gameBoy);
            
            // 具体操作的按键
            Buttons buttons = new Buttons(leftCommand,rightCommand,topCommand,bottomCommand,aCommand,bCommand);
            //按键按下执行操作
            buttons.toLeft();
            buttons.toRight();
            buttons.toTop();
            buttons.toBottom();
            buttons.toA();
            buttons.toB();
        }       
    }
    

    个人总结

    上述用例讲的是一个小游戏机运用了命令模式,将按钮这个动作请求和具体实现功能解耦。功能上的解耦伴随的代价是类的数量的增多,在实际开发中根据场景来考量是否采用。

    相关文章

      网友评论

          本文标题:设计模式之命令模式

          本文链接:https://www.haomeiwen.com/subject/tiictxtx.html