PPP Payroll Example
From: Steve Hutton (shutton_REMOVE__at_attbi.com)
Date: 02/29/04
- Next message: Dirk Raffel: "Re: What pattern to apply?"
- Previous message: Lee Riemenschneider: "Re: Relational model versus object model"
- Next in thread: H. S. Lahman: "Re: PPP Payroll Example"
- Reply: H. S. Lahman: "Re: PPP Payroll Example"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 29 Feb 2004 07:29:17 GMT
I've been re-reading Robert Martin's Principles,
Patterns and Practices book and I have a question
about the Payroll Case Study.
The design uses an Employee class, which
contains an PaymentClassification object.
PaymentClassification is actually an abstract
base class - each employee actually contains one
of three possible concrete subclasses:
- SalariedClassification
- HourlyClassification
- CommissionedClassification
Since hourly employees have timecards, HourlyClassification
exposes an AddTimeCard() member function. Similarly
CommissionedClassification() exposes AddSalesReceipt().
The base class does not support these member functions,
since they are not common to all payment classifications.
This means that a dynamic cast is required to e.g.
add a TimeCard:
PaymentClassification* pc = e->GetClassification();
if (HourlyClassification* hc = dynamic_cast<HourlyClassification*>(pc)
{
hc->AddTimeCard(new TimeCard(itsDate, itsHours);
}
else
{
throw("Tried to add timecard to non-hourly employee");
}
An alternative design might push AddTimeCard() up to a virtual
function in the base class, providing a default implementation
that throws. This would simplify client code to:
PaymentClassification* pc = e->GetClassification();
pc->AddTimeCard(new TimeCard(itsDate, itsHours);
If HourlyClassification changed in the future to require an
additional method, I can see an advantage to the dynamic cast
approach - the base class doesn't have to change. But for the
initial design this isn't an issue.
Which of the two designs do you prefer and why?
Steve
- Next message: Dirk Raffel: "Re: What pattern to apply?"
- Previous message: Lee Riemenschneider: "Re: Relational model versus object model"
- Next in thread: H. S. Lahman: "Re: PPP Payroll Example"
- Reply: H. S. Lahman: "Re: PPP Payroll Example"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|