Example taken from : Agile software development : principles, patterns and practices
A new employee is added by the receipt of an AddEmp transaction. This transaction contains the employee's name, address, and assigned employee number. The transaction has three forms:
AddEmp <EmpID> "<name>" "<address>" H <hourly-rate>
AddEmp <EmpID> "<name>" "<address>" S <monthly-salary>
AddEmp <EmpID> "<name>" "<address>" C <monthly-salary> <commission-rate>
proposed employee class
Add employee transaction
My question would it be "better" if instead of subclassing AddEmployeeTransaction
we have a factory for salary classification and pay schedule?
If better is too vague a term, what are disadvantages of my design over the one proposed by Uncle Bob?
Say inside EmployeeTransaction
we have SalaryClassificationFactory
and we call SalaryClassificationFactory.get(SalaryDetails d)
where,
enum SalaryId{H,S,C};
struct hourly
{
int hourlyRate;
};
struct monthly
{
int monthlySalary;
};
struct commissioned
{
int salary;
int commisssionRate;
};
struct SalaryDetails
{
SalaryId kind;
union SalaryUnion
{
hourly h;
monthly s;
commissioned c;
}
};