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();
}
}