Search code examples
design-patternsstrategy-pattern

Strategy Pattern in "real life"


I'm trying to transform wikipedia examples of strategy pattern to real life problems, but I'm not sure if my scenario does require such pattern.

say we a have a service that gets updates from several clients, and needs to do some processing and move these updates forward, depends on their size. i would like to keep the service as simple as possible, and open for future updates formats (from other clients) as well.

I thought I could let the updates themselves decide how to be processes and moved forward, by using this pattern, and let the service be a simple as possible. something like this:

public class Service {
    void processUpdate(Update myUpdate) {

        myUpdate.process();
        myUpdate.moveForward();
    }
}         

Am I wrong ? how (where...) do I assign a strategy to each update ?


Solution

  • The easiest way to do a bad job with design patterns is to learn a pattern, and then go trying to find places to put it. I did this a few times when I was first learning, and the results were quite frustrating.

    The strategy pattern is a solution for a specific shape of problem. That problem is " I need to do things that are basically the same, but there are some variations in the middle". So for now, remember how to do a strategy pattern, and when you see a problem that suggests it, use it.