For example, in the first pattern I have choosen to re-implement is the command pattern. It makes use of an interface based receiver contained in a command that is derived from an abstract class and finally executed by an invoker. The receiver, command and invoker classes are not supposed to know anything about each other and function as a framework to which new commands can be added. Starting with the finished project, here is what it looks like:
Where in C# or Java you would create an interface or abstract class, I opted to create:
- CommandAbstract.js – This has a property for the receiver implementation and a method that needs to be overridden in the concrete command implementations. Using the method in reference #1 is pretty darn close to inheritance. In running the sample, this class’s execute() was never called.
For the commands, I inherited the execute() method from CommandAbstract.js into the four command implementations. For the receivers, I ‘implemented’ (well, not really…but sounds good :)) the receivers action() method in the four receiver implementations.
All of this is put together in command.js (a driver file of sorts). I hope to have all of the patterns I am interested in implemented by the end of March.