2017-05-31 43 views
1

命令模式的意圖是「將請求封裝爲對象,從而讓你使用不同的請求參數化客戶端 ...」有人可以解釋具有不同請求的參數化客戶端是什麼意思嗎?以及命令模式如何用不同的請求參數化客戶端?命令模式如何用不同的請求參數化客戶端?

在這方面的任何解釋,將不勝感激

回答

1
class ICommand 
{ 
    public: 
    virtual ~ICommand() 
    {} 
    virtual int InitCommand() = 0; 
    virtual int ExecuteCommand() = 0; 
    virtual int FinalizeCommand() = 0; 
}; 

class CCommandProcessor 
{ 
    public: 
     virtual ~ICommandProcessor() 
     {} 
     int ProcessCommand(ICommand *_pCommand); 
     { 
      int iResult = _pCommand->InitCommand(); 
      if(iResult == 0) 
      { 
       cout << "InitCommand Failed" << endl; 
       return 0; 
      } 

      iResult = _pCommand->ExecuteCommand(); 
      if(iResult == 0) 
      { 
       cout << "ExecuteCommand Failed" << endl; 
       return 0; 
      } 

      iResult = _pCommand->FinalizeCommand(); 
      if(iResult == 0) 
      { 
       cout << "FinalizeCommand Failed" << endl; 
       return 0; 
      } 

      return 1; 
     } 
} 

class CCopyDocumentCommand : public ICommand 
{ 
    private: 
     std::string m_szDocumentName; 
     std::string m_szSavePath;  

    public: 
     CCopyDocumentCommand(std::string _szDocumentName, std::_szSavePath) 
     { 
      m_szDocumentName = _szDocumentName; 
      m_szSavePath = _szSavePath; 
     } 
     virtual int InitCommand() 
     { 
      //check the document save path valid. 
      //check the document for any errors.    
     } 
     virtual int ExecuteCommand() 
     { 
      //copy the document 
     } 
     virtual int FinalizeCommand() 
     { 
      //delete temporaries if used. 
     } 
}; 

class CPrintDocumentCommand : public ICommand 
{ 
    private: 
     std::string m_szDocumentName; 
     std::string m_szPageSettings;  
     int iTopMargin; 
     int iLeftMargin 
    public: 
     CPrintDocumentCommand(std::string _szDocumentName, std::_szPageSettings, int _iTopMargin, int iLeftMargin) 
     { 
      m_szDocumentName = _szDocumentName; 
      m_szPageSettings = _szPageSettings; 
      m_iTopMargin = _iTopMargin; 
      m_iLeftMargin = _iLeftMargin; 
     } 
     virtual int InitCommand() 
     { 
      //check the page settings. 
      //check the document for any errors. 
      //check printer 
     } 
     virtual int ExecuteCommand() 
     { 
      //print the document; 
     } 
     virtual int FinalizeCommand() 
     { 
      //delete temporary if used. 
     } 
}; 


CCommandProcessor oProcessor; 
CPrintDocumentCommand oPrintCommand("c:\\data\\report.txt", "some settings", 5, 5); 
CCopyDocumentCommand oCopyCommand("c:\\data\\report.txt", "c:\\data\\report_.txt"); 

oProcessor.ProcessCommand(&oPrintCommand); 
oProcessor.ProcessCommand(&oCopyCommand); 

正如你可以看到同樣的​​執行其在不同的參數設置工作,不同的命令。 也就是說,Command Pattern允許您將parameterscommand function轉換爲data memberscommand object

+0

感謝您快速回復...我有一個混淆,參與者的命令模式是命令,ConcreteCommand,客戶端,調用者和接收器,在上面提到的例子中,您可以確定誰是作爲客戶端參與不同請求使用命令模式? –

+0

爲了理解命令模式,考慮CPU的工作。作業/進程安排在CPU上。每個作業/過程都有不同的一組數據(參數)可供使用。然而,相同的CPU執行不同類型的作業/過程。在這個例子中,命令是作業/進程,而CommandProcessor是框架/ CPU,其上可以調度不同作業/命令的不同作業/命令。客戶端/應用程序使用不同的命令對象來執行不同的任務。所以,有'Application/Clients','Command Objects'和'Command Processing Framework'。 – sameerkn

+0

在這個例子中,我認爲**命令的**接收器**是參數化的,而不是**客戶端**,不是嗎?如果不是,請幫助我清除我的困惑,即如何使用命令模式將客戶端參數化爲不同的請求? –

0

目前還不清楚(對我)有什麼參數化的真正意義在這裏。我懷疑這隻意味着你可以給每個客戶端的接收器提供一系列不同的請求。

在適用性,gof接着說:「當你想

參數化對象由操作使用Command模式來執行,如MenuItem對象上面那樣。您可以使用回調函數在過程語言中表示這樣的參數化,也就是說,在稍後的某個位置將會調用某個函數。命令是回調的面向對象的替代品。 ......」