日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

命令(Command)模式

系統 2373 0

在軟件系統中,行為請求者;與行為實現者通常呈現一種緊耦合。但在某些場合,比如要對行為進行記錄、撤銷/重做、事務等處理,這種無法抵御變化的緊耦合是不合適的。在這種情況下,如何將行為請求者與行為實現者解耦?將一組行為抽象為對象,可以實現二者之間的松耦合。
將一個請求封裝為一個對象,從而使你可用不同的請求對客戶進行參數化;對請求排隊或記錄請求日志,以及支持可撤消的操作。

一,結構

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

二,示例代碼

    public class Document {
	public void display() {
		System.out.println("Display");
	}

	public void undo() {
		System.out.println("Undo");
	}

	public void redo() {
		System.out.println("Redo");
	}
}

/**
 * 抽象命令
 * 
 * @author Salmon
 * 
 */
public abstract class DocumentCommand {
	protected Document _document;

	public DocumentCommand(Document doc) {
		this._document = doc;
	}

	public abstract void execute();
}

/**
 * 顯示命令
 * 
 * @author Salmon
 * 
 */
public class DisplayCommand extends DocumentCommand {
	public DisplayCommand(Document doc) {
		super(doc);
	}

	public void execute() {
		this._document.display();
	}
}

/**
 * 撤銷命令
 * 
 * @author Salmon
 * 
 */
public class UndoCommand extends DocumentCommand {
	public UndoCommand(Document doc) {
		super(doc);
	}

	public void execute() {
		this._document.undo();
	}
}

/**
 * 重做命令
 * 
 * @author Salmon
 * 
 */
public class RedoCommand extends DocumentCommand {
	public RedoCommand(Document doc) {
		super(doc);
	}

	public void execute() {
		_document.redo();
	}
}

/**
 * invoker角色
 * @author Salmon
 *
 */
public class DocumentInvoker {
	private DocumentCommand _discmd;
	private DocumentCommand _undcmd;
	private DocumentCommand _redcmd;

	public DocumentInvoker(DocumentCommand discmd, DocumentCommand undcmd,
			DocumentCommand redcmd) {
		this._discmd = discmd;
		this._undcmd = undcmd;
		this._redcmd = redcmd;
	}

	public void display() {
		_discmd.execute();
	}

	public void undo() {
		_undcmd.execute();
	}

	public void redo() {
		_redcmd.execute();
	}
}

/**
 * 客戶端代碼
 * @author Salmon
 *
 */
public class Client {
	public static void main(String[] args) {
		Document doc = new Document();
		DocumentCommand discmd = new DisplayCommand(doc);
		DocumentCommand undcmd = new UndoCommand(doc);
		DocumentCommand redcmd = new RedoCommand(doc);
		DocumentInvoker invoker = new DocumentInvoker(discmd, undcmd, redcmd);
		invoker.display();
		invoker.undo();
		invoker.redo();
	}
}
  


可以看到:
1.在客戶程序中,不再依賴于Document的display(),undo(),redo()命令,通過Command對這些命令進行了封裝,使用它的一個關鍵就是抽象的Command類,它定義了一個操作的接口。同時我們也可以看到,本來這三個命令僅僅是三個方法而已,但是通過Command模式卻把它們提到了類的層面,這其實是違背了面向對象的原則,但它卻優雅的解決了分離命令的請求者和命令的執行者的問題,在使用Command模式的時候,一定要判斷好使用它的時機。
2.上面的Undo/Redo只是簡單示意性的實現,如果要實現這樣的效果,需要對命令對象設置一個狀態,由命令對象可以把狀態存儲起來。

?

命令(Command)模式


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 黄石市| 通州区| 临安市| 湖南省| 奉节县| 万源市| 武陟县| 岱山县| 台南市| 茶陵县| 扎兰屯市| 柯坪县| 滁州市| 房山区| 上犹县| 罗平县| 浙江省| 通许县| 江孜县| 潞城市| 南昌县| 蕉岭县| 寻乌县| 蕲春县| 武陟县| 平顶山市| 九寨沟县| 米脂县| 阿克苏市| 和静县| 凤凰县| 永兴县| 阿鲁科尔沁旗| 闸北区| 花莲县| 濮阳市| 彰化市| 淳安县| 息烽县| 视频| 九江市|