設(shè)計(jì)模式系列 — 命令模式

點(diǎn)贊再看,養(yǎng)成習(xí)慣,公眾號(hào)搜一搜【一角錢(qián)技術(shù)】關(guān)注更多原創(chuàng)技術(shù)文章。本文 GitHub org_hejianhui/JavaStudy 已收錄,有我的系列文章。

前言

23種設(shè)計(jì)模式快速記憶的請(qǐng)看上面第一篇,本篇和大家一起來(lái)學(xué)習(xí)命令模式相關(guān)內(nèi)容。

模式定義

將一個(gè)請(qǐng)求封裝為一個(gè)對(duì)象,使發(fā)出請(qǐng)求的責(zé)任和執(zhí)行請(qǐng)求的責(zé)任分割開(kāi)。這樣兩者之間通過(guò)命令對(duì)象進(jìn)行溝通,這樣方便將命令對(duì)象進(jìn)行儲(chǔ)存、傳遞、調(diào)用、增加與管理。

在軟件開(kāi)發(fā)系統(tǒng)中,常常出現(xiàn)“方法的請(qǐng)求者”與“方法的實(shí)現(xiàn)者”之間存在緊密的耦合關(guān)系。這不利于軟件功能的擴(kuò)展與維護(hù)。例如,想對(duì)行為進(jìn)行“撤銷(xiāo)、重做、記錄”等處理都很不方便,因此“如何將方法的請(qǐng)求者與方法的實(shí)現(xiàn)者解耦?”變得很重要,命令模式能很好地解決這個(gè)問(wèn)題。

模版實(shí)現(xiàn)如下

package com.niuh.designpattern.command.v1;

/**
 * <p>
 * 命令模式
 * </p>
 */
public class CommandPattern {
    public static void main(String[] args) {
        Command cmd = new ConcreteCommand();
        Invoker ir = new Invoker(cmd);
        System.out.println("客戶(hù)訪(fǎng)問(wèn)調(diào)用者的call()方法...");
        ir.call();
    }
}

//抽象命令
interface Command {
    public abstract void execute();
}

//具體命令
class ConcreteCommand implements Command {
    private Receiver receiver;

    ConcreteCommand() {
        receiver = new Receiver();
    }

    public void execute() {
        receiver.action();
    }
}

//接收者
class Receiver {
    public void action() {
        System.out.println("接收者的action()方法被調(diào)用...");
    }
}

//調(diào)用者
class Invoker {
    private Command command;

    public Invoker(Command command) {
        this.command = command;
    }

    public void setCommand(Command command) {
        this.command = command;
    }

    public void call() {
        System.out.println("調(diào)用者執(zhí)行命令command...");
        command.execute();
    }
}

輸出結(jié)果如下

客戶(hù)訪(fǎng)問(wèn)調(diào)用者的call()方法...
調(diào)用者執(zhí)行命令command...
接收者的action()方法被調(diào)用...

解決的問(wèn)題

在軟件系統(tǒng)中,行為請(qǐng)求者與行為實(shí)現(xiàn)者通常是一種緊耦合的關(guān)系,但某些場(chǎng)合,比如需要對(duì)行為進(jìn)行記錄、撤銷(xiāo)或重做、事務(wù)等處理時(shí),這種無(wú)法抵御變化的緊耦合的設(shè)計(jì)就不太合適。

模式組成

可以將系統(tǒng)中的相關(guān)操作抽象成命令,使調(diào)用者與實(shí)現(xiàn)者相關(guān)分離,其結(jié)構(gòu)如下。

組成(角色) 作用
抽象命令類(lèi)(Command)角色 聲明執(zhí)行命令的接口,擁有執(zhí)行命令的抽象方法 execute()。
具體命令角色(Concrete Command)角色 是抽象命令類(lèi)的具體實(shí)現(xiàn)類(lèi),它擁有接收者對(duì)象,并通過(guò)調(diào)用接收者的功能來(lái)完成命令要執(zhí)行的操作。
實(shí)現(xiàn)者/接收者(Receiver)角色 執(zhí)行命令功能的相關(guān)操作,是具體命令對(duì)象業(yè)務(wù)的真正實(shí)現(xiàn)者。
調(diào)用者/請(qǐng)求者(Invoker)角色 是請(qǐng)求的發(fā)送者,它通常擁有很多的命令對(duì)象,并通過(guò)訪(fǎng)問(wèn)命令對(duì)象來(lái)執(zhí)行相關(guān)請(qǐng)求,它不直接訪(fǎng)問(wèn)接收者。

實(shí)例說(shuō)明

實(shí)例概況

結(jié)合命令模式,實(shí)現(xiàn)一個(gè)課程視頻的打開(kāi)和關(guān)閉。


使用步驟

步驟1:聲明執(zhí)行命令的接口,擁有執(zhí)行命令的抽象方法 execute()

interface Command {
    void execute();
}

步驟2:定義具體命令角色,創(chuàng)建打開(kāi)課程鏈接 和 關(guān)閉課程連接

/**
 * 打開(kāi)課程鏈接
 */
class OpenCourseVideoCommand implements Command {

    private CourseVideo courseVideo;

    public OpenCourseVideoCommand(CourseVideo courseVideo) {
        this.courseVideo = courseVideo;
    }

    @Override
    public void execute() {
        courseVideo.open();
    }
}

/**
 * 關(guān)閉課程鏈接
 */
class CloseCourseVideoCommand implements Command {

    private CourseVideo courseVideo;

    public CloseCourseVideoCommand(CourseVideo courseVideo) {
        this.courseVideo = courseVideo;
    }

    @Override
    public void execute() {
        courseVideo.close();
    }
}

步驟3:定義接收者角色,執(zhí)行命令功能的相關(guān)操作,是具體命令對(duì)象業(yè)務(wù)的真正實(shí)現(xiàn)者

class CourseVideo {

    private String name;

    public CourseVideo(String name) {
        this.name = name;
    }

    public void open() {
        System.out.println(this.name + "課程視頻開(kāi)放。");
    }

    public void close() {
        System.out.println(this.name + "課程視頻關(guān)閉。");
    }
}

步驟4:創(chuàng)建User對(duì)象為請(qǐng)求的發(fā)送者,即請(qǐng)求者角色

class User {

    private List<Command> commands = new ArrayList<>();

    public void addCommand(Command command) {
        commands.add(command);
    }

    public void executeCommands() {
        commands.forEach(Command::execute);
        commands.clear();
    }
}

步驟4:測(cè)試執(zhí)行

public class CommandPattern {

    public static void main(String[] args) {
        //命令接收者
        CourseVideo courseVideo = new CourseVideo("設(shè)計(jì)模式系列");

        //創(chuàng)建命令
        OpenCourseVideoCommand openCourseVideoCommand = new OpenCourseVideoCommand(courseVideo);
        CloseCourseVideoCommand closeCourseVideoCommand = new CloseCourseVideoCommand(courseVideo);

        //創(chuàng)建執(zhí)行人
        User user = new User();

        //添加命令
        user.addCommand(openCourseVideoCommand);
        user.addCommand(closeCourseVideoCommand);

        //執(zhí)行
        user.executeCommands();
    }
}

輸出結(jié)果

設(shè)計(jì)模式系列課程視頻開(kāi)放。
設(shè)計(jì)模式系列課程視頻關(guān)閉。

優(yōu)點(diǎn)

  1. 降低系統(tǒng)的耦合度。命令模式能將調(diào)用操作的對(duì)象與實(shí)現(xiàn)該操作的對(duì)象解耦。
  2. 增加或刪除命令非常方便。采用命令模式增加與刪除命令不會(huì)影響其他類(lèi),它滿(mǎn)足“開(kāi)閉原則”,對(duì)擴(kuò)展比較靈活。
  3. 可以實(shí)現(xiàn)宏命令。命令模式可以與組合模式結(jié)合,將多個(gè)命令裝配成一個(gè)組合命令,即宏命令。
  4. 方便實(shí)現(xiàn) Undo 和 Redo 操作。命令模式可以與后面介紹的備忘錄模式結(jié)合,實(shí)現(xiàn)命令的撤銷(xiāo)與恢復(fù)。

缺點(diǎn)

可能產(chǎn)生大量具體命令類(lèi)。因?yàn)橛?jì)對(duì)每一個(gè)具體操作都需要設(shè)計(jì)一個(gè)具體命令類(lèi),這將增加系統(tǒng)的復(fù)雜性。

應(yīng)用場(chǎng)景

命令執(zhí)行過(guò)程較為復(fù)雜且可能存在變化,需要對(duì)執(zhí)行命令動(dòng)作本身進(jìn)行額外操作,此時(shí)可以考慮使用命令模式

命令模式的擴(kuò)展

在軟件開(kāi)發(fā)中,有時(shí)將命令模式與組合模式聯(lián)合使用,這就構(gòu)成了宏命令模式,也叫組合命令模式。宏命令包含了一組命令,它充當(dāng)了具體命令與調(diào)用者的雙重角色,執(zhí)行它時(shí)將遞歸調(diào)用它所包含的所有命令,其具體結(jié)構(gòu)圖如下:


模版實(shí)現(xiàn)如下

package com.niuh.designpattern.command.v2;

import java.util.ArrayList;

/**
 * <p>
 * 組合命令模式
 * </p>
 */
public class CompositeCommandPattern {
    public static void main(String[] args) {
        AbstractCommand cmd1 = new ConcreteCommand1();
        AbstractCommand cmd2 = new ConcreteCommand2();
        CompositeInvoker ir = new CompositeInvoker();
        ir.add(cmd1);
        ir.add(cmd2);
        System.out.println("客戶(hù)訪(fǎng)問(wèn)調(diào)用者的execute()方法...");
        ir.execute();
    }
}

//抽象命令
interface AbstractCommand {
    public abstract void execute();
}

//樹(shù)葉構(gòu)件: 具體命令1
class ConcreteCommand1 implements AbstractCommand {
    private CompositeReceiver receiver;

    ConcreteCommand1() {
        receiver = new CompositeReceiver();
    }

    public void execute() {
        receiver.action1();
    }
}

//樹(shù)葉構(gòu)件: 具體命令2
class ConcreteCommand2 implements AbstractCommand {
    private CompositeReceiver receiver;

    ConcreteCommand2() {
        receiver = new CompositeReceiver();
    }

    public void execute() {
        receiver.action2();
    }
}

//樹(shù)枝構(gòu)件: 調(diào)用者
class CompositeInvoker implements AbstractCommand {
    private ArrayList<AbstractCommand> children = new ArrayList<AbstractCommand>();

    public void add(AbstractCommand c) {
        children.add(c);
    }

    public void remove(AbstractCommand c) {
        children.remove(c);
    }

    public AbstractCommand getChild(int i) {
        return children.get(i);
    }

    public void execute() {
        for (Object obj : children) {
            ((AbstractCommand) obj).execute();
        }
    }
}

//接收者
class CompositeReceiver {
    public void action1() {
        System.out.println("接收者的action1()方法被調(diào)用...");
    }

    public void action2() {
        System.out.println("接收者的action2()方法被調(diào)用...");
    }
}

輸出結(jié)果如下

客戶(hù)訪(fǎng)問(wèn)調(diào)用者的execute()方法...
接收者的action1()方法被調(diào)用...
接收者的action2()方法被調(diào)用...

命令模式還可以同備忘錄(Memento)模式組合使用,這樣就變成了可撤銷(xiāo)的命令模式

源碼中的應(yīng)用

在 JdbcTemplate 中的應(yīng)用

在JdbcTemplate中命令模式的使用并沒(méi)有遵從標(biāo)準(zhǔn)的命令模式的使用,只是思想相同而已。

在 Spring 的 JdbcTemplate 這個(gè)類(lèi)中有 query() 方法,query() 方法中定義了一個(gè)內(nèi)部類(lèi) QueryStatementCallback,QueryStatementCallback 又實(shí)現(xiàn)了 StatementCallback 接口,另外還有其它類(lèi)實(shí)現(xiàn)了該接口,StatementCallback 接口中又有一個(gè)抽象方法 doInStatement()。在 execute() 中又調(diào)用了 query()。

StatementCallback充當(dāng)?shù)氖敲罱巧?code>JdbcTemplate即充當(dāng)調(diào)用者角色,又充當(dāng)接收者角色。上面的類(lèi)圖只是為了方便理解,實(shí)際上,QueryStatementCallback 與 ExecuteStatementCallback是JdbcTemplate中方法的內(nèi)部類(lèi),具體看源碼中的內(nèi)容。

部分源碼分析

StatementCallback接口:

public interface StatementCallback<T> {
    T doInStatement(Statement stmt) throws SQLException, DataAccessException;
}

JdbcTemplate類(lèi):

public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
    //相當(dāng)于調(diào)用者發(fā)布的一個(gè)命令
    @Override
    public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException {
        return query(sql, new RowMapperResultSetExtractor<T>(rowMapper));
    }
    //命令發(fā)布后由具體的命令派給接收者進(jìn)行執(zhí)行
    @Override
    public <T> T query(final String sql, final ResultSetExtractor<T> rse) throws DataAccessException {
        Assert.notNull(sql, "SQL must not be null");
        Assert.notNull(rse, "ResultSetExtractor must not be null");
        if (logger.isDebugEnabled()) {
            logger.debug("Executing SQL query [" + sql + "]");
        }
        //內(nèi)部類(lèi),實(shí)現(xiàn)StatementCallback,相當(dāng)于具體的命令
        class QueryStatementCallback implements StatementCallback<T>, SqlProvider {
            @Override
            public T doInStatement(Statement stmt) throws SQLException {
                ResultSet rs = null;
                try {
                    rs = stmt.executeQuery(sql);
                    ResultSet rsToUse = rs;
                    if (nativeJdbcExtractor != null) {
                        rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
                    }
                    return rse.extractData(rsToUse);
                }
                finally {
                    JdbcUtils.closeResultSet(rs);
                }
            }
            @Override
            public String getSql() {
                return sql;
            }
        }
        return execute(new QueryStatementCallback());
    }
    //相當(dāng)于接收者,命令真正的執(zhí)行者
    @Override
    public <T> T execute(StatementCallback<T> action) throws DataAccessException {
        Assert.notNull(action, "Callback object must not be null");
        Connection con = DataSourceUtils.getConnection(getDataSource());
        Statement stmt = null;
        try {
            Connection conToUse = con;
            if (this.nativeJdbcExtractor != null &&
                    this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
                conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
            }
            stmt = conToUse.createStatement();
            applyStatementSettings(stmt);
            Statement stmtToUse = stmt;
            if (this.nativeJdbcExtractor != null) {
                stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
            }
            T result = action.doInStatement(stmtToUse);
            handleWarnings(stmt);
            return result;
        }
        catch (SQLException ex) {
            // Release Connection early, to avoid potential connection pool deadlock
            // in the case when the exception translator hasn't been initialized yet.
            JdbcUtils.closeStatement(stmt);
            stmt = null;
            DataSourceUtils.releaseConnection(con, getDataSource());
            con = null;
            throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);
        }
        finally {
            JdbcUtils.closeStatement(stmt);
            DataSourceUtils.releaseConnection(con, getDataSource());
        }
    }
}

PS:以上代碼提交在 Githubhttps://github.com/Niuh-Study/niuh-designpatterns.git

文章持續(xù)更新,可以公眾號(hào)搜一搜「 一角錢(qián)技術(shù) 」第一時(shí)間閱讀, 本文 GitHub org_hejianhui/JavaStudy 已經(jīng)收錄,歡迎 Star。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容