Need help with OO design problem

Discussion in 'C++' started by Soulshift, Jul 9, 2003.

  1. Soulshift

    Soulshift Guest

    Hi all,

    Here's my problem. I have a design where the class Container inherits
    class Widget, and is used to contain and handle the proper display of
    widgets and behaves like a widget itself. I have a class IsoWidget
    which inherits from class Widget which is used to display isometric
    widgets. I want to make a class IsoContainer (which contains
    IsoWidgets and behaves like an IsoWidget), and since an IsoContainer
    seems like it should inherit from both IsoWidget and Container, I'm
    not sure what's best.

    I see a few options as of now:
    - Use multiple inheritance (ick) to resolve the issue by having
    IsoContainer inherit from Container and IsoWidget.
    - Copy code from IsoWidget and dump it into IsoContainer (ick)
    - Copy code from Container and dump it into IsoContainer (worse)
    - Redesign the entire class hierarchy, breaking nearly everything
    else, but resulting in a better design.

    Does anyone see a better solution? Help!

    Thanks in advance,
    Soulshift, Jul 9, 2003
    1. Advertisements

  2. David White

    David White Guest

    Another idea:

    template<class Item> class Composite : public Item
    void add(Item *);
    // etc.

    With this you can have a composite anything.

    David White, Jul 9, 2003
    1. Advertisements

  3. Agent Mulder

    Agent Mulder Guest

    PS> I'm sort of confused here. So, the class Container inherits from the
    PS> class Widget, and is also used to contain instances of Widget?

    This concept is (was) common in java 1.1.3, the good old AWT.
    There you have a Component that is much like a Widget. Then
    java.awt.Container inherits Component AND has the ability to store
    Components. I guess it's like inheriting from Box to make a Closet.
    You pack your Closet full of Boxes, while your Closet itself IS a Box.

    Agent Mulder, Jul 9, 2003
  4. David White

    David White Guest

    wrote in message news:<>...
    > And just
    > what you are doing inheriting containers from the things they
    > contain I can't imagine.

    It does make sense in some cases, and is a very powerful technique.
    For example, an XyGraph object consists of many smaller objects -
    axes, traces, labels etc. It makes things easier if each of those
    smaller objects is derived from the same base class, say,
    DrawableObject. Since the entire XyGraph object is also a thing that
    is drawn, it makes sense if it is also derived from DrawableObject.
    The XyGraph's Draw member would in turn call the Draw member of each
    of its component objects. Also, the trace objects, for example, might
    be made up of many line objects, each of which is also a
    DrawableObject. Generalized, this technique enables you to build up a
    DrawableObject of any complexity and depth, with both most complex
    objects and the simplest of their components having the same base
    class and interface.


    P.S. I've had to join Google temporarily, because my ISP's newsreader
    is down.
    David White, Jul 10, 2003
  5. j

    j Guest

    > I'm sort of confused here. So, the class Container inherits from the
    > class Widget, and is also used to contain instances of Widget?
    > So you've got a parking lot that is a type of car? Or you can use
    > the instance of the parking lot to drive the car? Really not sure
    > I'm seeing what you are up to here.

    it's a parking lot for parkinglots, you're confusing things.

    j, Jul 10, 2003
  6. Soulshift

    Soulshift Guest

    "David White" <> wrote in message news:<7aOOa.8923$>...

    > I think I would probably have CompositeIsoWidget derived from IsoWidget.
    > Since the standard library probably already has all the container support
    > you need, I don't see a problem with whatever duplication there would be
    > with CompositeWidget.
    > You could also consider protected inheritance, which allows you to inherit
    > the implementation of a general-purpose container class that does most of
    > the work, while the public interface of each composite Widget class only
    > allows the correct kind of Widgets.
    > DW

    Thanks for the suggestions. Probably the only thing that will work is
    the template idea, as the behaviour of a CompositeWidget is not
    trivially duplicatible. Implementing a template solution will take a
    lot of work, but it's good to know that there is a nice way to do it.
    Soulshift, Jul 11, 2003
    1. Advertisements

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. aaaaaa
    Dec 8, 2005
  2. Ed.
    Nov 22, 2003
  3. Julia
    Alvin Bruney [MVP]
    Sep 8, 2004
  4. SpamProof
    Dec 1, 2003
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    May 11, 2004

Share This Page