Android 架構師之路11 設計模式之命令模式

Android 架構師之路 目錄

1、命令模式概念

1.1 介紹

命令模式(Command Pattern)是一種數據驅動的設計模式,它屬于行為型模式。請求以命令的形式包裹在對象中,并傳給調用對象。調用對象尋找可以處理該命令的合適的對象,并把該命令傳給相應的對象,該對象執行命令。

1.2 定義

將一個請求封裝為一個對象,從而使你可用不同的請求對客戶進行參數化,對請求排隊或記錄請求日志。以及支持可撤銷的操作。

1.3 使用場景
  • 系統需要將請求調用者和請求接收者解耦,使得調用者和接收者不直接交互。
  • 系統需要在不同的時間指定請求、將請求排隊(如:線程池+工作隊列)和執行請求。
  • 系統需要支持命令的撤銷(Undo)操作和恢復(Redo)操作。
  • 系統需要將一組操作組合在一起,即支持宏命令。

2、命令模式UML類圖

命令模式UML類圖
  • Command(抽象命令類):抽象出命令對象,可以根據不同的命令類型。寫出不同的實現類

  • ConcreteCommand(具體命令類):實現了抽象命令對象的具體實現

  • Invoker(調用者/請求者):請求的發送者,它通過命令對象來執行請求。一個調用者并不需要在設計時確定其接收者,因此它只與抽象命令來之間存在關聯。在程序運行時,將調用命令對象的execute() ,間接調用接收者的相關操作。

  • Receiver(接收者):接收者執行與請求相關的操作,真正執行命令的對象。具體實現對請求的業務處理。未抽象前,實際執行操作內容的對象。

  • Client(客戶端):在客戶類中需要創建調用者對象,具體命令類對象,在創建具體命令對象時指定對應的接收者。發送者和接收者之間沒有之間關系,都通過命令對象來調用。

3、命令模式代碼實現

俄羅斯方塊游戲
向下方塊、向右方塊、向左方塊...,每一個方向都是一個命令

Command:
public interface ICommand extends Serializable {
    void execute();
}
ConcreteCommand:
public class LeftCommand implements ICommand {
    private Receiver receiver;
    public LeftCommand(Receiver receiver) {
        this.receiver = receiver;
    }
    @Override
    public void execute() {
        /**
         * 執行之前干一些事情
         *  例如 存檔
         */
        this.receiver.onLeft();
    }
}


public class RightCommand implements ICommand {
    private Receiver receiver;
    public RightCommand(Receiver receiver) {
        this.receiver = receiver;
    }
    @Override
    public void execute() {
        this.receiver.onRight();
    }

}



public class BottomCommand implements ICommand {
    private Receiver receiver;
    public BottomCommand(Receiver receiver) {
        this.receiver = receiver;
    }
    @Override
    public void execute() {
        this.receiver.onBottom();
    }
}


public class TransfromCommand implements ICommand {
    private Receiver receiver;

    public TransfromCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void execute() {
        this.receiver.onTransformation();
    }


}
Invoker:
public class Invoker {
    private ICommand leftCommand;
    private ICommand rightCommand;
    private ICommand bottomCommand;
    private ICommand transfromCommand;
    private List<ICommand> commandList = new ArrayList<>();

    public Invoker() {
    }

    public Invoker(ICommand leftCommand, ICommand rightCommand, ICommand bottomCommand, ICommand transfromCommand) {
        this.leftCommand = leftCommand;
        this.rightCommand = rightCommand;
        this.bottomCommand = bottomCommand;
        this.transfromCommand = transfromCommand;
    }

    public void toLeft() {
        this.leftCommand.execute();
        commandList.add(leftCommand);
    }

    public void toRight() {
        this.rightCommand.execute();
        commandList.add(rightCommand);
    }

    public void toBottom() {
        this.bottomCommand.execute();
        commandList.add(bottomCommand);
    }

    public void toTransfrom() {
        this.transfromCommand.execute();
        commandList.add(transfromCommand);
    }

    /**
     * 回退
     */
    public void fallback() {
        if (commandList.size() > 0) {
            commandList.remove(commandList.size() - 1);
        }
    }

    /**
     * 存檔
     */
    public void saveArchive() {
        Utils.serializable("gameOperation", commandList);
    }

    /**
     * 讀檔
     */
    public void loadArchive() {
        List<ICommand> list = Utils.deserialize("gameOperation");
        this.commandList.clear();
        this.commandList.addAll(list);
        for (ICommand command : list) {
            command.execute();
        }
    }
}
Receiver:
public class Receiver implements Serializable {

    public void onLeft() {
        System.out.println("向左");
    }

    public void onRight() {
        System.out.println("向右");
    }

    public void onBottom() {
        System.out.println("向下");
    }

    public void onTransformation() {
        System.out.println("變形");
    }
}
存檔、讀檔工具類:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;


public class Utils {
    /**
     * 序列化訂單對象
     */
    public static void serializable(String name,
                                    List<ICommand> commandList) {
        // 序列化對象的流
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(
                    new File("C:\\Users\\Administrator\\Desktop\\" + name + ".txt")));
            outputStream.writeObject(commandList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<ICommand> deserialize(String name) {
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(
                    new File("C:\\Users\\Administrator\\Desktop\\"
                            + name + ".txt")));
            Object readObject = objectInputStream.readObject();
            return (List<ICommand>) readObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
Client :
public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        /**
         * 接收者
         */
        Receiver receiver = new Receiver();

        // 命令對象
        ICommand leftCommand = new LeftCommand(receiver);
        ICommand rightCommand = new RightCommand(receiver);
        ICommand bottomCommand = new BottomCommand(receiver);
        ICommand transfromCommand = new TransfromCommand(receiver);

        //請求者
        Invoker invoker = new Invoker(leftCommand, rightCommand, bottomCommand, transfromCommand);
        invoker.toLeft();
        invoker.toRight();
        invoker.toBottom();
        invoker.toTransfrom();

        //序列化存檔
        System.out.println("----存檔----");
        invoker.saveArchive();

        invoker.toBottom();


        System.out.println("----讀檔----");
        //讀檔
        invoker.loadArchive();
    }
}

結果輸出:

向左
向右
向下
變形
----存檔----
向下
----讀檔----
向左
向右
向下
變形

4、命令模式Android中使用

IHttpCommand相當于命令接口Command:
/**
 * 網絡請求命令接口
 *
 * 以前采用Map集合傳入, 現在面向接口編程
 * Created by Xionghu on 2017/7/4.
 * Desc:
 */

public interface IHttpCommand<T extends IRequestParam> {
    public enum RequestType {
        Default(0), Get(1), Post(2), Delete(3);
        private int type;

        private RequestType(int type) {
            this.type = type;
        }

        public int getType() {
            return type;
        }
    }

    public String execute(Context context, String url, RequestType requestType,
                          T requestParam);
}
SystemHttpCommand 相當于具體命令實現ConcreteCommand:.
OKHttpCommand 省略
public class SystemHttpCommand extends AbsHttpCommand<SystemRequestParam> {
    @Override
    public String executePost(Context context, String url, SystemRequestParam requestParam) {
        //發送請求
        try {
            return HttpUtils.post(url, requestParam.getRequestParam());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String executeGet(Context context, String url, SystemRequestParam requestParam) {
        try {
            return HttpUtils.get(url,requestParam.getRequestParam());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
HttpUtils 相當于接收者Receiver:
public class HttpUtils {

    public static String get(String urlStr, Map<String, Object> paramMap)
            throws Exception {
        // 拼接參數
        StringBuilder params = new StringBuilder(urlStr + "?");
        int i = 0;
        for (String key : paramMap.keySet()) {
            Object value = paramMap.get(key);
            params.append(key);
            params.append("=");
            params.append(value);
            if (i < paramMap.size() - 1) {
                params.append("&");
            }
            i++;
        }
        return get(params.toString());
    }

    public static String get(String urlStr) {
        String result = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setReadTimeout(5000);
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            if (connection.getResponseCode() == 200) {
                InputStream inStream = connection.getInputStream();
                result = new String(StreamTool.readInputStream(inStream));
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String post(String urlStr, String username, String password)
            throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("username", username);
        paramMap.put("password", password);
        return post(urlStr, paramMap);
    }

    public static String post(String urlStr, Map<String, Object> paramMap)
            throws Exception {
        StringBuffer sb = null;
        // 拼接參數
        StringBuilder params = new StringBuilder();
        int i = 0;
        for (String key : paramMap.keySet()) {
            Object value = paramMap.get(key);
            params.append(key);
            params.append("=");
            params.append(value);
            if (i < paramMap.size() - 1) {
                params.append("&");
            }
            i++;
        }
        // 創建請求地址
        URL url = new URL(urlStr);
        // 打開連接
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        // 設置參數
        httpConn.setDoOutput(true); // 需要輸出
        httpConn.setDoInput(true); // 需要輸入
        httpConn.setUseCaches(false); // 不允許緩存
        httpConn.setRequestMethod("POST"); // 設置POST方式連接
        // 設置請求屬性
        httpConn.setRequestProperty("Charset", "UTF-8");
        // 連接,也可以不用明文connect,使用下面的httpConn.getOutputStream()會自動connect
        httpConn.connect();
        // 建立輸入流,向指向的URL傳入參數
        DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
        dos.writeBytes(params.toString());
        dos.flush();
        dos.close();
        // 獲得響應狀態
        int resultCode = httpConn.getResponseCode();
        sb = new StringBuffer();
        if (HttpURLConnection.HTTP_OK == resultCode) {
            // 解析服務器返回的數據
            String readLine = new String();
            BufferedReader responseReader = new BufferedReader(
                    new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
            while ((readLine = responseReader.readLine()) != null) {
                sb.append(readLine).append("\n");
            }
            responseReader.close();
            return sb.toString();
        }
        return null;
    }

    public interface OnHttpResultListener {
        public void onResult(String result);
    }
}
HttpTask 相當于請求者Invoker

public class HttpTask extends AsyncTask<String, Void, String> {

    private HttpTask.Builder.Params p;

    protected HttpTask(HttpTask.Builder.Params p) {
        this.p = p;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected String doInBackground(String... params) {
        try {
            // 執行命令
            return this.p.httpCommand.execute(this.p.context, this.p.url,
                    this.p.requestType, this.p.requestParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (this.p.onHttpResultListener != null) {
            this.p.onHttpResultListener.onResult(result);
        }
    }

    public void builder() {
        execute();
    }

    // 采用Builder設計模式
    public static class Builder {

        private Params p;

        public Builder(Context context, String url,
                       HttpUtils.OnHttpResultListener onHttpResultListener) {
            this.p = new Params(context, url, onHttpResultListener);
        }

        public Builder setRequestType(IHttpCommand.RequestType requestType) {
            this.p.requestType = requestType;
            return this;
        }

        public Builder setRequestParam(IRequestParam requestParam) {
            this.p.requestParam = requestParam;
            return this;
        }

        public Builder setHttpCommand(IHttpCommand httpCommand) {
            this.p.httpCommand = httpCommand;
            return this;
        }

        public HttpTask build() {
            return new HttpTask(p);
        }

        public static class Params {
            public Context context;
            public IHttpCommand.RequestType requestType;
            public String url;
            public IRequestParam requestParam;
            public HttpUtils.OnHttpResultListener onHttpResultListener;
            public IHttpCommand httpCommand;

            public Params(Context context, String url,
                          HttpUtils.OnHttpResultListener onHttpResultListener) {
                this.context = context;
                this.url = url;
                this.requestType = IHttpCommand.RequestType.Get;
                this.httpCommand = new SystemHttpCommand();
                this.requestParam = new SystemRequestParam();
                this.onHttpResultListener = onHttpResultListener;
            }
        }

    }

}
Client :
        //請求者
        HttpTask.Builder builder = new HttpTask.Builder(this, "", new HttpUtils.OnHttpResultListener() {
            @Override
            public void onResult(String result) {

            }
        });

        IRequestParam requestParam = new SystemRequestParam();
        requestParam.put("","");
        builder.setRequestParam(requestParam)
               .setRequestType(IHttpCommand.RequestType.Post)
               .build()
               .builder();

      //請求者
        HttpTask.Builder builder = new HttpTask.Builder(this, "", new HttpUtils.OnHttpResultListener() {
            @Override
            public void onResult(String result) {

            }
        });
        IRequestParam requestParam = new OKHttpRequestParam();
        requestParam.put("","");
        builder.setRequestParam(requestParam)
                .setHttpCommand(new OKHttpCommand())
                .setRequestType(IHttpCommand.RequestType.Post).build().builder();

整個網絡請求架構采用了命令模式
使我們得程序擴展性更加好,耦合降低了(比如 添加setHttpCommand 設置okhttp請求很方便)

5、模式總結

5.1 優點
  • 解除了請求者與實現者之間的耦合,降低了系統的耦合度。

  • 對請求排隊或記錄請求日志,支持撤銷操作。

  • 可以容易地設計一個組合命令。

  • 新命令可以容易地加入到系統中。

5.2 缺點
  • 因為針對每一個命令都需要設計一個具體命令類,使用命令模式可能會導致系統有過多的具體命令類。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容