Strategy Design Pattern - Type Script
Ducks Simulation Coding
Lets write a Duck Simulation
program to understand how Strategy Pattern
can be used.
Link to code - https://github.com/smarigowda/strategy-pattern-ducks-typescript
Lets say the application has a UI which shows large variety of Ducks swimming and making quaking sounds.
Lets say following types of Ducks are supported by the application.
- Mallard Duck
- Redhead Duck
- Rubber Duck
- Dummy Duck
Program should allow adding new type of duck in future, that is the program should be Open for Extension
Assume that each type of Duck has its own quaking
and flying
behaviour.
Flying Behaviour for each Duck is given below:
- Mallard Duck - Flys with wings
- Redhead Duck - Flys with wings
- Rubber Duck - Can't fly
- Dummy Duck - Can't fly
Quaking Behaviour for each Duck is given below:
- Mallard Duck - Quakes
- Redhead Duck - Quakes
- Rubber Duck - Squeaks
- Dummy Duck - No Sound
Interfaces
Create two interfaces. One to encapusulate Flying behaviours and another to encapsulate Quaking behaviours.
interface IFlying {
fly()
}
interface IQuaking {
quake()
}
Create concrete implementations of the above interfaces for each type of behaviours.
class Quake implements IQuaking {
quake() {
// makes real quake sound
}
}
class Squeak implements IQuaking {
quake() {
// makes squeaking sound
}
}
class Mute implements IQuaking {
quake() {
// makes no sound
}
}
class FlyWithWings implements IFly {
fly() {
// flys with wings
}
}
class NOFly implements IFly {
fly() {
// can't fly
}
}
Duck Types
Create an abstract Duck
which holds references to flying
and quaking
behaviours, provides methods to set the behaviours and methods to perform them.
Create concrete Duck types which extends abstract Duck type. Each concrete type uses the setter methods provided by the abstract Duck to set the behaviours to concrete types.
Concrete Duck types do not have the knowlege of behaviours. Behaviours are encapsulated using Interfaces - That's the Strategy Pattern
in action. Each Duck types just know about the interfaces. They can set the behaviours at run time.
Adding new types of behavious is easy, just create another concrerte type implementing the interface.
So the program can be extended without modiying the exisitng code ! Open for Extension
Access Modifiers
Note how the public, private and protected access modifiers are used to achieve encapsulation.
- Public access to the methods
performFly()
andperformQuake()
. Public access is needed here so the users can perform actual behaviours. - Private access to the variables
flying
andquaking
, these variables are going to hold references to the behaviours. They are used only inside the abstract Duck class hence private. - Protected access to the methods which are used to set the Flying and Quaking behaviours,
set*
methods. These methods should only be used inside a derived class and they should not be availale outside hence protected.
Install and Run
Clone my GitHub repository -
git@github.com:smarigowda/strategy-pattern-ducks-typescript.git
Install node_modules -
npm install
Run the program -
npm run test