Re: What databases have taught me

From: Robert Martin <unclebob_at_objectmentor.com>
Date: Tue, 27 Jun 2006 22:48:42 -0700
Message-ID: <2006062722484275249-unclebob_at_objectmentorcom>


On 2006-06-23 11:29:40 -0700, frebe73_at_gmail.com said:

> In a recent thread Robert Martin suggested a similar class hierachy:
> Employee
> - Salaried employee
> - Hourly employee
> - Commissioned employee
>
> The Employee interface should have a calculatePayment method and the
> subclasses should have different implementations.
>
> This might look like a natural thing to do and it probably is while the
> problem solution is small and not very complex. But imagine that
> depending on what union branch the employee belongs too, the salary is
> calculated differently in some aspectes. Now you have to repeat this
> logic in all three implementations of calculatePayment. This is one
> example of problem you will encounter when you start with on dimension
> and later have to handle multiple dimensions.

You use the Bridge pattern.

    |PaymentMethod|<-------|Employee|------>|UnionMembership|

           A                                        A
           |                                        |
    +------+------+                       +---------+--------+
    |             |                       |                  |
|HourlyMethod| |SalariedMethod|    |NonMember|         |Local705Member|

So, in fact, you don't have to repeat the implementation. Instead you create a different hierarchy that represents the union branch and pass the calculated pay to it.

To see this in action in much more detail I refer you to the Payroll solution in my book "Agile Software Development: Principles, Patterns, and Practices"

-- 
Robert C. Martin (Uncle Bob)  | email: unclebob_at_objectmentor.com
Object Mentor Inc.            | blog:  www.butunclebob.com
The Agile Transition Experts  | web:   www.objectmentor.com
800-338-6716                  |
Received on Wed Jun 28 2006 - 07:48:42 CEST

Original text of this message