(design-pattern) strategy
Purpose
We do not want to rewrite the same methods in different lower classes.
Concept
Constructs multiple intefaces for variant methods in a upper class so that we can have multiple combinations to create multiple classes inherit from this upper class with shared methods.
For example, given lots of varieties of dogs, there are four dogs with different behaviors according to their characteristics:
- stupid dog: walks for losing its way
- smart dog: walks to anywhere it want
- anxious dog: kiss their owner for stroking to feel relieved
- calm dog: kiss their owner to express intimacy
Given the characteristics above we may have four type of dogs:
- stupid and anxious dog: Chihuahua
- stupid and calm dog: Bulldog
- smart and anxious dog: Sheepdogs
- smart and calm dog: Retriever
Given the OOP concepts, we have choices as follow:
- create an upper class and then create four classes inherit from it
- create an polymorhpic class with four type
Given the four characteristics map four behaviors, we need to write logics or overwrite the methods in the upper class. What if we want Chihuahua to share their kissing behavior with Sheepdogs? We need the concept of strategy and the UML as follow:
What?
class Dog { constructor(walkBehavior, kissBehavior) { this.walkBehavior = walkBehavior; this.kissBehavior = kissBehavior; } kiss() { console.log(this.kissBehavior.kiss()); } walk() { console.log(this.walkBehavior.walk()); } } class KissBehavior { kiss() { throw new Error('Must'); } } class WalkBehavior { walk() { throw new Error('Must'); } } class KissForStroking extends KissBehavior { kiss() { return "kiss for stroking"; } } class KissForIntimacy extends KissBehavior { kiss() { return "kiss for intimacy"; } } class WalkForLost extends WalkBehavior { walk() { return "walk for lost"; } } class WalkToAnyWhere extends WalkBehavior { walk() { return "walk to anywhere"; } }
and create instance from these four classes
chihuahua = Dog.new(WalkForLost.new, KissForStroking.new) chihuahua.walk chihuahua.kiss