Game Development Reference
In-Depth Information
class does not need to know specific user interface components, because these are
able to create command instances on their own. The Application Logic box
represents the data that commands act on and the algorithms used to modify that
data.
13.3.1 Command Factory
The key element of the model proposed here is the CommandFactory that must be
able to create a command instance based on the command's name. It is much more
convenient for developers to not have to touch the factory's code every time a new
command is added. The extent to which this is possible depends very much on the
programming language used for implementation.
With the .NET framework and other modern languages, we can create an in-
stance based on a class name, so if we match the command name with the name of
the class that contains the command, the implementation of the factory is easy.
In the case of C++, we have to decide whether to implement a dictionary that
translates a command name into a specific factory function or to give commands
the ability to autoregister in the static initialization of the application.
From the point of view of design the command factory is a singleton, and there-
fore, there can only be one instance in the system.
13.3.2 Commands with Parameters
Let's look at a command that changes the color of a selected object in our model
editor. This command needs to know both the object on which it acts and the new
color to apply. In order to achieve this, we could establish a mechanism for the
command to know the elements that are necessary for it to perform the operation
correctly. However, this might imply that the command logic accesses the user
interface, which is not desirable and should be avoided if possible. Thus, we will
improve the commands with a generic parameter-passing mechanism instead. This
mechanism agrees with the low-coupling philosophy we have been working with so
far.
We will identify the parameters by their name and add the SetParam method
totheclass Command (see Figure13.3 ) .
Command
value:
a generic class like Object or
a variant type like boost::any
Execute()
SetParam(in name, in value)
Figure 13.3. Command extended with a parameter setting mechanism.
 
Search WWH ::




Custom Search