Design Pattern Examples
Overview of object-oriented design patterns
Command Class Reference

Represents an operation that can be applied to a TextObject. This class can handle two kinds of operations, one that takes no additional parameters and one that takes two additional string parameters. More...

#include <Command_Classes.h>

Collaboration diagram for Command:
Collaboration graph

Public Member Functions

 Command (Command_TextObject::shared_ptr_t source, const std::string &commandName, two_parameter_operation operation, const std::string &argument1, const std::string &argument2)
 Constructor for a command that applies an operation to a TextObject, where the operation takes two parameters.
 
 Command (Command_TextObject::shared_ptr_t source, const std::string &commandName, no_parameter_operation operation)
 Constructor for a command that applies an operation to a TextObject but does not take any additional parameters.
 
void Execute ()
 Execute the command on the TextObject.
 
std::string ToString ()
 Convert this command to a string representation.
 

Private Attributes

Command_TextObject::shared_ptr_t _receiver
 
std::string _commandName
 
two_parameter_operation _two_parameter_operation
 
no_parameter_operation _no_parameter_operation
 
std::string _argument1
 
std::string _argument2
 

Detailed Description

Represents an operation that can be applied to a TextObject. This class can handle two kinds of operations, one that takes no additional parameters and one that takes two additional string parameters.

In a real program, the commands would be represented by an interface and concrete classes for each type of operation (based on additional parameters) would be used. This requires the calling entity to instantiate the appropriate concrete class as opposed to letting the C# compiler figure out the correct constructor based on parameters. Or the calling entity could use a class factory to create the concrete classes.

It is also conceivable to make this Command class general case in terms of passing in 0 or more arguments along with a delegate that takes 0 or more arguments. This complicates the actual calling of the delegate and obscures some of the functionality here but it would reduce the API to a single constructor and delegate. But then it's up to the user to get the right number of parameters into the command for the delegate.

Definition at line 117 of file Command_Classes.h.

Constructor & Destructor Documentation

◆ Command() [1/2]

Command ( Command_TextObject::shared_ptr_t  source,
const std::string &  commandName,
two_parameter_operation  operation,
const std::string &  argument1,
const std::string &  argument2 
)
inline

Constructor for a command that applies an operation to a TextObject, where the operation takes two parameters.

Parameters
sourceThe TextObject to apply the operation to.
commandNameEasy-to-read name of the command.
operationThe operation to apply to the TextObject.
argument1First argument to the operation (after the TextObject).
argument2Second argument to the operation (after the TextObject).

Definition at line 148 of file Command_Classes.h.

References Command::_argument1, Command::_argument2, Command::_commandName, Command::_receiver, and Command::_two_parameter_operation.

◆ Command() [2/2]

Command ( Command_TextObject::shared_ptr_t  source,
const std::string &  commandName,
no_parameter_operation  operation 
)
inline

Constructor for a command that applies an operation to a TextObject but does not take any additional parameters.

Parameters
sourceThe TextObject to apply the operation to.
commandNameEasy-to-read name of the command.
operationThe operation to apply to the TextObject.

Definition at line 165 of file Command_Classes.h.

References Command::_commandName, Command::_no_parameter_operation, and Command::_receiver.

Member Function Documentation

◆ Execute()

void Execute ( )
inline

Execute the command on the TextObject.

Definition at line 176 of file Command_Classes.h.

References Command::_argument1, Command::_argument2, Command::_no_parameter_operation, Command::_receiver, and Command::_two_parameter_operation.

◆ ToString()

std::string ToString ( )
inline

Convert this command to a string representation.

Returns
A string containing the representation of the command.

Definition at line 192 of file Command_Classes.h.

References Command::_argument1, Command::_argument2, Command::_commandName, Command::_no_parameter_operation, Command::_two_parameter_operation, and Helpers::formatstring().

Member Data Documentation

◆ _argument1

std::string _argument1
private

Definition at line 133 of file Command_Classes.h.

Referenced by Command::Command(), Command::Execute(), and Command::ToString().

◆ _argument2

std::string _argument2
private

Definition at line 136 of file Command_Classes.h.

Referenced by Command::Command(), Command::Execute(), and Command::ToString().

◆ _commandName

std::string _commandName
private

Definition at line 124 of file Command_Classes.h.

Referenced by Command::Command(), and Command::ToString().

◆ _no_parameter_operation

no_parameter_operation _no_parameter_operation
private

Definition at line 130 of file Command_Classes.h.

Referenced by Command::Command(), Command::Execute(), and Command::ToString().

◆ _receiver

Command_TextObject::shared_ptr_t _receiver
private

Definition at line 121 of file Command_Classes.h.

Referenced by Command::Command(), and Command::Execute().

◆ _two_parameter_operation

two_parameter_operation _two_parameter_operation
private

Definition at line 127 of file Command_Classes.h.

Referenced by Command::Command(), Command::Execute(), and Command::ToString().


The documentation for this class was generated from the following file: