D
David Bolen
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
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