Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java equivalent of MI for augmenting base of a hierarchy

Reply
Thread Tools

Java equivalent of MI for augmenting base of a hierarchy

 
 
David Bolen
Guest
Posts: n/a
 
      08-21-2003
I was wondering what might be a typical approach in Java to handle a
case where I might have used multiple inheritance in languages that
supported it; specifically that of wanting to augment a base class and
its child classes without having to replicate code in the child
classes.

In the simplest case, let's say that I have a simple hierarchy like:

Player
|
Dealer

Now I come along and I wish to have an augmented Player class, say one
that is cognizant of a GUI subsystem. So what I'd like to end up with
is, conceptually, a new hierarchy of:

Player
|
+----+----+
| |
Dealer GUIPlayer
| |
+----+----+
|
GUIDealer

In an MI language, I would just inherit from both GUIPlayer and Dealer
and I'd have my GUIDealer - likely without writing any actual body
code in GUIDealer.

In Java, its simple enough for GUIPlayer to extend Player, but
obviously I can't MI from Dealer. And since Dealer is a concrete
class I can't use it as an interface. Even if I were to extract
Dealer into an interface (which would get me the equivalence I might
want with respect to method parameters) I'd still have to implement
the interface twice, in Dealer and GUIDealer, even though the
implementations would be identical.

At the moment I'm looking at doing delegation with an internal Dealer
object inside of GUIDealer. It still requires that I ensure I keep
GUIDealer manually in sync with the Dealer interface (so extracting
Dealer to an interface would make that easier to ensure in an
automated way), but I have to keep writing the code to delegate each
method.

Is there a more Java-centric way to handle this sort of problem?

Thanks.

-- David

 
Reply With Quote
 
 
 
 
James Wilson
Guest
Posts: n/a
 
      08-25-2003
David Bolen wrote:

> I was wondering what might be a typical approach in Java to handle a
> case where I might have used multiple inheritance in languages that
> supported it; specifically that of wanting to augment a base class and
> its child classes without having to replicate code in the child
> classes.
>
> In the simplest case, let's say that I have a simple hierarchy like:
>
> Player
> |
> Dealer
>
> Now I come along and I wish to have an augmented Player class, say one
> that is cognizant of a GUI subsystem. So what I'd like to end up with
> is, conceptually, a new hierarchy of:
>
> Player
> |
> +----+----+
> | |
> Dealer GUIPlayer
> | |
> +----+----+
> |
> GUIDealer
>
> In an MI language, I would just inherit from both GUIPlayer and Dealer
> and I'd have my GUIDealer - likely without writing any actual body
> code in GUIDealer.
>
> In Java, its simple enough for GUIPlayer to extend Player, but
> obviously I can't MI from Dealer. And since Dealer is a concrete
> class I can't use it as an interface. Even if I were to extract
> Dealer into an interface (which would get me the equivalence I might
> want with respect to method parameters) I'd still have to implement
> the interface twice, in Dealer and GUIDealer, even though the
> implementations would be identical.
>
> At the moment I'm looking at doing delegation with an internal Dealer
> object inside of GUIDealer. It still requires that I ensure I keep
> GUIDealer manually in sync with the Dealer interface (so extracting
> Dealer to an interface would make that easier to ensure in an
> automated way), but I have to keep writing the code to delegate each
> method.


Read "Effective Java Progamming Language Guide" by Joshua Block. Item's
14, 16, 17, 34 speek specifically to your questions.

>
> Is there a more Java-centric way to handle this sort of problem?
>
> Thanks.
>
> -- David
>

I think you're on the right track though. Composition is a good way.

public interface Player
methods of the interface

public interface Dealer extends Player
extra methods of the interface

public interface GUIPlayer extends Player
extra methods of the interface

public interface GUIDealer extends Dealer, GUIPlayer
extra methods of the interface


And then create concrete implementations of each of these
interfaces.

Hope this helps,

James

 
Reply With Quote
 
 
 
 
David Bolen
Guest
Posts: n/a
 
      08-26-2003
James Wilson <> writes:

> I think you're on the right track though. Composition is a good way.
>
> public interface Player
> methods of the interface
>
> public interface Dealer extends Player
> extra methods of the interface
>
> public interface GUIPlayer extends Player
> extra methods of the interface
>
> public interface GUIDealer extends Dealer, GUIPlayer
> extra methods of the interface
>
>
> And then create concrete implementations of each of these
> interfaces.


Yeah, that's what I figured - unfortunately in this example I
duplicate even more effort (implementations) than just single
inheriting from GUIPlayer in that I have to duplicate both the
GUIPlayer and Dealer logic in GUIDealer. If I inherit from GUIPlayer
than I just have to reimplement the original Dealer/Player logic
differences.

The rub is that the only practical between GUIDealer and Dealer is the
difference created between GUIPlayer and Player - so other than
wanting to inherit that difference, any other code I write as part of
GUIDealer is duplication that I have to maintain.

Thanks for the info (and book reference) though.

-- David
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      08-26-2003
David Bolen wrote:
> James Wilson <> writes:
>
>
>>I think you're on the right track though. Composition is a good way.
>>
>>public interface Player
>> methods of the interface
>>
>>public interface Dealer extends Player
>> extra methods of the interface
>>
>>public interface GUIPlayer extends Player
>> extra methods of the interface
>>
>>public interface GUIDealer extends Dealer, GUIPlayer
>> extra methods of the interface
>>
>>
>>And then create concrete implementations of each of these
>>interfaces.

>
>
> Yeah, that's what I figured - unfortunately in this example I
> duplicate even more effort (implementations) than just single
> inheriting from GUIPlayer in that I have to duplicate both the
> GUIPlayer and Dealer logic in GUIDealer. If I inherit from GUIPlayer
> than I just have to reimplement the original Dealer/Player logic
> differences.
>
> The rub is that the only practical between GUIDealer and Dealer is the
> difference created between GUIPlayer and Player - so other than
> wanting to inherit that difference, any other code I write as part of
> GUIDealer is duplication that I have to maintain.


I came late to this discussion, so likely I'm missing something, but it
sure seems like Dealer is a role that a Player can have more than it is
a specific type of Player. The design is more loosely coupled if Dealer
does not extend Player but instead is associated with a Player. Among
the advantages this would provide is that there would be no need for
different kinds of Dealers in parallel with different kinds of Players
(i.e. you could do away with GUIDealer altogether).

Also, interfaces are very useful in many situations, but not so useful
if there is no room for alternative implementations. In this particular
case, if Player is a class then it can expose a package-private API for
a seperate Dealer object to use, but if it is an interface then all its
methods are implictly public.


John Bollinger


 
Reply With Quote
 
David Bolen
Guest
Posts: n/a
 
      08-27-2003
"John C. Bollinger" <> writes:

> I came late to this discussion, so likely I'm missing something, but
> it sure seems like Dealer is a role that a Player can have more than
> it is a specific type of Player. The design is more loosely coupled
> if Dealer does not extend Player but instead is associated with a
> Player. Among the advantages this would provide is that there would
> be no need for different kinds of Dealers in parallel with different
> kinds of Players (i.e. you could do away with GUIDealer altogether).


As with any design, I'd have to agree that there are multiple ways to
build the original class hierarchy, but I did find it more natural to
consider Dealer to have a fairly classic "is a" relationship with
Player. A Dealer is just another form of a Player which overrides
some characteristics (such as presentation of hand). I suppose I
could have encapsulated presentation of hand into a separate role sort
of object, but then I end up with two tightly coupled object
effectively needing to share state (the hand) which otherwise just
belongs to the Player. I guess I could argue either way though. The
net is still though that I only burped in the Java implementation due
to lack of MI (of implementation).

I do have additional Rules and Behavior classes that function in more
of a role design, where any player object can be associated with
(composed of) rules that it obeys and with behavior that it exhibits
in reacting to events/queries. But Dealer felt more natural as a
subclass in the original (pre-GUI) implementation.

> Also, interfaces are very useful in many situations, but not so useful
> if there is no room for alternative implementations. In this
> particular case, if Player is a class then it can expose a
> package-private API for a seperate Dealer object to use, but if it is
> an interface then all its methods are implictly public.


All its methods, or just those methods that are needed to meet the
interface? Presumably Player could implement the interface, but still
offer other package-private API methods for use by other objects, right?

-- David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Augmenting Types Ryan Chan Javascript 113 01-09-2010 10:38 AM
Augmenting ECMA-262 specs: crazy idea? optimistx Javascript 8 11-09-2009 08:52 PM
Augmenting functions disappearedng Javascript 6 11-14-2008 08:47 PM
java.lang Exception hierarchy - why? Dan Wilkin Java 11 03-05-2004 02:18 AM
Changing base class of a big hierarchy John J. Lee Python 7 08-05-2003 04:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57