Article View: comp.lang.java
Article #380Re: Refactoring book + one to many implementation
From: abulka
Date: Sat, 13 Nov 1999 00:00
Date: Sat, 13 Nov 1999 00:00
59 lines
3278 bytes
3278 bytes
Mark Fussell <mark.fussell@chimu.com> wrote:
> 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.
Well - let's see if it really does. The code from the Refactoring
book, p.199 for setCustomer reads:
Class Order...
void setCustomer (Customer arg) ...
if (_customer != null) _customer.friendOrders().remove(this);
_customer = arg;
if (_customer != null) _customer.friendOrders().add(this);
}
So given we are using the 'elegant' one liner version of removeOrder
> Class Customer...
> void removeOrder( Order order ) {
> order.setCustomer(null);
> }
Notice that when we pass null to order.setCustomer, then setCustomer
skips the add/remove calls! This results in the Customer object's hash
set STILL POINTING to the order object, even though the order object is
no longer pointing to the customer.
The only way to keep our elegant one liner version of removeOrder, and
keep the spirit of what Martin was trying to show viz. have the Order
class controlling the association, I think, is to make setCustomer a
little smarter.
Class Order...
void setCustomer (Customer arg) ...
if (arg = null) and (_customer <> null) // EXTRA LOGIC
_customer.friendOrders().remove(this);
if (_customer != null) _customer.friendOrders().remove(this);
_customer = arg;
if (_customer != null) _customer.friendOrders().add(this);
}
Now admittedly, Martin Fowler says in the text that "The exact code in
the controlling modifier varies with the multiplicity of the
association. If the customer is not allowed to be null, I can forgo
the null checks, but I need to check for a null argument."
In our case, customer is allowed to be null, so we need to keep the
null checks. And as it turns out, we need to "check for a null
argument", though that is only one third the story - the extra logic I
added, above, hopefully fills out the rest of the story.
-Andy Bulka
WindowWare Multimedia
ATUG
Sent via Deja.com http://www.deja.com/
Before you buy.
Technical Details
Message-ID:
<80krmj$7sp$1@nnrp1.deja.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> <382D1653.EF337931@chimu.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit 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 X-Google-Thread: fbe9c,da48a4987570047e X-Google-Attributes: gidfbe9c,public From: abulka@my-deja.com Subject: Re: Refactoring book + one to many implementation Date: 1999/11/13 Message-ID: <80krmj$7sp$1@nnrp1.deja.com> X-Deja-AN: 548249204 References: <80hvjs$1pqi$1@otis.netspace.net.au> <382C8CEE.39347C17@chimu.com> <80iicf$od1$1@nnrp1.deja.com> <382D1653.EF337931@chimu.com> X-Http-Proxy: 1.0 proxy2.netspace.net.au:8080 (Squid/2.2.STABLE3), 1.0 x40.deja.com:80 (Squid/1.1.22) for client 210.15.250.252, 203.23.72.4 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Sat Nov 13 23:19:49 1999 GMT X-MyDeja-Info: XMYDJUIDabulka Newsgroups: comp.lang.java,aus.computers.java,borland.public.jbuilder.java,comp.lang.smalltalk X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)