Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9297757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:09:07+00:00 2026-06-18T22:09:07+00:00

Suppose I want to make a custom button from scratch: BasicButton : NSObject So

  • 0

Suppose I want to make a custom button from scratch:

  • BasicButton : NSObject
    So I created a class which knows how to handle mouse events and forward messages to interested receivers when the button is clicked (via delegation, target/action, notifications, whatever).
  • SolidColorButton : BasicButton : NSObject
    My button now works but it’s invisible, so I decided to draw a solid background to bring the button to life (not very good in terms of UI design but let’s pretend).
  • LabelledButton : SolidColorButton : BasicButton : NSObject
    Now let’s say I want to add a label on the button, so I’ve created a another subclass that can draw text on the button.

So now I have a button which can handle mouse events, draws a solid background, and draws some text. But what happens now if I decide I need a button with an image for a background instead of the solid color, but that still knows how to draw a label? This is where the single-inheritance subclassing approach fails. I can’t combine the mouse handling, label drawing and image drawing at will, I can only choose a level of refinement down the inheritance chain.

So I guess my question is: What’s the way to implement these separate modules so that they can then be mixed and matched to create some sort of offspring object which has the combined features of the desired modules and nothing else?

Example, I’d want to be able to write modules like:

  • Mouse handling
  • Keyboard handling
  • Solid background
  • Image background
  • Label
  • etc.

and then have the ability to to make a specific button which: a) handles mouse, b) draws a background and c) has a label; and nothing else.

The question is specific to how one would do this in Objective-C or another single-inheritance OOP language. In Ruby a way to do this would be using mixins.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:09:09+00:00Added an answer on June 18, 2026 at 10:09 pm

    You already noticed that inheritance is a bad idea here. The Decorator Pattern would come in handy: you will have one button class and several decorators that “decorate” the button (wrap it and add functionality) and can be combined arbitrarily.

    Class diagram:

     +---------------+     innerButton
     |ButtonInterface+------------------------+
     +---------------+ 1                      |
            ^    ^                            |
            |    |                            |
            |    +-------------+              |
     +------+------+   +-------+-------+      |
     | BasicButton |   |ButtonDecorator+------+
     +-------------+   +---------------+
                         ^           ^
                         |           |  ...
                         |           |
    +--------------------+----+ +----+------------------+
    |SolidColorButtonDecorator| |LabelledButtonDecorator|  ...
    +-------------------------+ +-----------------------+
    

    Object diagram (for your example):

    +----------------------------+
    |MouseHandlingButtonDecorator|
    +-----+----------------------+
          |
         1| innerButton
    +-----+-------------------+
    |BackgroundButtonDecorator|
    +-----+-------------------+
          |
         1| innerButton
    +-----+-----------------+
    |LabelledButtonDecorator|
    +-----+-----------------+
          |
         1| innerButton
    +-----+-----+
    |BasicButton|
    +-----------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to make a Web application which uses a fixed width and
Suppose you want to make a bash script which supports no options but acts
I have a class, suppose it's called EntityModel , and I want to make
Suppose I have a class that I want to make sure my compiler (GCC
Suppose I want to make an Android device with some custom OS modifications. How
Suppose we want two constructors for a class representing complex numbers: Complex (double re,
Suppose I create a custom object/javascript class (airquotes) as follows: // Constructor function CustomObject(stringParam)
suppose I want to make a Flash browser game that can be played in
see i am working on linux fedora Now suppose if i want to make
Suppose I want to make an ajax call to the server and use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.