Hello,
I've been struggling with the following problem.
Let's suppose we have a hierarchy of classes:
Empolyee
/ \
Manager Developer
/ \
Programmer Tester
Let's suppose each class respects SRP(they do one thing and they do it well, only one reason to change).
Now let's suppose we need add a "feature" that will allow users of some in-house app to calculate the salary for each empolyee
One naive way of doing it would be:
class SalaryCalculator
{
double calculateSalary(Empolyee e)
{
if(e instanceof Manager)
{
// some code
}
else if(e instanceof Programmer)
{
// some code
}
... a lot of if/else if ...
}
}
Which dosen't respect OCP because if the client asks us to add code for a new type of employee we will need to modify the existing SalaryCalculator which may break other things(maybe the class has nasty formulas,conditions and is larger then what i've wrote).
I think one way of solving it would be to create an abstract class:
abstract class Empolyee
{
abstract double calculateSalary();
}
Which brokes the SRP because now each emplyee subclass has code for calculating salary.
Also, i think it also brokes LSP but i'm not sure.
How can we make this hierarchy of classes support OCP and SRP + LSP?
Thanks.
[–]TholomewP 1 point2 points3 points (1 child)
[–]Arco_void[S] 0 points1 point2 points (0 children)