An organization has many members.
On the first of the month every member receives a paycheck.
Unfortunately, the way the gross amount of the paycheck is calculated depends on if the type of the member.
Salaried members receive a twelfth of their annual salary.
Hourly members receive an amount equal to the number of hours worked times the member's hourly rate.
Commissioned members receive 10% of their monthly sales.
We can introduce subclasses of Member to represent these different types. Here's a preliminary design:
Assume the following declarations of the member subclasses have been made:
Member.java
The control driven solution is embodied by the following implementation of calcPayroll:
Now suppose we want to add a new subclass of Member who will be paid for each contract completed for the amount specified by the contract:
class ContractMember extends Member {
Contract[] contracts;
// etc.
}
Where:
class Contract {
double amount;
// etc.
}
What changes need to be made to the existing code?
The problem is that the adding the new Member subclass requires a modification to the Organization class.
This violates the Open Closed Principle.
To overcome this problem we modify the design:
Here's the implementation: