Reputation: 2568
The intent of command pattern is "Encapsulate a request as an object, thereby letting you parametrize clients with different requests..." Can someone explain what does parametrization clients with different requests means? and how command pattern parametrizes clients with different requests?
Any Explanation in this regard will be highly appreciated
Upvotes: 2
Views: 574
Reputation: 2230
Here is a more detailed structure diagram of Command:
From this structure, you can see that the "parameterized objects" should be the parameter of the setCommand() method in the Invoker class, the type of the parameter is Command interface.
So "parametrize clients with different requests" should means that you can pass different instance of command, then you can achieve different operations with the same executeCommand() method calling.
Upvotes: 1
Reputation: 2259
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);
As you can see the same CommandProcessor
executes different commands which work on different set of parameters.
That is, the Command Pattern
allows you to convert parameters
of a command function
to data members
of a command object
.
Upvotes: 1
Reputation: 10003
It's not clear (to me) what parameterize really means here. I suspect that it just means that you can give each client's receiver a bunch of different requests.
Under Applicability, gof goes on to say: 'Use the Command pattern when you want to
parameterize objects by an action to perform, as MenuItem objects did above. You can express such parameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks. ...'
Upvotes: 0