Results 1 to 8 of 8

Thread: Extend one object from multiple classes?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    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!
    ^_^

  2. #2
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    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!
    ^_^

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    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.
    ^_^

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    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
    Last edited by MalcolmCarmen; Dec 5th, 2005 at 06:57 PM.
    ^_^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width