Extend one object from multiple classes?
Hello, I'm a .NET developer who's been getting experience in Java for some time now, and I think I'd like to excercise it outside the classroom finally.
If you find it relevant, I'm using PircBot, an IRC client (http://www.jibble.org/pircbot.php).
The examples make it easy to see how it works, and it is well documented. I plan to do my own research for dynamically loading classes to represent plugins for it, but I would like to do something like this, to avoid a lot of code re-writing:
1. Load multiple classes dynamically (not really part of my qustion)
2. Extend the same PircBot abstract class instance across all of these classes, hopefully also allowing called methods that PircBot uses for events to all be called.
Is my only solution to create a "proxy" class to extend pircbot, then manually handle all pircbot events and loop through my plugins to raise them?
Thank you!
Re: Extend one object from multiple classes?
Quote:
Originally Posted by MalcolmCarmen
Hello, I'm a .NET developer who's been getting experience in Java for some time now, and I think I'd like to excercise it outside the classroom finally.
If you find it relevant, I'm using PircBot, an IRC client (
http://www.jibble.org/pircbot.php).
The examples make it easy to see how it works, and it is well documented. I plan to do my own research for dynamically loading classes to represent plugins for it, but I would like to do something like this, to avoid a lot of code re-writing:
1. Load multiple classes dynamically (not really part of my qustion)
2. Extend the same PircBot abstract class instance across all of these classes, hopefully also allowing called methods that PircBot uses for events to all be called.
Is my only solution to create a "proxy" class to extend pircbot, then manually handle all pircbot events and loop through my plugins to raise them?
Thank you!
You can use an Interface to do this. Not like a user interface, but an Interface Class. Look them up.
Re: Extend one object from multiple classes?
I don't get the problem. Can you show some theoretical code of what you want to do?
Re: Extend one object from multiple classes?
I'll provide some psuedo-code for a method that might load these plugin .class files:
method loadPlugins {
while(thereArePluginsToLoad)
NewPlugin.DynamicallyExtendAPluginClass(PircBotClass)
}
Now, I've supposedly extended PircBotClass to MULTIPLE classes. If PircBotClass raised an event like SayThis() and two plugins were loaded, the two plugin classes would have their SayThis() method invoked, seeing as they extend from a single instantiated object.
The utility of this is obviously to make it easy for multiple plugin classes to communicate with a single instance of the PircBot class I mentioned earlier. As PircBot events fire (such as a user joining a "chat room"), they will be fired across multiple classes rather than one.
PircBot is *supposed* to be used like this:
myclass extends pircbot {
..
}
I need to extend a SINGLE instance of pircbot across *multiple* classes - something that I hope java has the dyanmic ability to do.
Thank you so much!
Re: Extend one object from multiple classes?
That's even conceptually impossible, not just practically. Inheritance works on the class level, not at the instance level.
Extend PircBot *once* with a special dispatcher class that maintains a list of your plugins and forwards the calls to each of them.
Re: Extend one object from multiple classes?
Quote:
Originally Posted by CornedBee
That's even conceptually impossible, not just practically. Inheritance works on the class level, not at the instance level.
Extend PircBot *once* with a special dispatcher class that maintains a list of your plugins and forwards the calls to each of them.
Is it possible at all to "catch" the methods called and forward them to X class, or will I have to handle the event methods and then manually forward them? I could do this, but it won't be a fun chore since Pircbot has a good number of events.
Re: Extend one object from multiple classes?
Using java.lang.reflection.Proxy, it might be possible to catch everything. The code will be ugly, but short.
Re: Extend one object from multiple classes?
Quote:
Originally Posted by CornedBee
Using java.lang.reflection.Proxy, it might be possible to catch everything. The code will be ugly, but short.
Thanks a lot, I'll look into using this. You've probably saved me a great deal of time :>
edit: for those like me:
http://java.sun.com/j2se/1.4.2/docs/...ect/Proxy.html