5

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!

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That was my hunch. I'll wait for an hour to see other opinions. BTW - Isn't this more of a delegate than strategy? p2p.wrox.com/book-professional-php-design-patterns/…
Using the "Delegation pattern" here means, the Employee does not calculate the salary by itself but delegates this to the strategy object. It does not say anything about different salary calculation models. "Strategy pattern" means you will have different subclasses, one for each type of salary calculation. It does not define where the calculation takes place (the different subclasses may just provide metadata to help "Employee" doing the right calculation). Both patterns will be applicable here, thouh I thing "strategy" is what you interviewer was after.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.