Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
OOP Design Pattern: Strategy
  • Deque
    Posts: 78
    Design Pattern

    Hello programmers. This is the start of a series about Design Pattern. Design Patterns are a standard solution for problems you have to deal with every now and then. Especially object oriented programming (OOP) has shortcomings you need to handle somehow.
    The right pattern in the right situation will help to produce code that is easily maintainable, understandable and has a lesser change to produce bugs.

    The Strategy Pattern

    Inheritance

    let's imagine we want a little bird simulation where we need every kind of bird behaviour. Our first idea might be to make an abstract class for bird that every sort of bird has to extend. Since we know that most birds can fly and swim, we implement those methods already. The method display is different for every bird, but has to be implemented by them, so we make it abstract.

    Our first bird type is a dug, the second a swan. No problem with that. They can use the predefined swim() and fly() methods and only implement display().

    http://s1.directupload.net/images/110704/abw7eeby.png

    Now we also want to add species like penguin, flamingo and ostrich. Those birds can not fly. Therefore they have to implement fly() to let it empty (if they inherit the default implementation they would be able to fly() which is wrong).
    The penguin is also capable of diving. I configure this as a different type of swimming, so we override this method too.
    An ostrich does not swim I think. This bird has to implement swim() and let it empty. The next problem is that it can run instead. So we need a new feature or skill for our birds: the method named run(). Now look at our diagram and say how many classes we have to change to implement the run() skill.

    http://s7.directupload.net/images/110704/3cu9mskj.png

    If we add 50 more birds like that implementing new skills will be a pain in the ass. Also we have a lot duplicated code, e.g. for all birds that can dive we override the swim method and implement the same code. That can not be a good solution. Let's think about another.

    Interfaces

    What if we use interfaces, so we don't need to implement so many empty methods?

    http://s7.directupload.net/images/110704/q2kvopol.png

    I somehow have the feeling this is even more complicated than before and it still does not avoid duplicated code.

    Strategy

    Since all we have done before is crap, we move on to the strategy pattern. We encapsulate now the behaviour instead of the bird types.

    Design principle 1: Identify the variable aspects of an application and separate them from the stable ones.

    http://s7.directupload.net/images/110704/hwuawqfp.png

    Our actual bird class will look like this:

    http://s1.directupload.net/images/110706/mla84b6t.png

    The methods might look like this (pseudocode):

    performFly() {
    fb.fly();
    }

    performSwim() {
    sb.swim();
    }


    The behaviour types are not fixed, so you can change the behaviour very easily on runtime. I.e. like this:

    sb = new DiveBehaviour();
    ...
    sb = new NormalSwimBehaviour();


    You might also notice that there is no duplicated code anymore and adding new features is easy.

    To summarize a Strategy Pattern defines a family of algorithms, encapsulates every algorithm and makes it this way exchangeable.

    Design principle 2: Composition is better than inheritance.

    Sources: Notes from my study

    Final note: I am not a bird expert and won't take any responsibility for wrong concepts about birds.
  • Sh3llc0d3
    Posts: 1,910
    Good write-up. I enjoyed studying UML (Unified Modelling Language) quite fun and beneficial if your wanting to pull off any big OOP projects.
  • Deque
    Posts: 78
    Thx. Every programmer should be capable to understand UML. It is easier and more clear explaining concepts with it than with source code. But specifications are mostly no fun.
  • Sh3llc0d3
    Posts: 1,910
    I hated learning the standards/specs while doing UML and other modelling/project management strategies.
  • Xin
    Posts: 3,251
    Nice, could have done with this back when i was doing this in my computing course.
    Xin