命令模式

package com.example.designmodel;

/**
 * 命令模式
 */
public class CommandModel {

    public class ReceiverTV{
        private String name;
        ReceiverTV(String name){
            this.name = name;
        }
        public String getName(){
            return name;
        }
        public void openTv(){
            System.out.println("打开了"+name);
        }
        public void closeTV(){
            System.out.println("关闭了"+name);
        }
    }

    /**
     * 命令超类
     */
    public interface ICommand{
        void executor();
    }

    /**
     * 遥控关闭电视
     */
    public class CloseCommand implements ICommand{
        private ReceiverTV receiverTV;
        /**
         * 需要关闭哪台电视机
         */
        CloseCommand(ReceiverTV receiverTV){
            this.receiverTV = receiverTV;
        }
        @Override
        public void executor() {
            System.out.println("请关闭"+receiverTV.getName());
            receiverTV.closeTV();
        }
    }

    /**
     * 遥控关闭电视
     */
    public class OpenCommand implements ICommand{
        private ReceiverTV receiverTV;
        /**
         * 需要打开哪台电视机
         */
        OpenCommand(ReceiverTV receiverTV){
            this.receiverTV = receiverTV;
        }
        @Override
        public void executor() {
            System.out.println("请打开"+receiverTV.getName());
            receiverTV.openTv();
        }
    }

    /**
     * 遥控发送命令
     */
    public class InvokerController{
        private ICommand command = null;
        InvokerController(ICommand command){
            this.command = command;
        }
        public void exe(){
            command.executor();
        }
    }

    public static void main(String[] args){
        CommandModel commandModel = new CommandModel();
        ReceiverTV receiverTV = commandModel.new ReceiverTV("5号电视");
        OpenCommand openCommand = commandModel.new OpenCommand(receiverTV);
        InvokerController invokerController = commandModel.new InvokerController(openCommand);
        invokerController.exe();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值