Article View: comp.lang.java
Article #381Re: Refactoring book + one to many implementation
From: Mark Fussell
Date: Sat, 13 Nov 1999 00:00
Date: Sat, 13 Nov 1999 00:00
75 lines
3964 bytes
3964 bytes
abulka@my-deja.com wrote:
> 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! ...
No it doesn't. Are you actually testing this out? In a transcript of
VisualAge or a little testing main for a command-line? I again suggest
you do, but will walk through the code line by line to help the process.
void setCustomer (Customer arg) {
if (_customer != null) _customer.friendOrders().remove(this);
_customer = arg;
if (_customer != null) _customer.friendOrders().add(this);
}
Coming into the method, '_customer' is not null: it contains the
customer the order is currently linked with. So the first line
(order-removal) will execute. Then the second line will execute and
'_customer' is set to 'null'. Finally the third line does not execute
because '_customer' is now null. So given a 'null' argument to
'setCustomer' the execution behavior is equivalent to:
void setCustomer (Customer arg) {
_customer.friendOrders().remove(this);
_customer = arg;
}
This is exactly what we want and does not require your extra additions.
Note that your last suggestion:
> if (arg = null) and (_customer <> null) // EXTRA LOGIC
> _customer.friendOrders().remove(this);
>
> if (_customer != null) _customer.friendOrders().remove(this);
will cause TWO calls to remove 'this' order from the customer if
'arg==null'. Considering that you keep making syntax errors (there are
two in the above snippet), you must not be trying this out. Why not
simply let the computer show you how the code really works and then
build a deeper understanding from there? Walk a debugger trace or put
in a bunch of print statements. Or you could create some postcondition
check or XP-style unit tests to verify that the postcondition is true:
/**
*@require order.customer = self
*@ensure !self.orders.contains(order) && order.customer = null
*/
void removeOrder( Order order ) {
order.setCustomer(null);
}
Good luck...
--Mark
mark.fussell@chimu.com
Technical Details
Message-ID:
<382E0AD4.79C1F9C5@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> <382D1653.EF337931@chimu.com> <80krmj$7sp$1@nnrp1.deja.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: fbe9c,da48a4987570047e X-Google-Attributes: gidfbe9c,public X-Google-Thread: 114809,da48a4987570047e X-Google-Attributes: gid114809,public X-Google-Thread: 10d6db,da48a4987570047e X-Google-Attributes: gid10d6db,public From: Mark Fussell <mark.fussell@chimu.com> Subject: Re: Refactoring book + one to many implementation Date: 1999/11/13 Message-ID: <382E0AD4.79C1F9C5@chimu.com> X-Deja-AN: 548304132 Content-Transfer-Encoding: 7bit References: <80hvjs$1pqi$1@otis.netspace.net.au> <382C8CEE.39347C17@chimu.com> <80iicf$od1$1@nnrp1.deja.com> <382D1653.EF337931@chimu.com> <80krmj$7sp$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 942541661 227 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