點(diǎn)贊再看,養(yǎng)成習(xí)慣肮疗,公眾號搜一搜【一角錢技術(shù)】關(guān)注更多原創(chuàng)技術(shù)文章额划。本文 GitHub org_hejianhui/JavaStudy 已收錄,有我的系列文章。
前言
- 23種設(shè)計(jì)模式速記
- 單例(singleton)模式
- 工廠方法(factory method)模式
- 抽象工廠(abstract factory)模式
- 建造者/構(gòu)建器(builder)模式
- 原型(prototype)模式
- 享元(flyweight)模式
- 外觀(facade)模式
- 適配器(adapter)模式
- 裝飾(decorator)模式
- 觀察者(observer)模式
- 策略(strategy)模式
- 橋接(bridge)模式
- 模版方法(template method)模式
- 責(zé)任鏈(chain of responsibility)模式
- 組合(composite)模式
- 代理(proxy)模式
- 備忘錄(memento)模式
- 持續(xù)更新中......
23種設(shè)計(jì)模式快速記憶的請看上面第一篇效拭,本篇和大家一起來學(xué)習(xí)命令模式相關(guān)內(nèi)容。
模式定義
將一個(gè)請求封裝為一個(gè)對象胖秒,使發(fā)出請求的責(zé)任和執(zhí)行請求的責(zé)任分割開缎患。這樣兩者之間通過命令對象進(jìn)行溝通,這樣方便將命令對象進(jìn)行儲存阎肝、傳遞挤渔、調(diào)用、增加與管理风题。
在軟件開發(fā)系統(tǒng)中判导,常常出現(xiàn)“方法的請求者”與“方法的實(shí)現(xiàn)者”之間存在緊密的耦合關(guān)系。這不利于軟件功能的擴(kuò)展與維護(hù)沛硅。例如眼刃,想對行為進(jìn)行“撤銷、重做摇肌、記錄”等處理都很不方便擂红,因此“如何將方法的請求者與方法的實(shí)現(xiàn)者解耦?”變得很重要围小,命令模式能很好地解決這個(gè)問題昵骤。
模版實(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("客戶訪問調(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é)果如下:
客戶訪問調(diào)用者的call()方法...
調(diào)用者執(zhí)行命令command...
接收者的action()方法被調(diào)用...
解決的問題
在軟件系統(tǒng)中树碱,行為請求者與行為實(shí)現(xiàn)者通常是一種緊耦合的關(guān)系,但某些場合涉茧,比如需要對行為進(jìn)行記錄赴恨、撤銷或重做、事務(wù)等處理時(shí)伴栓,這種無法抵御變化的緊耦合的設(shè)計(jì)就不太合適伦连。
模式組成
可以將系統(tǒng)中的相關(guān)操作抽象成命令,使調(diào)用者與實(shí)現(xiàn)者相關(guān)分離钳垮,其結(jié)構(gòu)如下惑淳。
組成(角色) | 作用 |
---|---|
抽象命令類(Command)角色 | 聲明執(zhí)行命令的接口,擁有執(zhí)行命令的抽象方法 execute()饺窿。 |
具體命令角色(Concrete Command)角色 | 是抽象命令類的具體實(shí)現(xiàn)類歧焦,它擁有接收者對象,并通過調(diào)用接收者的功能來完成命令要執(zhí)行的操作肚医。 |
實(shí)現(xiàn)者/接收者(Receiver)角色 | 執(zhí)行命令功能的相關(guān)操作绢馍,是具體命令對象業(yè)務(wù)的真正實(shí)現(xiàn)者。 |
調(diào)用者/請求者(Invoker)角色 | 是請求的發(fā)送者肠套,它通常擁有很多的命令對象舰涌,并通過訪問命令對象來執(zhí)行相關(guān)請求,它不直接訪問接收者你稚。 |
實(shí)例說明
實(shí)例概況
結(jié)合命令模式瓷耙,實(shí)現(xiàn)一個(gè)課程視頻的打開和關(guān)閉。
使用步驟
步驟1:聲明執(zhí)行命令的接口刁赖,擁有執(zhí)行命令的抽象方法 execute()
interface Command {
void execute();
}
步驟2:定義具體命令角色搁痛,創(chuàng)建打開課程鏈接 和 關(guān)閉課程連接
/**
* 打開課程鏈接
*/
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)操作宇弛,是具體命令對象業(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 + "課程視頻開放鸡典。");
}
public void close() {
System.out.println(this.name + "課程視頻關(guān)閉。");
}
}
步驟4:創(chuàng)建User對象為請求的發(fā)送者枪芒,即請求者角色
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:測試執(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ì)模式系列課程視頻開放彻况。
設(shè)計(jì)模式系列課程視頻關(guān)閉。
優(yōu)點(diǎn)
- 降低系統(tǒng)的耦合度病苗。命令模式能將調(diào)用操作的對象與實(shí)現(xiàn)該操作的對象解耦疗垛。
- 增加或刪除命令非常方便。采用命令模式增加與刪除命令不會影響其他類硫朦,它滿足“開閉原則”贷腕,對擴(kuò)展比較靈活。
- 可以實(shí)現(xiàn)宏命令。命令模式可以與組合模式結(jié)合泽裳,將多個(gè)命令裝配成一個(gè)組合命令瞒斩,即宏命令。
- 方便實(shí)現(xiàn) Undo 和 Redo 操作涮总。命令模式可以與后面介紹的備忘錄模式結(jié)合胸囱,實(shí)現(xiàn)命令的撤銷與恢復(fù)。
缺點(diǎn)
可能產(chǎn)生大量具體命令類瀑梗。因?yàn)橛?jì)對每一個(gè)具體操作都需要設(shè)計(jì)一個(gè)具體命令類烹笔,這將增加系統(tǒng)的復(fù)雜性。
應(yīng)用場景
命令執(zhí)行過程較為復(fù)雜且可能存在變化抛丽,需要對執(zhí)行命令動(dòng)作本身進(jìn)行額外操作谤职,此時(shí)可以考慮使用命令模式
命令模式的擴(kuò)展
在軟件開發(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("客戶訪問調(diào)用者的execute()方法...");
ir.execute();
}
}
//抽象命令
interface AbstractCommand {
public abstract void execute();
}
//樹葉構(gòu)件: 具體命令1
class ConcreteCommand1 implements AbstractCommand {
private CompositeReceiver receiver;
ConcreteCommand1() {
receiver = new CompositeReceiver();
}
public void execute() {
receiver.action1();
}
}
//樹葉構(gòu)件: 具體命令2
class ConcreteCommand2 implements AbstractCommand {
private CompositeReceiver receiver;
ConcreteCommand2() {
receiver = new CompositeReceiver();
}
public void execute() {
receiver.action2();
}
}
//樹枝構(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é)果如下:
客戶訪問調(diào)用者的execute()方法...
接收者的action1()方法被調(diào)用...
接收者的action2()方法被調(diào)用...
命令模式還可以同備忘錄(Memento)模式組合使用垒探,這樣就變成了可撤銷的命令模式
源碼中的應(yīng)用
- java.util.Timer類中scheduleXXX()方法
- java Concurrency Executor execute() 方法
- java.lang.reflect.Method invoke()方法
- org.springframework.jdbc.core.JdbcTemplate
- ......
在 JdbcTemplate 中的應(yīng)用
在JdbcTemplate中命令模式的使用并沒有遵從標(biāo)準(zhǔn)的命令模式的使用妓蛮,只是思想相同而已。
在 Spring 的 JdbcTemplate 這個(gè)類中有 query() 方法叛复,query() 方法中定義了一個(gè)內(nèi)部類 QueryStatementCallback仔引,QueryStatementCallback 又實(shí)現(xiàn)了 StatementCallback 接口扔仓,另外還有其它類實(shí)現(xiàn)了該接口褐奥,StatementCallback 接口中又有一個(gè)抽象方法 doInStatement()。在 execute() 中又調(diào)用了 query()翘簇。
StatementCallback
充當(dāng)?shù)氖敲罱巧?code>JdbcTemplate即充當(dāng)調(diào)用者角色撬码,又充當(dāng)接收者角色。上面的類圖只是為了方便理解版保,實(shí)際上呜笑,QueryStatementCallback 與 ExecuteStatementCallback是JdbcTemplate中方法的內(nèi)部類,具體看源碼中的內(nèi)容彻犁。
部分源碼分析
StatementCallback接口:
public interface StatementCallback<T> {
T doInStatement(Statement stmt) throws SQLException, DataAccessException;
}
JdbcTemplate類:
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)部類叫胁,實(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:以上代碼提交在 Github :https://github.com/Niuh-Study/niuh-designpatterns.git
文章持續(xù)更新驼鹅,可以公眾號搜一搜「 一角錢技術(shù) 」第一時(shí)間閱讀, 本文 GitHub org_hejianhui/JavaStudy 已經(jīng)收錄,歡迎 Star输钩。