The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Runtime Class Information



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Dynamic interface composition?

Posted by Chris Macksey on October 18, 2000 at 4:38 PM

This question seems to overlap with many of the forum areas, but I felt this one would be the most appropriate.

My question is this: is there a way in Java to do dynamic interface composition? Following the Hippopotamus example, suppose I declared a class Hippopotamus (a regular old hippo, not a dancing one.. yet). Further, suppose I declared an interface Dancing like this:

public interface Dancing {
public String DanceKind();

The DanceKind() method would, theoretically, supply the type of dance, such as "Disco", etc.

Now, is there a way to attach this interface *at run time* to an existing Hippopotamus object (say, using a helper object that implements it), or even a method in a subclass of Hippopotamus, so that "oHippo instanceof Dancing" only returns a value when the Hippopotamus is actually dancing (and then, of course, if it is, we can use the DanceKind() method to return the type of dance)? And likewise, when the poor hippo gets tired, is there any way to "revoke" the Dancing interface so that "oHippo instanceof Dancing" returns nil?

The reason why this sort of thing might be useful is if you had a large collection of different kinds of animals that theoretically *could* dance, but very few of them actually will - thus you'd prefer not to have to create Dancing... versions of the animals just on the off chance that a few might decide to dance, but rather just attach an extra dancing object to the individuals that do. :)

Chris Macksey
Senior Developer
HealthLink Technologies, Inc.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us