Command命令模式介紹:
Command命令模式是一種對象行為型模式,它主要解決的問(wèn)題是:在軟件構建過(guò)程中,“行為請求者”與“行為實(shí)現者”通常呈現一種“緊耦合”的問(wèn)題。如下圖:

有時(shí)我們必須向某對象提交請求,但并不知道關(guān)于被請求的操作或請求的接受者的任何信息,此時(shí)無(wú)法抵御變化的緊耦合是不合適的。如:需要對行為進(jìn)行“記錄、撤銷(xiāo)/重做、事務(wù)”等處理。我們所要做的是將依賴(lài)關(guān)系轉化,將緊耦合變?yōu)樗神詈?。則上圖的形式轉化為如下形式:
Command模式通過(guò)將請求本身變成一個(gè)對象來(lái)使行為請求者可向未指定的應用對象提出請求。
GoF《設計模式》中說(shuō)道:將一個(gè)請求封裝為一個(gè)對象,從而使你可用不同的請求對客戶(hù)進(jìn)行參數化;對請求排隊或記錄請求日志,以及支持可撤銷(xiāo)的操作。
Command命令模式結構:
定義場(chǎng)景:
現在來(lái)看一個(gè)場(chǎng)景:對于notepad大家都很熟悉,在我們使用notepad打開(kāi)一個(gè)文檔之后,往往做一些操作,如;輸入字符(Write)、刪除前一個(gè)字符(Delete)、撤銷(xiāo)剛才的操作(UnDo)?,F在我們就用Console程序模擬這個(gè)過(guò)程。
代碼實(shí)現與結構分析:
在實(shí)現代碼前先說(shuō)明實(shí)現Command模式需要烤爐的一些問(wèn)題:
1、 一個(gè)命令對象應達到何種智能程度:命令對象的能力可大可小。這樣就出現了兩個(gè)極端。一是:它僅確定一個(gè)接收者和執行該請求的動(dòng)作;一是:它自己實(shí)現所有功能,根本不需要額外的接收者對象。我給他們起了便于我方便記憶的名字,第一種叫OperationCommand,第二種叫ObjectCommand。當然只是為了便于記憶和理解,如有不理解,在代碼實(shí)現與結構分析最后我會(huì )再談?wù)勎业南敕?,如有不妥,還請多提意見(jiàn),一會(huì )在下面的代碼中會(huì )分別對這兩種情況進(jìn)行示范。
2、 支持取消和重做:為了達到這個(gè)目的ConcreteCommand類(lèi)中要存儲額外的狀態(tài)信息。也就是上圖中ConcreteCommand的state屬性。
3、 避免取消操作中過(guò)程中的錯誤積累:由于命令重復的執行、取消執行和重執行的過(guò)程可能會(huì )積累錯誤,以致一個(gè)應用的狀態(tài)最終偏離初始值。這就有必要在Command中存入更多的信息以保證這些對象可被精確的復原。
下面來(lái)看看代碼上的實(shí)現:首先,我先作一個(gè)OperationCommand的例子,先做一個(gè)請求的接收者Document(也就是結構圖中的Receiver)
class Document
{
public string strContent;
public Document()
{
strContent = "";
}
}
在這個(gè)程序中我們還要定義一個(gè)抽象類(lèi)Command,對于OperationCommand類(lèi)型來(lái)說(shuō),它僅確定一個(gè)接收者和執行該請求的動(dòng)作。所以,在抽象類(lèi)Command中,只聲明一個(gè)Excute的方法。這個(gè)方法在其子類(lèi)中進(jìn)行實(shí)現。(當然這個(gè)Command還可以定義成接口)
abstract class Command
{
public Command()
{ }
public abstract void Excute();
}
接下來(lái),就要實(shí)現各種操作(結構圖中的ConcreteCommand),代碼如下
//寫(xiě)操作
class WriteCommand : Command
{
Document doc;
ArrayList ObjectState;
public WriteCommand(Document doc, ArrayList state)
{
this.doc = doc;
ObjectState = state;
}
public override void Excute()
{
doc.strContent += Console.ReadLine();
ObjectState.Add(doc.strContent);
}
}
//刪除操作
class DeleteCommand : Command
{
Document doc;
ArrayList ObjectState;
public DeleteCommand(Document doc, ArrayList state)
{
this.doc = doc;
ObjectState = state;
}
public override void Excute()
{
doc.strContent = doc.strContent.Substring(0, doc.strContent.Length - 1);
ObjectState.Add(doc.strContent);
}
}
//撤銷(xiāo)操作
class UnDoCommand : Command
{
Document doc;
ArrayList ObjectState;
public UnDoCommand(Document doc, ArrayList state)
{
this.doc = doc;
ObjectState = state;
}
public override void Excute()
{
doc.strContent = (string)ObjectState[ObjectState.Count - 2];
ObjectState.Add(doc.strContent);
}
}
實(shí)現了各種操作后,編寫(xiě)一個(gè)客戶(hù)代碼進(jìn)行測試
class Program
{
static void
{
Document doc = new Document();
Console.WriteLine("Please Input next operation:");
string strOperation = Console.ReadLine();
Command com = null;
ArrayList ObjectState = new ArrayList();//Record state
while (strOperation != "Exit")
{
switch (strOperation.ToLower())
{
case "write":
com = new WriteCommand(doc, ObjectState);
com.Excute();
Console.WriteLine("Write Operation:" + doc.strContent);
break;
case "
com = new DeleteCommand(doc, ObjectState);
com.Excute();
Console.WriteLine("Delete Operation:" + doc.strContent);
break;
case "undo":
com = new UnDoCommand(doc, ObjectState);
com.Excute();
Console.WriteLine("UnDo Operation:" + doc.strContent);
break;
default:
Console.WriteLine("Wrong Operation:");
break;
}
Console.WriteLine("Please Input next operation:");
strOperation = Console.ReadLine();
}
}
}
測試結果:
Please Input next operation:
write
k
Write Operation:k
Please Input next operation:
write
i
Write Operation:ki
Please Input next operation:
write
d
Write Operation:kid
Please Input next operation:
write
d
Write Operation:kidd
Please Input next operation:
Delete Operation:kid
Please Input next operation:
undo
UnDo Operation:kidd
Please Input next operation:
下面再來(lái)實(shí)現以下ObjectCommand的例子,首先還是編寫(xiě)一個(gè)已存在的請求接收者Document(結構圖中的Receiver)
class Document
{
public string strContent;
public Document()
{
strContent = "";
}
}
接下來(lái)實(shí)現抽象類(lèi)Command(也可以使用接口),對于ObjectCommand類(lèi)型來(lái)說(shuō),它自己實(shí)現所有功能,根本不需要額外的接收者對象,所以在Command中聲明了所有的操作
abstract class Command
{
public Command()
{ }
public abstract void Write();
public abstract void Delete();
public abstract void UnDo();
}
有了Command,就可以實(shí)現具體的操作類(lèi)型DocumentCommand(結構圖中的ConcreteCommand)
class DocumentCommand : Command
{
private Document doc;
private ArrayList ObjectState = new ArrayList();//Record State
public DocumentCommand(Document doc)
{
this.doc = doc;
}
public override void Write()
{
Console.WriteLine("Please input an character:");
string strRead = Console.ReadLine();
doc.strContent += strRead;
ObjectState.Add(doc.strContent);
}
public override void Delete()
{
doc.strContent = doc.strContent.Substring(0, doc.strContent.Length - 1);
ObjectState.Add(doc.strContent);
}
public override void UnDo()
{
doc.strContent = (string)ObjectState[ObjectState.Count - 2];
ObjectState.Add(doc.strContent);
}
}
接下來(lái)就用一個(gè)客戶(hù)端代碼作一下測試
class Program
{
static void
{
Document doc = new Document();
DocumentCommand com = new DocumentCommand(doc);
Console.WriteLine("Please Input next operation:");
string strOperation = Console.ReadLine();
while (strOperation != "Exit")
{
switch (strOperation.ToLower())
{
case "write":
com.Write();
Console.WriteLine("Write Operation:" + doc.strContent);
break;
case "
com.Delete();
Console.WriteLine("Delete Operation:" + doc.strContent);
break;
case "undo":
com.UnDo();
Console.WriteLine("UnDo Operation:" + doc.strContent);
break;
default:
Console.WriteLine("Wrong Operation:");
break;
}
Console.WriteLine("Please Input next operation:");
strOperation = Console.ReadLine();
}
}
}
測試結果如下:
Please Input next operation:
write
Please input an character:
k
Write Operation:k
Please Input next operation:
write
Please input an character:
i
Write Operation:ki
Please Input next operation:
write
Please input an character:
d
Write Operation:kid
Please Input next operation:
write
Please input an character:
d
Write Operation:kidd
Please Input next operation:
Delete Operation:kid
Please Input next operation:
undo
UnDo Operation:kidd
Please Input next operation:
這兩個(gè)程序中需要有幾點(diǎn)說(shuō)明:
1、 對于OperationCommand,我的理解是它所實(shí)現的Command只是某一個(gè)操作對于某一個(gè)接收者,所以我給它取名為OperationCommand。對于ObjectCommand,是實(shí)現這樣一種對象,它實(shí)現了請求接收者的所有操作,所以取名為ObjectCommand
2、 在代碼實(shí)例中,我對狀態(tài)的保存處理相對簡(jiǎn)單,但這是因為利用了String對象的特點(diǎn),當String對象被修改時(shí),系統會(huì )重新分配一塊內存。不修改原內存上的內容。如果是要保存其他的引用類(lèi)型應當注意使用深拷貝,否則,所保存的狀態(tài)對象都指向一個(gè)內存地址,隨著(zhù)狀態(tài)的改變,保存不了原有的狀態(tài)。
3、 在對象狀態(tài)的保存上,我們可以使用Prototype模式。
Command模式的幾個(gè)要點(diǎn):
1、 Command模式的根本目的在于將“行為請求者”與“行為實(shí)現者”解耦,在面向對象語(yǔ)言中,常見(jiàn)的實(shí)現手段是“將行為抽象為對象”。
2、 實(shí)現Command接口的具體命令對象ConcreteCommand 有時(shí)候根據需要可能會(huì )保存一些額外的狀態(tài)信息。
3、 通過(guò)使用Composite模式,可以將多個(gè)“命名”封裝為一個(gè)“復合命令”MacroCommand。
4、 Command模式與C#中的Delegate有些類(lèi)似。但兩者定義行為接口的規范有所區別:Command以面向對象中的“接口-實(shí)現”類(lèi)定義行為接口規范,更嚴格,更符合抽象原則:Delegate以函數簽名來(lái)定義行為接口規范,更靈活,但抽象能力比較弱
聯(lián)系客服