Java描述设计模式(20):命令模式
本文源碼:GitHub·點這里 || GitEE·點這里
一、生活場景
1、場景描述
智能電腦的品牌越來越多,由此誕生了一款電腦控制的APP,萬能遙控器,用戶在使用遙控器的時候,可以切換為自家電視的品牌,然后對電視進行控制。
2、代碼實現(xiàn)
public class C01_InScene {public static void main(String[] args) {TVClient tvClient = new TVClient() ;Remote remote = new RemoteApp(tvClient) ;UserClient userClient = new UserClient(remote) ;userClient.action("HM","換臺");} } /*** 遙控接口*/ interface Remote {void controlTV (String tvType,String task); } /*** 遙控器APP*/ class RemoteApp implements Remote {private TVClient tvClient = null ;public RemoteApp (TVClient tvClient){this.tvClient = tvClient ;}@Overridepublic void controlTV(String tvType, String task) {tvClient.action(tvType,task);} } /*** 用戶端*/ class UserClient {// 持有遙控器private Remote remote = null ;public UserClient (Remote remote){this.remote = remote ;}public void action (String tvType, String task){remote.controlTV(tvType,task);} } /*** 電視端*/ class TVClient {public void action (String tvType, String task){System.out.println("TV品牌:"+tvType+";執(zhí)行:"+task);} }二、命令模式
1、基礎概念
命令模式屬于對象的行為模式。命令模式把一個請求或者操作封裝到一個對象中。把發(fā)出命令的動作和執(zhí)行命令的動作分割開,委派給不同的對象。命令模式允許請求的一方和接收的一方獨立開來,使得請求的一方不必知道接收請求的一方的接口,更不必知道請求是怎么被接收,以及操作是否被執(zhí)行。
2、模式圖解
3、核心角色
- 命令角色
聲明所有具體命令類的抽象接口。
- 具體命令角色
定義接收者和行為之間的交互方式:實現(xiàn)execute()方法,調用接收者的相應操作 , 傳遞命令信息。
- 請求者角色
負責調用命令對象執(zhí)行請求,相關的方法叫做行動方法。
- 接收者角色
執(zhí)行請求。任何一個類都可以成為接收者,執(zhí)行請求的方法叫做行動方法。
4、源碼實現(xiàn)
public class C02_Command {public static void main(String[] args) {Receiver receiver = new Receiver();Command command = new ConcreteCommand(receiver);Invoker invoker = new Invoker(command);invoker.action("臥倒");} } /*** 命令角色*/ interface Command {// 執(zhí)行方法void execute(String task); } /*** 具體命令角色類*/ class ConcreteCommand implements Command {//持有相應的接收者對象private Receiver receiver = null;public ConcreteCommand(Receiver receiver){this.receiver = receiver;}@Overridepublic void execute(String task) {//接收方來真正執(zhí)行請求receiver.action(task);} } /*** 請求者角色類*/ class Invoker {// 持有命令對象private Command command = null;public Invoker(Command command){this.command = command;}// 行動方法public void action(String task){command.execute(task);} } /*** 接收者角色類*/ class Receiver {// 執(zhí)行命令操作public void action(String task){System.out.println("執(zhí)行命令:"+task);} }三、Spring框架應用
Spring框架中封裝的JdbcTemplate類API使用到了命令模式。
1、JdbcOperations接口
public interface JdbcOperations {@Nullable<T> T execute(StatementCallback<T> var1) ; }2、JdbcTemplate類
這里只保留模式方法的代碼。
public class JdbcTemplate implements JdbcOperations {@Nullablepublic <T> T execute(StatementCallback<T> action) {try {T result = action.doInStatement(stmt);} catch (SQLException var9) {} finally {}} }3、StatementCallback接口
@FunctionalInterface public interface StatementCallback<T> {@NullableT doInStatement(Statement var1) ; }四、命令模式總結
- 松散的耦合
命令模式使得命令發(fā)起者和命令執(zhí)行者解耦,發(fā)起命令的對象完全不知道具體實現(xiàn)對象是誰。這和常見的MQ消息隊列原理是類似的。
- 動態(tài)的控制
命令模式把請求封裝起來,可以動態(tài)地對它進行參數(shù)化、隊列化和等操作和管理,使系統(tǒng)更加的靈活。
- 良好的擴展性
命令發(fā)起者和命令執(zhí)行者實現(xiàn)完全解耦,因此擴展添加新命令很容易。
五、源代碼地址
GitHub·地址 https://github.com/cicadasmile/model-arithmetic-parent GitEE·地址 https://gitee.com/cicadasmile/model-arithmetic-parent總結
以上是生活随笔為你收集整理的Java描述设计模式(20):命令模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSF 源代码赏析之FacesServl
- 下一篇: POJ 3069 Saruman's A