Chapter 21. COMMAND and ACTIVE OBJECT: Versatility and Multitasking
© Jennifer M. Kohnke
Of all the design patterns that have been described over the years, COMMAND impresses me as one of the simplest and most elegant. But we shall see, the simplicity is deceptive. The range of uses that COMMAND may be put to is probably without bound. The simplicity of COMMAND, as shown in Figure 21-1, is almost laughable. Listing 21-1 doesn't do much to dampen the levity. It seems absurd that we can have a pattern that consists of nothing more than an interface with one method. Figure 21-1. COMMAND pattern
Listing 21-1. Command.cs
In fact, this pattern has crossed a very interesting line. And it is in the crossing of this line that all the interesting complexity lies. Most classes associate a suite of methods with a corresponding set of variables. The COMMAND pattern does not do this. Rather, it encapsulates a single function free of any variables. In strict object-oriented terms, this is anathema, smacking of functional decomposition. It elevates the role of a function to the level of a class. Blasphemy! Yet at this boundary where two paradigms clash, interesting things start to occur. |