.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

Strategy Pattern or Template pattern?

I have some simple .NET console applications that handle our job batch processing. Each "Job" will have different "Steps". A "Step" will be things like Run A Stored proc, Execute a Select Statement, Write a File, Read a file, Ftp put a file, Ftp get a file.

Currently, I'm using a "Switch" statement on "Step" type. The strategy patterns seems to lend itself to handle If then/else if statements nicely. Which would lead me to the Strategy pattern.

But, some people think I should use a a Template pattern. For each "Step" type, I would have 2 methods, "GetStepInfo" and "ProcessStep". The client would simply call these methods.

What do you think? Any similiar experiences?
Patrick From An IBank Send private email
Monday, January 28, 2008
Sounds tailor-made for the Command pattern, actually.  There are variations, but fundamentally you have a base class (or interface), Command, with an abstract Execute or Run method.  Then you create a class for each individual command, overriding Run/Execute.  You also need some sort of unique key for each command.  You stick an instance of each class in a dictionary, pass in the key, and execute the command you get out.
Kyralessa Send private email
Monday, January 28, 2008
You could integrate with Windows workflow as a sequential workflow too.
Monday, January 28, 2008
Do you have (or can you get your hands on) a copy of Fowler's "Refactoring" book? The one you want to look at is "Replace Type Code With Polymorphism".

This isn't really Strategy OR Template method, it's just a straightforward application of polymorphism.

In C#, you'd have something like this:

interface IJobStep
    void ExecuteStep(JobContext context);

Where JobContext includes whatever data about the job as a whole that the step needs. You'd create separate classes that implement this interface for each concrete step: ReadFileStep, CalculateInterestStep, SendRoundoffErrorsToPrivateAccountStep, that sort of thing.

Then your Job class becomes:

class Job
    List<IJobStep> steps;

    // fill in steps in constructor or something

    void Execute()
        JobContext context = ...;
        foreach(IJobStep in steps) {

That's not really the Strategy pattern, since you're not really customizing the execution of an algorithm in the Job class; the Steps really are the algorithm. I don't see how Template Method applies here at all.

Anyway, hope this helps.
Chris Tavares Send private email
Tuesday, January 29, 2008
When i've read your post, Chain of Responsibility design pattern came right into my mind, dunno if there is something similar for dotnet but apache commons library has the chain pattern included, and the sequencing of commands as if in a chain with filters allowd for postprocessing is marvellous for some situations.

best regards
joão viegas
joão viegas Send private email
Tuesday, January 29, 2008
I have written a similar application - I'm using templates, which are then set up using provided parameters.

My process config file says for example: connect to a database, query it and output results. So there's a connection step with connection string and timeout provided, there's a query step obviously with query text, and finally there's an output step with filename and file type.

Templates are selected using a "dynamic" select - a dictionary of types.

Actually, 2.0 version of my app is close to release. It was intended as an internal corporate app, but should anyone be interested in using it/cooperation, please write to me.
Wojtek Send private email
Wednesday, January 30, 2008

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz