HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpModerate

Is this a good example of the strategy pattern?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thistheexamplegoodstrategypattern

Problem

I've been reading The Pragmatic Programmer for a few days and I've come across a reference to the strategy pattern. Looked it up in Design Patterns and figured I could refactor a piece of code I'm currently working on to comply with the strategy pattern.

I need to process text using many string algorithms much like Lucene analyze and tokenize text. This is for an importation routine. Strategy:

public class ChainOfProcessor: IStringProcessor
{
    public List Processors;

    public ChainOfProcessor()
    {
        Processors = new List();
    }

    public ChainOfProcessor Add()
        where TProcessor: IStringProcessor, new()
    {
        return Add(new TProcessor());
    }

    public ChainOfProcessor Add(IStringProcessor processor)
    {
        Processors.Add(processor);
        return this;
    }

    public string Process(string input)
    {
        return Processors.Aggregate(input, (current, processor) => processor.Process(current));
    }
}

public interface IStringProcessor
{
    string Process(string input);
}

public class HtmlStripper: IStringProcessor
{
    public virtual string Process(string input)
    {
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(input);
        return htmlDoc.DocumentNode.InnerText;
    }
}

public class HtmlDecoder: IStringProcessor
{
    public string Process(string input)
    {
        return HttpUtility.HtmlDecode(input);
    }
}


In Design Patterns, the authors use a Composition class and Compositor classes as examples. The client is allowed to chose a single Compositor through Composition to do whatever the Compositor is supposed to do. There is a subtle difference in how the client uses the Composition in my example. Instead of allowing a single IStringProcessor to be used through some Composition class, I allow many, to be executed one after the other through the concrete class ChainOfProcessor. Usage:

```
var text = new ChainOfProcessor()
.Add()
.Add()
.Proc

Solution

It seems like a you should implement Chain of responsibility as you are passing the same string to different kind of processor with a particular sequence

http://www.dofactory.com/Patterns/PatternChain.aspx#_self1

This is a gud pattern in your scenario.

One more thing your StringProcessorChain might not be needed in this case....

Let me know if you need a more highlight on same

Context

StackExchange Code Review Q#18352, answer score: 10

Revisions (0)

No revisions yet.