dimanche 30 mai 2021

Are all interface compositions considered as strategy pattern

Using Strategy pattern, we can decouple behaviors using interfaces. Behaviors are moved in to interface which can have multiple implementations. Clients can have has-a-relationship to interface and can refer to any of its implementation in runtime.

In all explanations, it is mentioned as a solution to solve issues related to inheritance, where two classes in different branches in inheritance hierarchy need same behavior.

I think we can use the same approach to decouple behaviors from classes even when inheritance hierarchy is not present. A single class having a reference to interface can achieve multiple behaviors based on the implementation it is referring at runtime.

Can we call such use cases as an implementation of Strategy pattern? If so, are all has-a-relationships to interfaces considered as strategy pattern?

Aucun commentaire:

Enregistrer un commentaire