Description:
This is a type of behavioral design pattern. There is an operation in a parent class which has few sub-tasks and all or few sub-tasks behavior may be changed by its child classes at run time.
It means we are defining a structure of an operation / algorithm and we are allowing child classes to implement their own behavior.
Example:
Let’s take an example of bank account. We need to calculate interest rate at the end of some period. so there are few steps we need to perform.
- Need to retrieve balance from database / source
- Need to calculate interest rate with balance
- Update balance into database / source
So here, step 1 and 3 will remain same for all types of account however, step 2 may differ so each type of account will implement its own behavior.
Structure:
Advantages:
- When algorithm behavior will be changed, then each sub classes will handle their behavior by themselves.
- Code duplication will be reduced.