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...

Collaboration diagram for Command:
Collaboration graph

Public Member Functions

 Command (Command_TextObject source, string commandName, two_parameter_operation operation, string argument1, string argument2)
 Constructor for a command that applies an operation to a TextObject, where the operation takes two parameters.
 
 Command (Command_TextObject source, 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.
 
override string ToString ()
 Convert this command to a string representation.
 

Private Attributes

Command_TextObject _receiver
 
string _commandName
 
two_parameter_operation_two_parameter_operation
 
no_parameter_operation_no_parameter_operation
 
string _argument1
 
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 111 of file Command.cs.

Constructor & Destructor Documentation

◆ Command() [1/2]

Command ( Command_TextObject  source,
string  commandName,
two_parameter_operation  operation,
string  argument1,
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 140 of file Command.cs.

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

◆ Command() [2/2]

Command ( Command_TextObject  source,
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 157 of file Command.cs.

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

Member Function Documentation

◆ Execute()

void Execute ( )
inline

◆ ToString()

override string ToString ( )
inline

Convert this command to a string representation.

Returns
A string containing the representation of the command.

Definition at line 186 of file Command.cs.

References Command._argument1, Command._argument2, Command._commandName, Command._no_parameter_operation, and Command._two_parameter_operation.

Member Data Documentation

◆ _argument1

string _argument1
private

Definition at line 126 of file Command.cs.

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

◆ _argument2

string _argument2
private

Definition at line 129 of file Command.cs.

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

◆ _commandName

string _commandName
private

Definition at line 117 of file Command.cs.

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

◆ _no_parameter_operation

no_parameter_operation? _no_parameter_operation
private

Definition at line 123 of file Command.cs.

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

◆ _receiver

Command_TextObject _receiver
private

Definition at line 114 of file Command.cs.

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

◆ _two_parameter_operation

two_parameter_operation? _two_parameter_operation
private

Definition at line 120 of file Command.cs.

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


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