Game Development Reference
In-Depth Information
given the ability to work with these commands. The result is basically the command
pattern[Gammaetal.95]whichcanbemodeledasshownin
Figure13.1
.
According to this model, the
Application
class is responsible for creating the
elements and commands of the user interface, and for linking each item of the
interface with its corresponding command. In the model proposed here, we go a step
further in the separation of user interface and application logic by eliminating the
necessity of an entity that links every element of the interface with its corresponding
command.
13.3 Low Coupling
For a low-coupling system, we will eliminate the necessity of the command consumer
to know the specific types of commands in order to be able to create them. Each
command will be identified by its name, and with the help of a factory, we will
create an instance of the command from anywhere in the system. Coming back
to the example of the user interface, a button can create and run an associated
command and only needs to know its name to do so. This can be modeled as in
In this diagram, we appreciate that there is no link to the specific type of
commands. The command consumer (in this case
UIItem
) only knows the
Command
interface and the
factory
used to create the instances. Moreover, the
Application
CommandFactory
CreateCommand()
UIItem
Command
commandName
Execute()
Activated ()
...
command = factory->CreateCommand(commandName);
command->Execute();
...
COMMANDS
Application
Logic
Application
Init()
...
CreateUI()
...
Figure 13.2.
Low-coupling command system.