Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP Design Question

Got this interview question which i'm wondering about:

A software company designed an app that manages employees and, among other functions, calculates Salary.

The current structure which fits the customer's requirements is:

abstract Class Employee;
Class Manager extends Employee;
Class Engineer extends Employee;

The customer would now like to add the ability to support different types of salary calculations for employees who work on an hourly wage, monthly salary. Both Engineer and Manager can be either.

The customer also notified the software company that they will add a number of other types of salaries in the future.

The Question - How would you design this? Does if fall in any design pattern solution?

Thanks!

like image 434
Turd Avatar asked Sep 25 '11 06:09

Turd


2 Answers

Apply the strategy pattern:

http://en.wikipedia.org/wiki/Strategy_pattern

Make "Salary_Calculation" a strategy associated to Employee. "Salary_Calculation" should be an interface or an abstract base class, and each salary calculation model is a subclass of that.

like image 55
Doc Brown Avatar answered Oct 21 '22 17:10

Doc Brown


Add a SalaryCalculator interface and instantiate a SalaryCalculator object during the Employee object's construction using the salary type. The SalaryCalculaotr object will take care of salary calculations for each salary type.

like image 22
John W Avatar answered Oct 21 '22 16:10

John W