DEV Community

Discussion on: Using the Strategy Pattern (Examples in C#)

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.