Command Pattern
In a nutshell, we
have a group/set of behaviors and want them to treat them uniformly. We want to
program them against the single interface. We want the Clients of these related
set of behaviors to be treated uniformly. Client is coded against
the Interface that the different commands also implement. Client is not aware
of different concrete implementation of the commands. Thus these commands can
vary their details and implementations and client will be not affected with
this.
Usually we have in-memory
collections of such commands initialized at the program startup. At start up
all the dependencies and context required to the work are loaded at that time.
So that command server clients efficiently.
So much of context and dependencies required for a Command Objects to do
its job is pre-fetched at start up. Client gets Command Object from HashTable
or HashMap holding all command objects and execute their methods to do the work.
BCS Bookstore. Commands may be singletons that only one object is enough to do
the job.
In a nutshell, we have set of
commands, and all are at the same level and we want them to be treated as the
same. We do not want big SWITCH statement for the command that we want to
execute. The factory or some other mechanism will specify what kind of command
we want to execute. Command line applications and the UI Menu based operations
are example of such patterns.