PPP Payroll Example

From: Steve Hutton (shutton_REMOVE__at_attbi.com)
Date: 02/29/04


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



Relevant Pages

  • Re: PPP Payroll Example
    ... > I've been re-reading Robert Martin's Principles, ... > Since hourly employees have timecards, HourlyClassification ... > The base class does not support these member functions, ... > An alternative design might push AddTimeCardup to a virtual ...
    (comp.object)
  • Re: Did I write a good (efficient) program?
    ... The priniples of object-oriented design do not include ... the use of inheritance to overcome inadequate or faulty ... modifying a base class may be ... principles to those of the paradigm. ...
    (comp.lang.cobol)
  • RE: Capturing an employee in a team at a point in time
    ... you will have a robust design that will not need altering. ... Access Query Grid interrogates the Windows setting and as ... you have entered a date in US m/d/y format. ... I would like to design a db that captures employee data. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Looking for PCB layout designer
    ... at a $5 adder to go to six layer), but that does *not* mean that they ... Descrimination against smart people (as well as ... for a digital design type job one question ... Are you saying that every employee should have identical skills? ...
    (sci.electronics.design)
  • Re: Still New to Access DB
    ... The tab order in the design view on the subform only is correct, ... Employee, can you add more than 1 record to it. ... EmpID ...
    (microsoft.public.access.tablesdbdesign)