I am confuse between the best way to organize dependency between multiple classes
assume i have the following classes
Employee,
Salary,
DataAccess
Should i go for:
Option1
Employee emp = new Employee();
Salary sal = new Salary();
DataAccess data = new DataAccess();
sal.Calculate(emp);
data.Save(emp);
or Option2
Employee emp = new Employee();
Salary sal = new Salary();
sal.Calculate(emp); //once salary has been calculated salary object will initialize data access class to do the actual saving.
or Option 3
Employee emp = new Employee();
emp.Calculate(); // employee object will encapsulate both the salary and data access object
Usually Employee HAS-A salary. So I’d go with something like:
You can create a generic class that would fetch all of the salary calculation business rules in it(from a data source), and calculate salary for employees based upon employee attributes.
–EDIT–
This is in response to your comment.
Your
hrmsobject(if any) should then encapsulate theEmployee, and provide aCalculateSalaryoption.How about something like:
Here your Salary class would inherit from your abstact BaseSalary class, that would contain the business rules, and would perform the
Calculate()ions. Therefore, thenew Salary(someStartPointForSalaryIfAny)would perform the calculation internally.To save your object you can have: