老闆:阿飛,咱們公司又接了個新項目,一個客戶,,臥室和客廳很大,電燈電視開關也不好找,所以希望製造一個遙控器來控制一些傢俱的開啓與關閉,目前需要5個按鍵,臥室的燈,臥室的電視,客廳的燈,客廳的電視,在留一個預備按鍵。我等會把需求文檔給你。
項目組長阿飛:好的,老闆
項目組長阿飛:小三,來了個需求,你看下,你先設計一下架構
阿三:好的,飛哥
三天過後:飛哥,好了,你看下
先設計了一個接口,裏面包含了,每一個按鈕的統一行爲
package com.commandPattern.command;
/**
* @program: testPattern
* @description: 命令接口
* @author: Mr.Yang
* @create: 2018-12-08 13:54
**/
public interface Command {
//執行方法
public void exceute();
}
然後建立了一個對象,代表了空對象,什麼操作也不執行
package com.commandPattern.command.nullCommand;
import com.commandPattern.command.Command;
/**
* @program: testPattern
* @description: 建立一個空對象,在許多設計模式種,都會看到空對象的使用,甚至有些時候,空對象本身也被視爲一種設計模式
* @author: Mr.Yang
* @create: 2018-12-08 17:40
**/
public class NullCommand implements Command {
public void exceute() {
System.out.println("什麼都不做處理");
}
}
燈的具體類
package com.commandPattern.entity;
/**
* @program: testPattern
* @description: 燈的具體類
* @author: Mr.Yang
* @create: 2018-12-08 17:31
**/
public class Lamp {
private String name;
/**
* name爲燈的具體裝飾,即爲哪裏的燈
* @param name
*/
public Lamp(String name){
this.name=name;
}
public void on (){
System.out.println(name+"_燈打開");
}
public void off (){
System.out.println(name+"_燈關閉");
}
}
電視的具體類
package com.commandPattern.entity;
/**
* @program: testPattern
* @description: 電視的具體類
* @author: Mr.Yang
* @create: 2018-12-08 17:35
**/
public class Tv {
private String name;
public Tv(String name){
this.name=name;
}
public void on (){
System.out.println(name+"_電視打開");
}
public void off(){
System.out.println(name+"_電視關閉");
}
}
關閉燈的具體命令
package com.commandPattern.command.off;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;
/**
* @program: testPattern
* @description: 燈關閉
* @author: Mr.Yang
* @create: 2018-12-08 17:33
**/
public class LampOffCommand implements Command {
Lamp lamp;
public LampOffCommand(Lamp lamp){
this.lamp=lamp;
}
//燈關閉
public void exceute() {
lamp.off();
}
}
打開燈的具體命令
package com.commandPattern.command.on;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;
/**
* @program: testPattern
* @description: 燈打開的命令
* @author: Mr.Yang
* @create: 2018-12-08 17:29
**/
public class LampOnCommand implements Command {
Lamp lamp;
public LampOnCommand(Lamp lamp){
this.lamp=lamp;
}
//燈打開的命令
public void exceute() {
lamp.on();
}
}
電視關閉的具體命令
package com.commandPattern.command.off;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;
/**
* @program: testPattern
* @description: 電視關閉
* @author: Mr.Yang
* @create: 2018-12-08 17:36
**/
public class TvOffCommand implements Command {
Tv tv;
public TvOffCommand(Tv tv){
this.tv=tv;
}
public void exceute() {
tv.off();
}
}
電視打開的具體命令
package com.commandPattern.command.on;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;
/**
* @program: testPattern
* @description: 電視打開
* @author: Mr.Yang
* @create: 2018-12-08 17:37
**/
public class TvOnCommand implements Command {
Tv tv;
public TvOnCommand(Tv tv){
this.tv=tv;
}
public void exceute() {
tv.on();
}
}
建立一個遙控器
package com.commandPattern.control;
import com.commandPattern.command.Command;
import com.commandPattern.command.nullCommand.NullCommand;
import java.util.Arrays;
/**
* @program: testPattern
* @description: 遙控器
* @author: Mr.Yang
* @create: 2018-12-08 17:39
**/
public class RemoteControl {
Command[] onCommand;
Command[] offCommand;
//初始化每個操作爲空操作
public RemoteControl(){
onCommand=new Command[5];
offCommand=new Command[5];
NullCommand nullCommand = new NullCommand();
for (int i = 0; i < 5; i++) {
onCommand[i]=nullCommand;
offCommand[i]=nullCommand;
}
}
public void setCommond(int index,Command onCommand,Command offCommand){
this.offCommand[index]=offCommand;
this.onCommand[index]=onCommand;
}
public void clickOn(int index){
onCommand[index].exceute();
}
public void clickOff(int index){
offCommand[index].exceute();
}
/**
* 輸出每個按鈕的具體代表類
* @return
*/
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < onCommand.length; i++) {
sb.append("[index : "+i+"] ");
sb.append(onCommand[i].getClass().getName());
sb.append(" ");
sb.append(offCommand[i].getClass().getName());
sb.append("\r\n");
}
return sb.toString();
}
}
測試類
package com.commandPattern.testPattern;
import com.commandPattern.command.off.LampOffCommand;
import com.commandPattern.command.off.TvOffCommand;
import com.commandPattern.command.on.LampOnCommand;
import com.commandPattern.command.on.TvOnCommand;
import com.commandPattern.control.RemoteControl;
import com.commandPattern.entity.Lamp;
import com.commandPattern.entity.Tv;
/**
* @program: test
* @description:
* @author: Mr.Yang
* @create: 2018-12-08 17:48
**/
public class TestPattern {
public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl();
/**
* 創建裝置到合適位置
*/
Tv bedRoomTV = new Tv("臥室");
Tv drawiTV = new Tv("客廳");
Lamp bedRoomLamp = new Lamp("臥室");
Lamp drawiLamp = new Lamp("客廳");
/**
* 創建所有命令操作對象
*/
//臥室燈關閉對象
LampOffCommand bedLampOffCommand = new LampOffCommand(bedRoomLamp);
//臥室燈打開對象
LampOnCommand bedLampOnCommand = new LampOnCommand(bedRoomLamp);
//臥室TV關閉對象
TvOffCommand bedTvOffCommand = new TvOffCommand(bedRoomTV);
//臥室TV打開對象
TvOnCommand bedTVcommand = new TvOnCommand(bedRoomTV);
//客廳燈打開對象
LampOnCommand drawLampOnCommand = new LampOnCommand(drawiLamp);
//客廳燈關閉對象
LampOffCommand drawLampOffCommand = new LampOffCommand(drawiLamp);
//客廳TV關閉對象
TvOffCommand drawTVOffCommand = new TvOffCommand(drawiTV);
//客廳TV打開對象
TvOnCommand drawTVOnCommand = new TvOnCommand(drawiTV);
System.out.println("---------------------------------------------未賦值之前------------------------------------------------");
System.out.println(remoteControl);
System.out.println("******************************************************************************************************");
/**
* //將操作對象與卡槽一一對應
*/
//賦值臥室燈打開與關閉
remoteControl.setCommond(0,bedLampOnCommand,bedLampOffCommand);
//賦值臥室TV打開與關閉
remoteControl.setCommond(1,bedTVcommand,bedTvOffCommand);
//賦值客廳燈打開與關閉
remoteControl.setCommond(2,drawLampOnCommand,drawLampOffCommand);
//賦值客廳TV打開與關閉
remoteControl.setCommond(3,drawTVOnCommand,drawTVOffCommand);
System.out.println("---------------------------------------------賦值之後------------------------------------------------");
System.out.println(remoteControl);
System.out.println("******************************************************************************************************");
/**
* 測試每一個按鈕
*/
remoteControl.clickOn(0);
remoteControl.clickOff(0);
remoteControl.clickOn(1);
remoteControl.clickOff(1);
remoteControl.clickOn(2);
remoteControl.clickOff(2);
remoteControl.clickOn(3);
remoteControl.clickOff(3);
}
}
測試結果
---------------------------------------------未賦值之前------------------------------------------------
[index : 0] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 1] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 2] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 3] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
******************************************************************************************************
---------------------------------------------賦值之後------------------------------------------------
[index : 0] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
[index : 1] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
[index : 2] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
[index : 3] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
[index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
******************************************************************************************************
臥室_燈打開
臥室_燈關閉
臥室_電視打開
臥室_電視關閉
客廳_燈打開
客廳_燈關閉
客廳_電視打開
客廳_電視關閉
阿三:飛哥,我這裏使用的設計模式-命令模式,
將動作執行(LampOnCommand,TvOnCommand……)與接收者(Lamp,Tv)包裝到對象裏面,對外暴露的只有一個Command接口中的execute方法,其他對象不需要知道那個接收者執行了什麼動作,只需要知道調用execute,就能完成一個請求的操作,這個對象,與其他對象沒有關聯,完全解耦和,如果需要做新增,不需要修改原有代碼,拓展接收者類和動作執行類,就能實現功能。
項目組長阿飛:不錯,不錯,進步很大。
項目組長阿飛:第5個按鈕可能需要做一個恢復上一步動作的效果,類似於CTRL+Z這個效果,你再改改把。
阿三:好的。
阿三:飛哥修改好了,你看下
命令接口新增撤銷方法
package com.commandPattern.command;
/**
* @program: testPattern
* @description: 命令接口
* @author: Mr.Yang
* @create: 2018-12-08 13:54
**/
public interface Command {
//執行方法
public void exceute();
//撤銷方法
public void revoke();
}
建立一個空對象,實現了撤銷方法
package com.commandPattern.command.nullCommand;
import com.commandPattern.command.Command;
/**
* @program: testPattern
* @description: 建立一個空對象,在許多設計模式種,都會看到空對象的使用,甚至有些時候,空對象本身也被視爲一種設計模式
* @author: Mr.Yang
* @create: 2018-12-08 17:40
**/
public class NullCommand implements Command {
public void exceute() {
System.out.println("什麼都不做處理");
}
public void revoke() {
System.out.println("什麼都不做處理");
}
}
燈關閉實現了撤銷方法
package com.commandPattern.command.off;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;
/**
* @program: testPattern
* @description: 燈關閉
* @author: Mr.Yang
* @create: 2018-12-08 17:33
**/
public class LampOffCommand implements Command {
Lamp lamp;
public LampOffCommand(Lamp lamp){
this.lamp=lamp;
}
//燈關閉
public void exceute() {
lamp.off();
}
//執行到這個具體實現類,代表上一步是燈關閉,撤銷操作即爲燈打開
public void revoke() {
lamp.on();
}
}
燈打開實現了撤銷方法
package com.commandPattern.command.on;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Lamp;
/**
* @program: testPattern
* @description: 燈打開的命令
* @author: Mr.Yang
* @create: 2018-12-08 17:29
**/
public class LampOnCommand implements Command {
Lamp lamp;
public LampOnCommand(Lamp lamp){
this.lamp=lamp;
}
//燈打開的命令
public void exceute() {
lamp.on();
}
//執行到這個具體實現類,代表上一步是燈打開,撤銷操作即爲燈關閉
public void revoke() {
lamp.off();
}
}
電視關閉實現了撤銷方法
package com.commandPattern.command.off;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;
/**
* @program: testPattern
* @description: 電視關閉
* @author: Mr.Yang
* @create: 2018-12-08 17:36
**/
public class TvOffCommand implements Command {
Tv tv;
public TvOffCommand(Tv tv){
this.tv=tv;
}
public void exceute() {
tv.off();
}
//執行到這個具體實現類,代表上一步是電視關閉,撤銷操作即爲電視打開
public void revoke() {
tv.on();
}
}
電視打開實現了撤銷方法
package com.commandPattern.command.on;
import com.commandPattern.command.Command;
import com.commandPattern.entity.Tv;
/**
* @program: testPattern
* @description: 電視打開
* @author: Mr.Yang
* @create: 2018-12-08 17:37
**/
public class TvOnCommand implements Command {
Tv tv;
public TvOnCommand(Tv tv){
this.tv=tv;
}
public void exceute() {
tv.on();
}
//執行到這個具體實現類,代表上一步是電視打開,撤銷操作即爲電視關閉
public void revoke() {
tv.off();
}
}
遙控器類,新增變量記錄上一步操作
package com.commandPattern.control;
import com.commandPattern.command.Command;
import com.commandPattern.command.nullCommand.NullCommand;
import java.util.Arrays;
/**
* @program: testPattern
* @description: 遙控器
* @author: Mr.Yang
* @create: 2018-12-08 17:39
**/
public class RemoteControl {
Command[] onCommand;
Command[] offCommand;
//這個變量來記錄上一個命令
Command upStepCommand;
//初始化每個操作爲空操作
public RemoteControl(){
onCommand=new Command[5];
offCommand=new Command[5];
NullCommand nullCommand = new NullCommand();
for (int i = 0; i < 5; i++) {
onCommand[i]=nullCommand;
offCommand[i]=nullCommand;
}
upStepCommand=nullCommand;
}
public void setCommond(int index,Command onCommand,Command offCommand){
this.offCommand[index]=offCommand;
this.onCommand[index]=onCommand;
}
//新增upStepCommand記錄上一步命令
public void clickOn(int index){
onCommand[index].exceute();
upStepCommand=onCommand[index];
}
//新增upStepCommand記錄上一步命令
public void clickOff(int index){
offCommand[index].exceute();
upStepCommand=offCommand[index];
}
public void toUpStepClick(){
System.out.println("---撤銷---");
upStepCommand.revoke();
}
/**
* 輸出每個按鈕的具體代表類
* @return
*/
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < onCommand.length; i++) {
sb.append("[index : "+i+"] ");
sb.append(onCommand[i].getClass().getName());
sb.append(" ");
sb.append(offCommand[i].getClass().getName());
sb.append("\r\n");
}
return sb.toString();
}
}
測試類新增撤銷測試
package com.commandPattern.testPattern;
import com.commandPattern.command.off.LampOffCommand;
import com.commandPattern.command.off.TvOffCommand;
import com.commandPattern.command.on.LampOnCommand;
import com.commandPattern.command.on.TvOnCommand;
import com.commandPattern.control.RemoteControl;
import com.commandPattern.entity.Lamp;
import com.commandPattern.entity.Tv;
/**
* @program: test
* @description:
* @author: Mr.Yang
* @create: 2018-12-08 17:48
**/
public class TestPattern {
public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl();
/**
* 創建裝置到合適位置
*/
Tv bedRoomTV = new Tv("臥室");
Tv drawiTV = new Tv("客廳");
Lamp bedRoomLamp = new Lamp("臥室");
Lamp drawiLamp = new Lamp("客廳");
/**
* 創建所有命令操作對象
*/
//臥室燈關閉對象
LampOffCommand bedLampOffCommand = new LampOffCommand(bedRoomLamp);
//臥室燈打開對象
LampOnCommand bedLampOnCommand = new LampOnCommand(bedRoomLamp);
//臥室TV關閉對象
TvOffCommand bedTvOffCommand = new TvOffCommand(bedRoomTV);
//臥室TV打開對象
TvOnCommand bedTVcommand = new TvOnCommand(bedRoomTV);
//客廳燈打開對象
LampOnCommand drawLampOnCommand = new LampOnCommand(drawiLamp);
//客廳燈關閉對象
LampOffCommand drawLampOffCommand = new LampOffCommand(drawiLamp);
//客廳TV關閉對象
TvOffCommand drawTVOffCommand = new TvOffCommand(drawiTV);
//客廳TV打開對象
TvOnCommand drawTVOnCommand = new TvOnCommand(drawiTV);
System.out.println("---------------------------------------------未賦值之前------------------------------------------------");
System.out.println(remoteControl);
System.out.println("******************************************************************************************************");
/**
* //將操作對象與卡槽一一對應
*/
//賦值臥室燈打開與關閉
remoteControl.setCommond(0,bedLampOnCommand,bedLampOffCommand);
//賦值臥室TV打開與關閉
remoteControl.setCommond(1,bedTVcommand,bedTvOffCommand);
//賦值客廳燈打開與關閉
remoteControl.setCommond(2,drawLampOnCommand,drawLampOffCommand);
//賦值客廳TV打開與關閉
remoteControl.setCommond(3,drawTVOnCommand,drawTVOffCommand);
System.out.println("---------------------------------------------賦值之後------------------------------------------------");
System.out.println(remoteControl);
System.out.println("******************************************************************************************************");
/**
* 測試每一個按鈕
*/
remoteControl.clickOn(0);
remoteControl.clickOff(0);
//撤銷一次
remoteControl.toUpStepClick();
System.out.println("\r\n");
//撤銷一次
remoteControl.toUpStepClick();
System.out.println("\r\n");
remoteControl.clickOn(1);
remoteControl.clickOff(1);
//撤銷一次
remoteControl.toUpStepClick();
System.out.println("\r\n");
remoteControl.clickOn(2);
remoteControl.clickOff(2);
//撤銷一次
remoteControl.toUpStepClick();
System.out.println("\r\n");
remoteControl.clickOn(3);
remoteControl.clickOff(3);
//撤銷一次
remoteControl.toUpStepClick();
System.out.println("\r\n");
}
}
修改之後的測試結果
---------------------------------------------未賦值之前------------------------------------------------
[index : 0] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 1] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 2] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 3] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
[index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
******************************************************************************************************
---------------------------------------------賦值之後------------------------------------------------
[index : 0] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
[index : 1] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
[index : 2] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
[index : 3] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
[index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
******************************************************************************************************
臥室_燈打開
臥室_燈關閉
---撤銷---
臥室_燈打開
---撤銷---
臥室_燈打開
臥室_電視打開
臥室_電視關閉
---撤銷---
臥室_電視打開
客廳_燈打開
客廳_燈關閉
---撤銷---
客廳_燈打開
客廳_電視打開
客廳_電視關閉
---撤銷---
客廳_電視打開
項目組長阿飛:不錯,不錯,以後給你漲工資。