Tuesday, August 22, 2006

Strategy or Template Pattern

The training that I had last week was perfect. It put patterns into my mind and I have recognized that I have a problem to solve that requires either the Strategy Pattern or the Template pattern.

But how can I decide which one?

Problem description.
Iterate through a folder containing a list of files. Connect to a backend system. Process the files.

This itself does not lend itself to the pattern, it is not until I checked out the code from the repository that I see that we already have 2 other applications (they look like they were copy and pasted) that do the same basic logic. There is a slight difference in the processing of the file, but the logical flow of the application is exactly the same in all 3 instances.

Strategy Pattern according to James Cooper in C# Desing Patterns
"The strategy pattern consists of a number of related algorithms encapsulated in a driver class called the Context. Your client program can select one of these differing algorithms, or in some cases, the Context might select the best one for you. The intent is to make these algorithms interchangeable and provide a way to choose th emost appropriate one."

Template Pattern according to James Cooper in C# Desing Patterns
"The Template pattern formalizes the idea of defining an algorithm in a class but leaving some of the details to be implemented in subclasses."

I think they could both be made to work, but the Template pattern sounds better. So can build the base class, derive my class from it, and build a front end to use my new generic class. Then I need to refactor the other 2 implementations to make them also fit and I can have a single front end that can handle calling all implementations.

I like this whole patterns thing, I wish I had known it sooner.

No comments: