描述
????命令模式(Command Pattern)是一種數(shù)據(jù)驅(qū)動(dòng)的設(shè)計(jì)模式,它屬于行為型模式。請(qǐng)求以命令的形式包裹在對(duì)象中,并傳給調(diào)用對(duì)象。調(diào)用對(duì)象尋找可以處理該命令的合適的對(duì)象,并把該命令傳給相應(yīng)的對(duì)象,該對(duì)象執(zhí)行命令。
簡(jiǎn)介
????命令模式是對(duì)命令的封裝。命令模式把發(fā)出命令的責(zé)任和執(zhí)行命令的責(zé)任分割開(kāi),委派給不同的對(duì)象。
????每一個(gè)命令都是一個(gè)操作:請(qǐng)求的一方發(fā)出請(qǐng)求要求執(zhí)行一個(gè)操作;接收的一方收到請(qǐng)求,并執(zhí)行操作。命令模式允許請(qǐng)求的一方和接收的一方獨(dú)立開(kāi)來(lái),使得請(qǐng)求的一方不必知道接收請(qǐng)求的一方的接口,更不必知道請(qǐng)求是怎么被接收,以及操作是否被執(zhí)行、何時(shí)被執(zhí)行,以及是怎么被執(zhí)行的。

命令模式類圖
角色
- 客戶端(Client)角色:創(chuàng)建一個(gè)具體命令(ConcreteCommand)對(duì)象并確定其接收者。
- 命令(Command)角色:聲明了一個(gè)給所有具體命令類的抽象接口。
- 具體命令(ConcreteCommand)角色:定義一個(gè)接收者和行為之間的弱耦合;實(shí)現(xiàn)execute()方法,負(fù)責(zé)調(diào)用接收者的相應(yīng)操作。execute()方法通常叫做執(zhí)行方法。
- 請(qǐng)求者(Invoker)角色:負(fù)責(zé)調(diào)用命令對(duì)象執(zhí)行請(qǐng)求,相關(guān)的方法叫做行動(dòng)方法。
- 接收者(Receiver)角色:負(fù)責(zé)具體實(shí)施和執(zhí)行一個(gè)請(qǐng)求。任何一個(gè)類都可以成為接收者,實(shí)施和執(zhí)行請(qǐng)求的方法叫做行動(dòng)方法。
優(yōu)缺點(diǎn)
優(yōu)點(diǎn)
- 降低系統(tǒng)的耦合度。命令模式能將調(diào)用操作的對(duì)象與實(shí)現(xiàn)該操作的對(duì)象解耦。
- 增加或刪除命令非常方便。采用命令模式增加與刪除命令不會(huì)影響其他類,它滿足“開(kāi)閉原則”,對(duì)擴(kuò)展比較靈活。
- 可以實(shí)現(xiàn)宏命令。命令模式中的命令對(duì)象能夠很容易地組合成復(fù)合命令,也就是宏命令,從而使系統(tǒng)操作更簡(jiǎn)單,功能更強(qiáng)大。
缺點(diǎn)
- 可能產(chǎn)生大量具體命令類。因?yàn)橛?jì)對(duì)每一個(gè)具體操作都需要設(shè)計(jì)一個(gè)具體命令類,這將增加系統(tǒng)的復(fù)雜性。
使用場(chǎng)景
- 當(dāng)系統(tǒng)需要將請(qǐng)求調(diào)用者與請(qǐng)求接收者解耦時(shí),命令模式使得調(diào)用者和接收者不直接交互。
- 當(dāng)系統(tǒng)需要隨機(jī)請(qǐng)求命令或經(jīng)常增加或刪除命令時(shí),命令模式比較方便實(shí)現(xiàn)這些功能。
- 當(dāng)系統(tǒng)需要執(zhí)行一組操作時(shí),命令模式可以定義宏命令來(lái)實(shí)現(xiàn)該功能。
示例
/**
* 接收者角色
**/
public class Receiver {
public void operation(){
System.out.println("執(zhí)行操作!");
}
}
/**
* 抽象命令角色
**/
public interface Command {
/**
* 執(zhí)行方法
*/
void execute();
}
/**
* 具體命令角色
**/
public class ConcreteCommand implements Command {
private Receiver receiver;
public ConcreteCommand(Receiver receiver) {
this.receiver = receiver;
}
/**
* 執(zhí)行方法
*/
@Override
public void execute() {
receiver.operation();
}
}
/**
* 請(qǐng)求者角色
**/
public class Invoker {
private Command command;
public Invoker(Command command) {
this.command = command;
}
public void operation() {
command.execute();
}
}
/**
* 客戶端角色
**/
public class Client {
public static void main(String[] args) {
//創(chuàng)建接收者
Receiver receiver = new Receiver();
//創(chuàng)建命令對(duì)象,設(shè)定它的接收者
Command command = new ConcreteCommand(receiver);
//創(chuàng)建請(qǐng)求者,把命令對(duì)象設(shè)置進(jìn)去
Invoker invoker = new Invoker(command);
//執(zhí)行方法
invoker.operation();
}
}
播放視頻的示例
/**
* 接收者角色
**/
public class VideoPlayer {
public void play() {
System.out.println("播放");
}
public void stop() {
System.out.println("暫停");
}
public void fastForward() {
System.out.println("快進(jìn)");
}
}
/**
* 抽象命令角色
**/
public interface Command {
/**
* 執(zhí)行方法
*/
void execute();
}
/**
* 具體命令角色
**/
public class PlayCommand implements Command {
private VideoPlayer videoPlayer;
public PlayCommand(VideoPlayer videoPlayer) {
this.videoPlayer = videoPlayer;
}
/**
* 執(zhí)行方法
*/
@Override
public void execute() {
videoPlayer.play();
}
}
/**
* 具體命令角色
**/
public class StopCommand implements Command {
private VideoPlayer videoPlayer;
public StopCommand(VideoPlayer videoPlayer) {
this.videoPlayer = videoPlayer;
}
/**
* 執(zhí)行方法
*/
@Override
public void execute() {
videoPlayer.stop();
}
}
/**
* 具體命令角色
**/
public class FastForwardCommand implements Command {
private VideoPlayer videoPlayer;
public FastForwardCommand(VideoPlayer videoPlayer) {
this.videoPlayer = videoPlayer;
}
/**
* 執(zhí)行方法
*/
@Override
public void execute() {
videoPlayer.fastForward();
}
}
/**
* 請(qǐng)求者角色
**/
public class Mouse {
private Command playCommand;
private Command stopCommand;
private Command fastForwardCommand;
public void play() {
playCommand.execute();
}
public void stop() {
stopCommand.execute();
}
public void fastForward() {
fastForwardCommand.execute();
}
public Command getPlayCommand() {
return playCommand;
}
public void setPlayCommand(Command playCommand) {
this.playCommand = playCommand;
}
public Command getStopCommand() {
return stopCommand;
}
public void setStopCommand(Command stopCommand) {
this.stopCommand = stopCommand;
}
public Command getFastForwardCommand() {
return fastForwardCommand;
}
public void setFastForwardCommand(Command fastForwardCommand) {
this.fastForwardCommand = fastForwardCommand;
}
}
/**
* 客戶端角色
**/
public class Client {
public static void main(String[] args) {
VideoPlayer videoPlayer = new VideoPlayer();
Command playCommand = new PlayCommand(videoPlayer);
Command stopCommand = new StopCommand(videoPlayer);
Command fastForwardCommand = new FastForwardCommand(videoPlayer);
Mouse mouse = new Mouse();
mouse.setPlayCommand(playCommand);
mouse.setStopCommand(stopCommand);
mouse.setFastForwardCommand(fastForwardCommand);
mouse.play();
mouse.stop();
mouse.fastForward();
}
}