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 6643401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:02:47+00:00 2026-05-26T00:02:47+00:00

Below is a simplified version of code I’ve written to override a class method

  • 0

Below is a simplified version of code I’ve written to override a class method (using composition), in this case method name i’m overriding is addbuttons(); The class “Screen” adds buttons to a screen whereas class “SubScreen” adds custom buttons to an instance of Screen. Maybe this is not an example of decorator pattern since I’m overriding functionality instead of extending it ? Is there a better way (using a design pattern ?) to achieve same functionality ?

public class Driver {

    public static void main(String args[]){
        AddComponents add1 = new Screen();
        add1.addButtons();

        Screen newScreen = new Screen();
        AddComponents add2 = new SubScreen(newScreen);
        add2.addButtons();
    }

}
    public interface AddComponents {

         public void addButtons();
        }

public class Screen implements AddComponents{

    public void addButtons() {
        //Add Buttons to screen class
    }

}
public class SubScreen implements AddComponents{

    private Screen screen;

    public SubScreen(Screen screen){
        this.screen = screen;
    }

    public void addButtons() {
        //add different custom buttons to Screen class
    }

}
  • 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-05-26T00:02:47+00:00Added an answer on May 26, 2026 at 12:02 am

    Another possibility would be to call it Proxy. Decorator and Proxy are technically very similar – the difference is – in most cases – not a technical one but based on the intention.
    Your example is a little bit minimal and therefore it is hard to guess the intention correctly.

    Edit

    At the detailed level: Screen and SubScreen do not share any code. If you start adding methods to both implementations and the common interface AddComponents you might find

    • that you must duplicate code both in Screen and SubScreen (or delegate to Screen) and
    • that you must add methods to AddComponents which make this interface badly named.

    If both screen classes are similar both on the abstract logical level and at the implementation level, then a an class AbstractScreen with two derived classed would be better. To bring back pattern speak: Use a Factory Method in AbstractScreen to specialize the behaviour regarding the different buttons.

    On your current code there is one strange thing: Why is there a method addButton defined anyway? Simply add the buttons in the appropriate constructor, since the user has to call addButtons in any case and the method does not have arguments.

    Another point not explained is this: SubScreen has a reference to Screen which is not used. Why? Will there be more method in all involved classes Screen, SubScreen and AddComponents? Will each method be a in SubScreen delegate to Screen or only half of them?

    You see – there are many possibilities we do not know and are not shown in the example code but are very important. I’m sure that your head contains a lot of details saying “This proposed stuff will not work because I want to do this and that one way or the other in the near future.” Sadly we cannot get the content of your head into this site without more writing. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below is a simplified version of the pattern my project is using.
I have a method (the code below is a simplified version) that parses small
Ok, here is setup. Note the code below is SIMPLIFIED version. PHP 5.2 ENGINE:
Below is a simplified version of a segment of code that I'm working on
A simplified version of the code is posted below (white space, comments, etc. removed
I have the following C++ code (simplified version): class Shape { bool isCircle =
I have two data structure classes (this is a simplified version of my code)
This is a simplified version of what I want to do. Basically I have
I am making a simplified version of the ball physics app found at this
This is a simplified version of a query we are running where we need

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.