Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview question on Class design

Recently I Attended an interview. This question was asked.

This is the scenario.

We have two type of employees. Regular and contract employee. Regular employees will be paid on a fixed basis at the end of the month. Contract employees will be paid weekly based on the number of hours they worked.

Managers will be assigned to these employees for supervision. A manager may have regular and contract employees under him.

This application will calculate the payroll for these employees.

They asked me to come up with the class design for this situation.

What answer is the interviewer expecting from me?. Pointers in this direction will be highly appreciated.

like image 942
Ananth Avatar asked Mar 28 '11 09:03

Ananth


2 Answers

They are testing that you understand some basic tenets of good OO design. Specifically, they seem to be looking for:

  1. An understanding of Polymorphism (by using functionality of each Employee at abstract base level)
  2. An understanding of Specialisation (by extending the functionality of the base Employee class to produce different behaviour)
like image 166
Steve Mayne Avatar answered Oct 11 '22 08:10

Steve Mayne


This is a question which would test you, if you have an understanding of the separation of concerns (calculating the wage of an employee is not a responsability of the employee itself), and to see if you understand abstractness and polymorphism: there are (at the moment) 2 different ways to calculate a wage. You could implement some kind of strategy design pattern in order to perform the calculation, so that each implementation of the algorithm is specified in a separate class. This helps maintainability and extension (if another kind of algorithm would be needed).

public class Employee
{
    public bool IsContractEmployee
    {
       get;
       set;
    }
}

public class WageCalculator
{

    private abstract class WageCalculAlgorithm
    { 
        public virtual decimal Calculate( Employee emp )
        {
             // regular calc goes here
        }
    }

    private class ContractorCalculator : WageCalculAgorithm
    {
        public override decimal Calculate( Employee emp )
        {
            // contractor calc goes here
        }
    }

    public static decimal CalculateWageFor( Employee emp )
    {
       if( emp.IsContractEmployee )
            return new ContractorCalculator().Calculate(emp);
       else
            return new WageCalculAlgorithm().Calculate(emp);
    }
}
like image 36
Frederik Gheysels Avatar answered Oct 11 '22 06:10

Frederik Gheysels