天天看點

Java描述設計模式(20):指令模式一、生活場景二、指令模式三、Spring架構應用四、指令模式總結五、源代碼位址

本文源碼: GitHub·點這裡 || GitEE·點這裡

一、生活場景

1、場景描述

智能電腦的品牌越來越多,由此誕生了一款電腦控制的APP,萬能遙控器,使用者在使用遙控器的時候,可以切換為自家電視的品牌,然後對電視進行控制。

2、代碼實作

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 ;
    }
    @Override
    public 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+";執行:"+task);
    }
}           

二、指令模式

1、基礎概念

指令模式屬于對象的行為模式。指令模式把一個請求或者操作封裝到一個對象中。把發出指令的動作和執行指令的動作分割開,委派給不同的對象。指令模式允許請求的一方和接收的一方獨立開來,使得請求的一方不必知道接收請求的一方的接口,更不必知道請求是怎麼被接收,以及操作是否被執行。

2、模式圖解

Java描述設計模式(20):指令模式一、生活場景二、指令模式三、Spring架構應用四、指令模式總結五、源代碼位址

3、核心角色

  • 指令角色
聲明所有具體指令類的抽象接口。
  • 具體指令角色
定義接收者和行為之間的互動方式:實作execute()方法,調用接收者的相應操作 , 傳遞指令資訊。
  • 請求者角色
負責調用指令對象執行請求,相關的方法叫做行動方法。
  • 接收者角色
執行請求。任何一個類都可以成為接收者,執行請求的方法叫做行動方法。

4、源碼實作

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 {
    // 執行方法
    void execute(String task);
}
/**
 * 具體指令角色類
 */
class ConcreteCommand implements Command {
    //持有相應的接收者對象
    private Receiver receiver = null;
    public ConcreteCommand(Receiver receiver){
        this.receiver = receiver;
    }
    @Override
    public void execute(String task) {
        //接收方來真正執行請求
        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 {
    // 執行指令操作
    public void action(String task){
        System.out.println("執行指令:"+task);
    }
}           

三、Spring架構應用

Spring架構中封裝的JdbcTemplate類API使用到了指令模式。

1、JdbcOperations接口

public interface JdbcOperations {
    @Nullable
    <T> T execute(StatementCallback<T> var1) ;
}           

2、JdbcTemplate類

這裡隻保留模式方法的代碼。

public class JdbcTemplate implements JdbcOperations {
    @Nullable
    public <T> T execute(StatementCallback<T> action) {
        try {
            T result = action.doInStatement(stmt);
        } catch (SQLException var9) {
        } finally {
        }
    }
}           

3、StatementCallback接口

@FunctionalInterface
public interface StatementCallback<T> {
    @Nullable
    T doInStatement(Statement var1) ;
}           

四、指令模式總結

  • 松散的耦合
指令模式使得指令發起者和指令執行者解耦,發起指令的對象完全不知道具體實作對象是誰。這和常見的MQ消息隊列原理是類似的。
  • 動态的控制
指令模式把請求封裝起來,可以動态地對它進行參數化、隊列化和等操作和管理,使系統更加的靈活。
  • 良好的擴充性
指令發起者和指令執行者實作完全解耦,是以擴充添加新指令很容易。

五、源代碼位址

GitHub·位址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·位址
https://gitee.com/cicadasmile/model-arithmetic-parent