Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Strategy Pattern and Inheritance

There is a same concept for Strategy Pattern and Inheritance, so I can implement Strategy Pattern with Inheritance that sounds it is simpler and cleaner than Strategy Pattern.

Startegy Pattern:

class IBase
{
public:
    virtual void processAction(void *data) = 0; // pure virtual
}

class Worker: public IBase
{
public:
    virtual void processAction(void *data)
    {
        // define logic
    }
}

Inheritance:

class Base
{
public:
    virtual void processAction(void *data) {}
}

class Worker: public Base
{
public:
    virtual void processAction(void *data) override
    {
        // define logic
    }
}

My question is what is the difference between them? or when should I use Strategy Pattern or Inheritance?

Link: Strategy Pattern

like image 911
Reza Ebrahimi Avatar asked Sep 07 '14 12:09

Reza Ebrahimi


People also ask

What is the difference between pattern and strategies?

State pattern helps a class to exhibit different behaviors in a different state. Strategy Pattern encapsulates a set of related algorithms and allows the client to use interchangeable behaviors through composition and delegation at runtime.

What is the difference between strategy and factory pattern?

A factory pattern is a creational pattern. A strategy pattern is an operational pattern. Put another way, a factory pattern is used to create objects of a specific type. A strategy pattern is use to perform an operation (or set of operations) in a particular manner.

What is the difference between strategy and bridge pattern?

Strategy Pattern is used for Behavioural decisions, while Bridge Pattern is used for Structural decisions. Brigde Pattern separats the abstract elements from the implementation details, while Strategy Pattern is concerned making algorithms more interchangeable.

Is design pattern an inheritance?

The union design pattern is a structural pattern that depicts the inheritance relationship between a superclass and its subclasses. The superclass is an abstract representation of the union of all the subclasses. Due to this polymorphism, the subclasses can thus be used wherever the superclass is required.


2 Answers

Imagine you design a Cache. The cache can have options regarding

  • eviction policy (LIFO, FIFO, LRU)
  • expiration policy (after read, after write)
  • maximum size (number of elements, memory usage)

Now imagine you want to let the user of your cache choose any of these options, and you use inheritance. You'll need 3 * 2 * 2 = 12 different classes:

  • LifoAfterReadNumberOfElementsBasedCache,
  • LifoAfterReadMemoryBasedCache,
  • etc.

Each of the 4 LifoXxx classes will have to implement the same algorithm to implement the LIFO strategy. Same for the other ones.

Implement it with the strategy pattern instead of inheritance, and you'll have a single Cache class, and 7 strategy classes (one for each strategy), that you can combine however you want, and without code duplication.

And you can also let the user of your Cache define its own strategy, that you haven't anticipated, and let him combine it with other strategies without needing to create a whole lot of new subclasses.

like image 107
JB Nizet Avatar answered Sep 21 '22 05:09

JB Nizet


Strategy is a pattern of OOP and Inheritance is a principle of OOP. Strategy is implemented using (or to support) Inheritance (actually, interface generalization).

Inheritance

  • Inheritance is used to achieve all benefits of OOP (it eventually leads to better readbility\maintainability\structure of code).
  • Inheritance is used to form many other patterns, not only Strategy
  • Inheritance is sometimes a limitation because of its static compile-time nature

Strategy

  • Strategy assumes to have different implementations which can be replaced in runtime (or configured in a flexible way). So, your first example is not strategy as well.
  • In general, composition (which is implemented by Strategy) as a way of logic reuse is preferred over inheritance. At first, it provided dynamic polymorphism. At second, it has less implementation limitations like multi-class inheritance, etc.
  • Strategy corresponds to "some changeable algorithm" in terms of DDD, thus has real impact on domain modelling.
  • Strategy, as a pattern itself, provides a language ("pattern language") for communication between developers.

Moreover, according to your example:

  • Base abstract class and interface have two different semantics, thus inheritance in your example have different semantics too. First is about implementation of public contract. Second is about generalization as an extraction of common properties and methods.
like image 27
Valentin P. Avatar answered Sep 20 '22 05:09

Valentin P.