As I’ve blogged before, a Kata is a practice exercise. I’m attempting to brainstorm out a kata for each of the main GoF design patterns, partly as a mental exercise, and partly as the first step in producing a course that I can give to developers so that they will truly “get” how design patterns can be used.
Template method is a simple pattern, really. The idea is that the base class provides code for a method that calls abstract methods to do some of the work. The concrete children implement only the abstract methods. Users of the concrete methods call the base class implementation which does all the work.
For a kata to work, I need to create a DLL that behaves in a particular way, and a set of unit tests, all of which fail. The student is responsible for creating a class or multiple classes in such a way that all unit tests pass and any non-testable constraints (like the choice to use a particular pattern) can be verified.
I’m thinking that the base class should have methods for depth-first-search and breadth-first-search, but should delegate the movement from a node to its child to the concrete object. That way, a developer who creates a tree structure in memory can use either mechanism to search the tree simply by implementing the code to move from one node to another.
If you can think of a better Kata for Template Method, jump in.
One thought on “Coding Dojo Suggestion: Template Method Kata”
This sounds like an overly complex manner to handle a KATA.
As much as I love inheritance, I belive that a single Base Class can be built powerful enough to not require a Child Class. This saves on lines of code and simplifies everything. Also, why design a DLL when you can take your single class and stick it in a header file?
I have one technique which I think can handle virtually anything you throw at it. It’s a 3 step process.
1) Create a class KATA with the appropriate functions and member variables.
2) Create an Array of the Class KATA and make it hold as many (pattern movements) as you need (if this is an unknown then you set the Array to NULL or if you prefer you could just set it to 1 pattern as a default, since all KATAs have at least 1 move).
3) Create a Vector and make it hold the Array.
Now you got a Vector of an Array of the Class KATA and you are virtually unstoppable:-)
The member variables and functions of the class KATA will identity the KATA pattern and produce as many pattern movements as required. Simply assigning an "integer" can be used to associate it with a particular KATA.
If you want your program to hold 4 different KATA patterns then integer 1 = KATA pattern 1 and integer 2 = KATA pattern 2 and so on.
If it’s easier for you to pass a "string" based off of user input then that can be done as well within your KATA class. But, it makes more sense to pass an Integer using Keyboard recognition to save your user’s from too much typing.
The vector will resize itself based on increasing or decreasing the size of your array. So, no matter which KATA your users select and how many movements there are to the KATA your Vector will resize itself accordingly.
Searching the Array is fairly straightforward since you can set up a function with an appropriate pointer variable in the Class KATA to search, sort, and re-organize your KATA’s patterns in order to create a new KATA form. You can start from the front or back of your Vector. You could even start in the middle if you check for NULL.
And best of all you can throw in a Destructor to, in effect, allow your users to dispatch your KATA from memory when they exit the program.
Hope this helps:-)