Strategy Pattern
“Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. ”
Design Patterns: Elements of Reusable Object Oriented Software
The 3 attack() methods represents a family of algorithms. Each algorithm is encapsulated in an independent class.
The 3 classes implement the IWeapon interface, which is what renders the algorithms interchangeable.
The class StarwarsCharacter by using the IWeapon Interface it could access any algorithm without depending on the class.
This design is very powerful, it makes it possible to add algorithms as you go when needed without altering any existing code.
So in case the character is a Clone they could attack using a Blaster and if it is a Jedi they could attack using a Lightsaber.
But what if it is Chewbacca ?
In this case all we have to do is add a new class Bowcaster that implements the IWeapon Interface and overrides the method attack().
And now Chewbacca could use his Bowcaster.
Link to the source code
Strategy Pattern
“Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. ”
Design Patterns: Elements of Reusable Object Oriented Software
The 3 attack() methods represents a family of algorithms. Each algorithm is encapsulated in an independent class The 3 classes implement the IWeapon interface, which is what renders the algorithms interchangeable The class StarwarsCharacter by using the IWeapon Interface it could access any algorithm without depending on the class.
This design is very powerful, it makes it possible to add algorithms as you go when needed without altering any existing code.
So in case the character is a Clone they could attack using a Blaster and if it is a Jedi they could attack using a Lightsaber.
But what if it is Chewbacca ?
In this case all we have to do is add a new…
Top comments (1)
Sounds like a DI??
May be familiar....