Re: Interfaces and aggregation
From: Adam (NOTFORSPAM.a_szczeblewski_at_poczta.onet.pl)
Date: 01/30/04
- Next message: kaman: "Struts forwarding does not change URL in browser"
- Previous message: Rich Hajinlian: "Re: Tutorial on JSP/EJB implementation"
- In reply to: BarryNL: "Re: Interfaces and aggregation"
- Next in thread: Markos Charatzas: "Re: Interfaces and aggregation"
- Reply: Markos Charatzas: "Re: Interfaces and aggregation"
- Reply: Red Orchid: "Re: Interfaces and aggregation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Jan 2004 15:54:53 +0100
> Considering it doesn't make any sense for a Cabin to exist without a
> Ship, I'd pass the Ship to the constructor of Cabin, thus:
>
> class Cabin implements Query {
> private Ship myShip;
>
> public Cabin(Ship ship) {
> myShip = ship;
> }
>
> public String getQuery() {
> return myShip.getQuery();
> }
> }
Another possibility is to make Cabin a public nested
(inner) class of ship. Then it automaticaly has a
reference to its owning ship. Even creating
cabins is safer then - you have to have a ship
to create a cabin:
Ship ship = new Ship();
Ship.Cabin cabin = ship.new Cabin();
There is no way you can create a cabin not
belonging to any ship.
As for Barry's suggestion (above), it still would be
possible to do new Cabin(null). Even
if a NullPointerException was thrown from the Cabin's ctor
you would not know it until runtime.
With nested class it is the compiler's (not programmer's) responsibility
to assure that each cabin has an owning ship.
Disadvantage of this approach is having one big source file
for two classes. That's why probably many programmers
avoid this way of implementing aggregation (yes, me too)
Adam
- Next message: kaman: "Struts forwarding does not change URL in browser"
- Previous message: Rich Hajinlian: "Re: Tutorial on JSP/EJB implementation"
- In reply to: BarryNL: "Re: Interfaces and aggregation"
- Next in thread: Markos Charatzas: "Re: Interfaces and aggregation"
- Reply: Markos Charatzas: "Re: Interfaces and aggregation"
- Reply: Red Orchid: "Re: Interfaces and aggregation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|