Search code examples
classoopmvpaggregationcomposition

Is the relation between Salary, Earnings and Deductions composition, aggregation or generalization?


In a payroll system I have a class called Salary. To prepare salary we need Earnings and Deduction details of employees.

For eg:

Earnings may be: Basic Salary, Overtime, Allowances etc. Deductions may be: Fines, Loan installments, Welfare contributions etc.

I just want to know what's the relationship between these Salary, Deduction and Earning classes. It seems that the Deduction and Earning is a part of salary so it may have a composition/aggregation relationship.

However you calculate salary as : [Salary = Earnings - Deductions].

Also SalaryID, EmployeeID and Date fields may be common for both Earning and Deduction classes which could be inherited from Salary.

So that could you please clarify what relationships the above classes have in this scenario? I'd also appreciate if you could show an example of how those relationships are implemented in classes.

EDIT: Im trying to develop a win forms system employing MVP pattern and my language is C#.net. Basically I have employee attendance in the database so using two SPs I generate earnings and deductions. Then the user is give an UI to edit figures or to enter random figures like special allowances, fines etc. Finally all deduction and earning details will be saved in a single table called SalaryTrans [sal_id, emp_id, pay_period, basic_salary,overtime_payment, allowances, fines , other_deductions, ..., gross_salary].


Solution

  • How about the following structure:

    enter image description here

    Earnings and Deductions are abstract classes and inherited by concrete concepts.

    In order to calculate a Salary for a month, you just need to create a set of Earnings and Deductions and to assign them to a corresponding Salary. The Salary's amount is calculated automatically (therefore "derived" att), so no worry about that.

    The most important decision is how to create Earning and Deduction objects for a month. A possible way is to add a method in Salary that creates Earnings and Deductions according to a well defined algorithm and connects them with itself. Salary object has all the info needed to calculate this (Person, month). I like this method, because all the knowledge remains localized, the whole structure remains nicely closed and encapsulated.