Design question for UI library

Discussion in 'C++' started by xargon, Dec 9, 2005.

  1. xargon

    xargon Guest

    Hi everyone,

    I am writing a small user interface library in C++ (I know it has been
    done to death, but it is good for learning ).

    Anyways, I need some help/advice with the deisgn of the software
    architecture. The problem I am facing is that a control can have so
    many states... For example, a button could be have various shapes
    (rounded, square, some other custom geometry). Also, it could be
    bitmapped, textured, text only, text with bitmap etc. So, you get the
    picture. Having a specialized Button class for each of these would be
    very difficult to maintain.

    So, my question to you gurus is how would you organize a widget library
    like that. What design patterns would you recommend that I look into?
    Any advice would be appreciated.

    Thanks and cheers!

    xargy
    xargon, Dec 9, 2005
    #1
    1. Advertising

  2. xargon

    W Marsh Guest

    On 9 Dec 2005 13:20:04 -0800, "xargon" <> wrote:

    >Hi everyone,
    >
    >I am writing a small user interface library in C++ (I know it has been
    >done to death, but it is good for learning ).
    >
    >Anyways, I need some help/advice with the deisgn of the software
    >architecture. The problem I am facing is that a control can have so
    >many states... For example, a button could be have various shapes
    >(rounded, square, some other custom geometry). Also, it could be
    >bitmapped, textured, text only, text with bitmap etc. So, you get the
    >picture. Having a specialized Button class for each of these would be
    >very difficult to maintain.
    >
    >So, my question to you gurus is how would you organize a widget library
    >like that. What design patterns would you recommend that I look into?
    >Any advice would be appreciated.
    >
    >Thanks and cheers!
    >
    >xargy


    Don't make separate classes buttons with different properties. Use
    different classes for different /things/. If you want to be able to
    choose between a rounded button and a square button for example,
    expose this choice as a property in the class interface.
    W Marsh, Dec 9, 2005
    #2
    1. Advertising

  3. xargon

    mlimber Guest

    xargon wrote:
    > Hi everyone,
    >
    > I am writing a small user interface library in C++ (I know it has been
    > done to death, but it is good for learning ).
    >
    > Anyways, I need some help/advice with the deisgn of the software
    > architecture. The problem I am facing is that a control can have so
    > many states... For example, a button could be have various shapes
    > (rounded, square, some other custom geometry). Also, it could be
    > bitmapped, textured, text only, text with bitmap etc. So, you get the
    > picture. Having a specialized Button class for each of these would be
    > very difficult to maintain.
    >
    > So, my question to you gurus is how would you organize a widget library
    > like that. What design patterns would you recommend that I look into?
    > Any advice would be appreciated.
    >
    > Thanks and cheers!
    >
    > xargy


    This post is off-topic here, as it does not concern the C++ *language*
    proper (see the FAQ:
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9). Try in
    comp.software.patterns or comp.object.

    Cheers! --M
    mlimber, Dec 9, 2005
    #3
  4. xargon

    mlimber Guest

    W Marsh wrote:
    > On 9 Dec 2005 13:20:04 -0800, "xargon" <> wrote:
    >
    > >Hi everyone,
    > >
    > >I am writing a small user interface library in C++ (I know it has been
    > >done to death, but it is good for learning ).
    > >
    > >Anyways, I need some help/advice with the deisgn of the software
    > >architecture. The problem I am facing is that a control can have so
    > >many states... For example, a button could be have various shapes
    > >(rounded, square, some other custom geometry). Also, it could be
    > >bitmapped, textured, text only, text with bitmap etc. So, you get the
    > >picture. Having a specialized Button class for each of these would be
    > >very difficult to maintain.
    > >
    > >So, my question to you gurus is how would you organize a widget library
    > >like that. What design patterns would you recommend that I look into?
    > >Any advice would be appreciated.
    > >
    > >Thanks and cheers!
    > >
    > >xargy

    >
    > Don't make separate classes buttons with different properties. Use
    > different classes for different /things/. If you want to be able to
    > choose between a rounded button and a square button for example,
    > expose this choice as a property in the class interface.


    OT: That's just one way to do it, but it's really not the design
    patterns or OO way.

    Cheers! --M
    mlimber, Dec 9, 2005
    #4
  5. xargon

    W Marsh Guest

    On 9 Dec 2005 13:50:41 -0800, "mlimber" <> wrote:

    >
    >W Marsh wrote:
    >> On 9 Dec 2005 13:20:04 -0800, "xargon" <> wrote:
    >>
    >> >Hi everyone,
    >> >
    >> >I am writing a small user interface library in C++ (I know it has been
    >> >done to death, but it is good for learning ).
    >> >
    >> >Anyways, I need some help/advice with the deisgn of the software
    >> >architecture. The problem I am facing is that a control can have so
    >> >many states... For example, a button could be have various shapes
    >> >(rounded, square, some other custom geometry). Also, it could be
    >> >bitmapped, textured, text only, text with bitmap etc. So, you get the
    >> >picture. Having a specialized Button class for each of these would be
    >> >very difficult to maintain.
    >> >
    >> >So, my question to you gurus is how would you organize a widget library
    >> >like that. What design patterns would you recommend that I look into?
    >> >Any advice would be appreciated.
    >> >
    >> >Thanks and cheers!
    >> >
    >> >xargy

    >>
    >> Don't make separate classes buttons with different properties. Use
    >> different classes for different /things/. If you want to be able to
    >> choose between a rounded button and a square button for example,
    >> expose this choice as a property in the class interface.

    >
    >OT: That's just one way to do it, but it's really not the design
    >patterns or OO way.
    >
    >Cheers! --M


    Hm? Why not? Would you create a new class because you wanted a red
    button also?
    W Marsh, Dec 9, 2005
    #5
  6. W Marsh wrote:
    > Hm? Why not? Would you create a new class because you wanted a red
    > button also?


    Depends. If you had a different button shape, then the button function
    CalculateArea might use a different function. All depends on what
    functions you have on the button class and what you need to change.
    Josh Mcfarlane, Dec 9, 2005
    #6
  7. xargon

    W Marsh Guest

    On 9 Dec 2005 14:32:32 -0800, "Josh Mcfarlane" <>
    wrote:

    >W Marsh wrote:
    >> Hm? Why not? Would you create a new class because you wanted a red
    >> button also?

    >
    >Depends. If you had a different button shape, then the button function
    >CalculateArea might use a different function. All depends on what
    >functions you have on the button class and what you need to change.


    Ah, right, yes. The way I phrased it made it sound like it was THE
    solution for the problem, which is obviously foolishness. I meant that
    it sounds like the best way to do it in the circumstance.
    W Marsh, Dec 9, 2005
    #7
  8. W Marsh wrote:
    > On 9 Dec 2005 14:32:32 -0800, "Josh Mcfarlane" <>
    > wrote:
    > Ah, right, yes. The way I phrased it made it sound like it was THE
    > solution for the problem, which is obviously foolishness. I meant that
    > it sounds like the best way to do it in the circumstance.


    Well, in his specific example, how would you differentiate drawing the
    circle button from the bitmap button?
    Josh Mcfarlane, Dec 9, 2005
    #8
  9. xargon

    xargon Guest

    Well, I feel guilty about posting this in the wrong forum... but both
    these solutions are kinda complicated. One would have a lot of
    functions in same class... other might result in an unmanagable number
    of classes..
    xargon, Dec 9, 2005
    #9
  10. xargon

    W Marsh Guest

    On 9 Dec 2005 14:56:59 -0800, "Josh Mcfarlane" <>
    wrote:

    >
    >W Marsh wrote:
    >> On 9 Dec 2005 14:32:32 -0800, "Josh Mcfarlane" <>
    >> wrote:
    >> Ah, right, yes. The way I phrased it made it sound like it was THE
    >> solution for the problem, which is obviously foolishness. I meant that
    >> it sounds like the best way to do it in the circumstance.

    >
    >Well, in his specific example, how would you differentiate drawing the
    >circle button from the bitmap button?


    I didn't take what he said to mean a circle button - I thought he
    simply meant rounded edges, a minor aesthetic difference.
    W Marsh, Dec 9, 2005
    #10
  11. xargon

    W Marsh Guest

    On 9 Dec 2005 14:56:59 -0800, "Josh Mcfarlane" <>
    wrote:

    >
    >W Marsh wrote:
    >> On 9 Dec 2005 14:32:32 -0800, "Josh Mcfarlane" <>
    >> wrote:
    >> Ah, right, yes. The way I phrased it made it sound like it was THE
    >> solution for the problem, which is obviously foolishness. I meant that
    >> it sounds like the best way to do it in the circumstance.

    >
    >Well, in his specific example, how would you differentiate drawing the
    >circle button from the bitmap button?


    The one GUI library I saw generalized control shapes with a separate
    class, and used composition to assign the appropriate shape to the
    control.

    It seemed to work well enough, but it didn't feel like the ideal
    solution at all.
    W Marsh, Dec 10, 2005
    #11
  12. xargon

    benben Guest

    > This post is off-topic here, as it does not concern the C++ *language*

    This of course IS the concern of the C++ **Language**. The **Language**
    IS designed to deal with these problems where you need **Language**
    level support in addition to library level support.

    Ben
    benben, Dec 10, 2005
    #12
  13. Josh Mcfarlane wrote:
    > W Marsh wrote:
    > > Hm? Why not? Would you create a new class because you wanted a red
    > > button also?

    >
    > Depends. If you had a different button shape, then the button function
    > CalculateArea might use a different function. All depends on what
    > functions you have on the button class and what you need to change.


    Right. See WTL (http://sourceforge.net/projects/wtl) which provides
    separated classes for button and bitmap button. I think such variations
    should be expressed as differennt classes.

    First, I thought a kind of traits or policies could be fine but I'm not
    an expert of traits so I can not say much :)

    Cheers
    --
    Mateusz Loskot
    http://mateusz.loskot.net
    Mateusz Loskot, Dec 10, 2005
    #13
  14. * xargon:
    > Hi everyone,
    >
    > I am writing a small user interface library in C++ (I know it has been
    > done to death, but it is good for learning ).
    >
    > Anyways, I need some help/advice with the deisgn of the software
    > architecture.


    Make sure that C++ object creation is identical to control/widget/window
    creation.

    No "init" or "create" functions available to client code.

    I.e., let constructors construct, and destructors destruct (not doing
    this is the most common & fundamental architectural flaw in GUI libs).


    > The problem I am facing is that a control can have so
    > many states... For example, a button could be have various shapes
    > (rounded, square, some other custom geometry). Also, it could be
    > bitmapped, textured, text only, text with bitmap etc. So, you get the
    > picture. Having a specialized Button class for each of these would be
    > very difficult to maintain.


    Some of the problem here is that you're looking at concrete effects
    rather than general means to implement such effects. E.g., you complain
    that a button can contain anything, and supporting every different thing
    it can contain is impractical. Yes, it is, and therefore you should
    support the general functionality of containment, and perhaps some very
    common cases, not each and every thing client code might want to use.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 10, 2005
    #14
  15. xargon

    W Marsh Guest

    On 9 Dec 2005 17:19:23 -0800, "Mateusz Loskot" <>
    wrote:

    >Josh Mcfarlane wrote:
    >> W Marsh wrote:
    >> > Hm? Why not? Would you create a new class because you wanted a red
    >> > button also?

    >>
    >> Depends. If you had a different button shape, then the button function
    >> CalculateArea might use a different function. All depends on what
    >> functions you have on the button class and what you need to change.

    >
    >Right. See WTL (http://sourceforge.net/projects/wtl) which provides
    >separated classes for button and bitmap button. I think such variations
    >should be expressed as differennt classes.


    I agree there. But rounded edges is, in my opinion, a property of a
    type of button, not a new type of button itself.

    An actual round button, or a bitmap button, should probably be a new
    type.

    Some code libraries, such as the .NET platform don't seem to agree. A
    button is made into a bitmap button by setting a property of the
    object - this sort of thing occurs throughout the different controls.
    I wonder what the code looks like beyond the interface.
    W Marsh, Dec 10, 2005
    #15
  16. xargon

    benben Guest

    xargon wrote:
    > Hi everyone,
    >
    > I am writing a small user interface library in C++ (I know it has been
    > done to death, but it is good for learning ).
    >
    > Anyways, I need some help/advice with the deisgn of the software
    > architecture. The problem I am facing is that a control can have so
    > many states... For example, a button could be have various shapes
    > (rounded, square, some other custom geometry). Also, it could be
    > bitmapped, textured, text only, text with bitmap etc. So, you get the
    > picture. Having a specialized Button class for each of these would be
    > very difficult to maintain.
    >
    > So, my question to you gurus is how would you organize a widget library
    > like that. What design patterns would you recommend that I look into?
    > Any advice would be appreciated.
    >
    > Thanks and cheers!
    >
    > xargy
    >


    Don't lump everything into a single class. You do want a number of
    simpler classes than the a single monolith. And a class must be able to
    do no more than what it should do.

    Breaking down to the very bottom line, a button serves three needs:

    1. to be contained in some UI framework;
    2. to trap user action especially the clicking;
    3. to let some external routine to handle the user action.

    Anything else is desirable but not at all essential. So my personal
    suggestion is for you to provide a simple class to encapsulate the above
    three and give a way for the user to extend it.

    For example, letting the user provide a drawing routine (by overriding a
    virtual function, by assigning a function pointer, or by a policy class)
    can pretty much do away all those shape and color properties. Whichever
    way you do it is up to you.

    That done, you can build up more interesting buttons upon the existing.
    If you are doing OOP, then provide some derived button classes
    (Round_button, Transparent_button, Scary_button, etc); If you prove the
    basic button as a template, provide default policy class and additional
    policy classes, for example:

    template <typename DrawingPolicy = Default_drawing_policy>
    class basic_button;

    class Defaut_drawing_policy;
    class Round_button_drawer;
    class Scary_button_drawer;
    class Transparent_button_drawer;

    Ben
    benben, Dec 10, 2005
    #16
    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. ZackS
    Replies:
    5
    Views:
    6,779
    Just an Illusion
    Jul 9, 2004
  2. dave
    Replies:
    5
    Views:
    578
    William Brogden
    Jul 17, 2004
  3. Dilip
    Replies:
    5
    Views:
    314
    Dilip
    May 24, 2006
  4. Bartholomew Simpson

    class design/ design pattern question

    Bartholomew Simpson, Jun 12, 2007, in forum: C++
    Replies:
    2
    Views:
    432
    Daniel T.
    Jun 12, 2007
  5. Replies:
    1
    Views:
    97
    Richard Cornford
    Sep 24, 2006
Loading...

Share This Page