Article View: comp.lang.java
Article #377Re: Refactoring book + one to many implementation
From: Mark Fussell
Date: Fri, 12 Nov 1999 00:00
Date: Fri, 12 Nov 1999 00:00
64 lines
3180 bytes
3180 bytes
abulka@my-deja.com wrote:
> Mark Fussell <mark.fussell@chimu.com> wrote:
> > actually you should be able to do the above with:
> >
> > void removeOrder( Order order ) {
> > order.setCustomer(null);
> > }
> >
> > which would keep all the association-controlling logic in the
> > 'setCustomer' method.
>
> I don't think this would work because the Order class / controlling
> side is not being told WHICH customer to no longer point to. The hash
> set _orders that lives in Customer needs to be updated, somehow.
>
> Which was exactly my point, the Customer side needs to be:
>
> void removeOrder( Order order ) {
> _orders.remove( order ) // this line is inelegant, but needed.
> order.setCustomer(null); // this line is ok, calls controlling side
> }
Please review the pattern's example code again or simply try out the
version I gave. Order::setCustomer already contains the functionality
you are trying to put into 'removeOrder' -- it already knows how to
remove itself from its (only) customer's collection of orders. Remember
that an Order can only have a single customer because of the cardinality
rules (Martin gave a different example for the many-to-many
implementation).
I am not saying you can't do it multiple ways, but I would say the
simple:
void removeOrder( Order order ) {
order.setCustomer(null);
}
works correctly and is probably in the spirit of what Martin was trying
to show.
Now I would agree that you might want to enhance the above to do a
simple integrity check:
/**
*@require order.customer = self
*/
void removeOrder( Order order ) {
order.setCustomer(null);
}
Since it is a bit peculiar to allow a customer to clear out an order
that wasn't for that customer. You could also simply ignore a request
to remove an order that doesn't belong to this customer if you want to
be more tolerant.
void removeOrder( Order order ) {
if (!_orders.contains(order)) return;
order.setCustomer(null);
}
But in either case, Order::setCustomer is still controlling the
association.
--Mark
mark.fussell@chimu.com
Technical Details
Message-ID:
<382D1653.EF337931@chimu.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<80hvjs$1pqi$1@otis.netspace.net.au> <382C8CEE.39347C17@chimu.com> <80iicf$od1$1@nnrp1.deja.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fbe9c,da48a4987570047e X-Google-Attributes: gidfbe9c,public X-Google-Thread: f5d71,da48a4987570047e X-Google-Attributes: gidf5d71,public X-Google-Thread: 10d6db,da48a4987570047e X-Google-Attributes: gid10d6db,public X-Google-Thread: 114809,da48a4987570047e X-Google-Attributes: gid114809,public From: Mark Fussell <mark.fussell@chimu.com> Subject: Re: Refactoring book + one to many implementation Date: 1999/11/12 Message-ID: <382D1653.EF337931@chimu.com> X-Deja-AN: 548053855 Content-Transfer-Encoding: 7bit References: <80hvjs$1pqi$1@otis.netspace.net.au> <382C8CEE.39347C17@chimu.com> <80iicf$od1$1@nnrp1.deja.com> To: abulka@my-deja.com X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Trace: nntp1.ba.best.com 942479079 234 206.86.217.25 Organization: ChiMu Corporation MIME-Version: 1.0 Newsgroups: comp.lang.java,aus.computers.java,borland.public.jbuilder.java,comp.lang.smalltalk