class design question

Discussion in 'Java' started by vk02720, Jul 14, 2005.

  1. vk02720

    vk02720 Guest

    Hi,
    I have different type of messages to be handled in my app. I wrote a
    base class like
    BaseHandler {
    public boolean perform() {
    }
    }
    I get a message with a String messageType.
    Just to avoid making changes to the same file ( and intending to keep a
    good design ), I subclass each specific message handler from
    BaseHandler and override perform() for each message.
    The issue is how to organize the classes properly.
    For example, I have following function to return correct handler :
    public BaseHandler getHandler(messageType) {
    //return the correct Handler based on messageType
    }
    Which class should such function belong ?
    Is this a normal way to solve such problem. It appears quite common!
    Not sure if this conforms to any Design Pattern....
    The code is in Java.

    Please advise.

    TIA
     
    vk02720, Jul 14, 2005
    #1
    1. Advertising

  2. vk02720

    Chris Smith Guest

    vk02720 <> wrote:
    > For example, I have following function to return correct handler :
    > public BaseHandler getHandler(messageType) {
    > //return the correct Handler based on messageType
    > }
    > Which class should such function belong ?
    > Is this a normal way to solve such problem. It appears quite common!
    > Not sure if this conforms to any Design Pattern....


    It depends on the extent to which you want to isolate specific
    BaseHandler implementations from the application. You have a range of
    options for trading off isolation of concerns against the intrinsic
    complexity of the technique. They include:

    A. Write the method in BaseHandler, and make BaseHandler aware of its
    subclasses. This is simple, and accomplishes the goal that 95% of the
    time you don't get people stepping on each other's toes.

    B. Write a MessageDispatcher that knows about all the subclasses. This
    eliminates a circular dependency, and makes the abstract BaseHandler
    more reusable, should you happen to care about that.

    C. Write a MessageDispatcher and have it read a handler mapping file via
    Class.getResource (could be as simple as a Properties file) and dispatch
    as appropriate. This removes registration of handler instances from
    code.

    D. Write a MessageDispatcher and have it read a set of handler mapping
    files via ClassLoader.getResources and dispatch from there. This
    prevents anyone from ever having to edit the same file for different
    handlers in the case where they are packaged and distributed separately,
    such as being provided by different vendors.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 14, 2005
    #2
    1. Advertising

  3. vk02720

    Hemal Pandya Guest

    vk02720 wrote:
    [....]
    > public BaseHandler getHandler(messageType) {
    > //return the correct Handler based on messageType
    > }
    > Which class should such function belong ?


    I have seen and implemented a lot of examples of this during my C++
    days. A template implementation even allowed a set of classes to
    register themselves as handlers of objects of a particular type.

    The basic idea is as follows: All Handler classes are cloneable and
    contain a static member of their own type. This static member is
    instantiated with a special constructor which registers itself as the
    handler for a particular message type. You still have to ensure somehow
    that all these classes do get loaded.

    In java world, all this is perhaps much better handled by a specifying
    handler classes for various message types in a property file.
     
    Hemal Pandya, Jul 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dave
    Replies:
    5
    Views:
    602
    William Brogden
    Jul 17, 2004
  2. E11
    Replies:
    1
    Views:
    4,842
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  4. John_Woo

    class design vs. db design

    John_Woo, Dec 19, 2006, in forum: Java
    Replies:
    2
    Views:
    339
    John_Woo
    Dec 19, 2006
  5. Bartholomew Simpson

    class design/ design pattern question

    Bartholomew Simpson, Jun 12, 2007, in forum: C++
    Replies:
    2
    Views:
    456
    Daniel T.
    Jun 12, 2007
Loading...

Share This Page