0

enter image description here

From a picture, Can i use inheritance instead of implement an interface?

I mean change from "ConcreteStrategyA and ConcreteStrategyB implements Strategy Interface" to "ConcreteStrategyA and ConcreteStrategyB extends Strategy Class"

Is it still work well or have some problem?

If it still work well my next question is "Why most people prefer to use interface?"

fronthem
  • 4,011
  • 8
  • 34
  • 55

4 Answers4

4

Well technically from a Design pattern perspective with the Strategy pattern, the concrete Strategies need to implement (I mean write code for, not the interface implements thing) a common contract which the Strategy Context is aware of. This is the primary backbone of Strategy pattern philosophy. The adherence to the common contract is what allows the Strategy context to replace the concrete strategies based on some runtime feature. This pattern ideology is what we loosely call Polymorphism in OOP parlance. Now in Java you can implement his polymorphic strategy either as an interface or as inheritance. For interface you have given the example in the question itself. For inheritance as long as the contract holds between subclasses (something like a base abstract class with an abstract contract which subclasses implement to provide concrete strategy implementations) you can implement Strategy pattern in inheritance as well.

Now thinking about it from OOP perspective. For OOP inheritance is something which a subclass inherits from a super class. The subclass automatically owns and thus demonstrates that inherited generic behavior but it has a choice to make that behavior more specific to its own type. Thus multiple subclasses can override the same behavior and make bits of it more specific to their use. But this chain becomes cumbersome to manage when it gets too long or when subclasses try to inherit the behaviors which don't apply to them logically.

Thus it makes more sense to implement Strategy pattern using interfaces as against inheritance.

Nazgul
  • 1,892
  • 1
  • 11
  • 15
3

Absolutely. Inheritance is most often used with an abstract base class, when you want your derived strategies to share some common code.

People prefer to use interfaces or abstract classes over concrete base classes because :

  • With a Dependency Inversion approach, a class needs loose coupling to its Strategy. It only needs to know that the Strategy fulfills a contract, but doesn't want to know about its implementation details. Interfaces and abstract classes are an elegant and minimal way to define a contract without specifying the implementation.

  • It doesn't make sense to instantiate the base Strategy class most of the time, because it's a general, abstract concept -- in fact, it's better if you forbid instantiating it.

guillaume31
  • 13,738
  • 1
  • 32
  • 51
0

There are no technical problems.

However, a class can only extend one base class but it can implement multiple interfaces. So if you want to, let's say, change your inheritance structure in the future it is easier if you choose to implement an interface instead.

Emil Lundin
  • 577
  • 5
  • 14
0

As you know a design pattern is "a general solution to a commonly occurring problem". It just describe a general solution without indications concerning implementation details.

If your problem requires a class in place of the interface, there is nothing wrong replacing it with a concrete (or abstract) class.

Using an interface in the pattern UML is a way to say: "you have to expose this set of public methods".

So, no problem using your approach. As an alternative you could leave the Strategy interface and implement it in a StrategyImpl class, then you can inherit this class in your ConcreteStrategyA and ConcreteStrategyB classes.

davioooh
  • 23,742
  • 39
  • 159
  • 250