Nati
Nati

Reputation: 1032

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 ?

Upvotes: 1

Views: 1554

Answers (2)

Supermalf
Supermalf

Reputation: 336

  • What is a Strategy? A strategy is a plan of action designed to achieve a specific goal;
  • “Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.” (Gang of Four);
  • Specifies a set of classes, each representing a potential behaviour. Switching between those classes changes the application behaviour. (the Strategy);
  • This behaviour can be selected at runtime (using polymorphism) or design time;
  • Capture the abstraction in an interface, bury implementation details in derived classes;

enter image description here

  • An alternative to the Strategy is to change the application behaviour by using conditional logic. (BAD);
  • Using this pattern makes it easier to add or remove specific behaviour, without having to recode and retest, all or parts of the application;

  • Good uses:

    • When we have a set of similar algorithms and its need to switch between them in different parts of the application. With Strategy Pattern is possible to avoid ifs and ease maintenance;
    • When we want to add new methods to superclass that don’t necessarily make sense to every subclass. Instead of using an interface in a traditional way, adding the new method, we use an instance variable that is a subclass of the new Functionality interface. This is known as Composition : Instead of inheriting an ability through inheritance the class is composed with Objects with the right ability;

Upvotes: 2

tallseth
tallseth

Reputation: 3675

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.

Upvotes: 5

Related Questions