DEV Community

Using the Strategy Pattern (Examples in C#)

Sam Ferree on May 15, 2018

Prerequisites To get the most out of this post, it helps if you have a basic understanding of object oriented programming and inheritanc...
Collapse
 
jfrankcarr profile image
Frank Carr

This is one of my favorite patterns to use with things like manufacturing lines and work cells where there is a lot of common activity (barcode reading, inventory control, etc) but the implementation details vary to some degree from work area to area.

Collapse
 
thompcd profile image
Corey Thompson

Hey Frank, I know this is super old, but do you happen to have any examples around of the use case you described? I'm building basically the same type of system for manufacturing lines and trying to tackle the issue without blowing up our config files further. Also, for the first time, we'll have to conditionally load extern dll's in each strategy also, so I don't know how to prevent bundling in every extern dll into my deployment binaries.

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

Very nice post with a descriptive example of usage. Real life examples always are adding additional value to an example. I'm a big fan of design patterns too so I enjoyed this post.

Personally I'd change inheritance to composition for ShouldDelete method, but once again great article.

Collapse
 
brunobck profile image
Bruno

Could you make an example?

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

Sure.

Instead of inheriting from a base class and method overriding like here:

public class MyClass:BaseClass
{
    public override void Foo()
    {
       base.Foo();
    }
}

You add the class variable of a given class and calls it's method if needed.

public class MyClass
{
    private BaseClass _baseClass = new BaseClass();

    public void Foo()
    {
       _baseClass.Foo();
    }
}

More about the comparison between Inheritance and Composition you can find here. Take a closer look at the table at the end of article.

Collapse
 
bgadrian profile image
Adrian B.G.

Generally speaking, I am curious, how can one do automatic tests on this pattern?

First step would be to test each strategy implementation I presume.
Second would be to test if the correct strategy was selected based on the input?

Collapse
 
sam_ferree profile image
Sam Ferree

That’s how I’d do it, Unit test the concrete implementations, and selection, then Integration test both.

Collapse
 
boumboumjack profile image
Boumboumjack

I used to do similar things, but for task that have a very similar output/process, I tend to use a "DeleteOption" argument to keep the process centralised. I find it easier to then save the parameters with a DataContract.

I guess you use a dictionnary to select the correct function? I usually do this for very unrelated tasks:

IDictionnary <myEnum.MethodName, myIAction>

. Then it is as well quite easy to save it.

Collapse
 
kylegalbraith profile image
Kyle Galbraith

Great write up Sam! This is one of my favorite programming patterns to use and it can be applied it a lot of different languages. I wrote a post about the strategy pattern as well if folks are looking for more examples.

Collapse
 
brunobck profile image
Bruno

Nice article sir! Thank you.

Collapse
 
dhavalcharadva profile image
Dhaval Charadva • Edited

We are implementing data integration with netsuite. We are in process of using any good design pattern. What is best suggestion for data integration.

Domain is e-commerce.