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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:31:36+00:00 2026-05-29T04:31:36+00:00

I have a problem with understanding of some basic OOP concepts. I’ll try to

  • 0

I have a problem with understanding of some basic OOP concepts. I’ll try to describe it with an example. Let’s say I have an application which should draw figures of different shapes.

I create a Figure class and add a field Shape (which is kind of enumeration and can be Shape.Circle or Shape.Square).

public class Figure {
    public Shape shape;

    public void draw() {
        if( shape == Shape.Square )
            // draw a square
        else if( shape == Shape.Circle )
            // draw a circle
    }
}

When I need to add more shapes to my application I have to add more ifs to draw() method. I think it’s bad.

I can make Figure class abstract (or make it an interface), inherit figures of concrete shape from this class and override draw() method.

public abstract Figure { void draw(); }

public class Circle extends Figure {
    @Override public void draw() {
        // draw a circle
    }
}

// same for Square

When I need new shape I just add new class.

Next I decide I want to have a color for my figures: Black or White. Black square and white square should be drawn differently. The problem looks the same. I can add color field to Figure class and deal with ifs in each draw method or create classes like BlackCircle, BlackSquare, WhiteCircle, WhiteSquare.

Later if I decide to add another property for Figure (let’s say Size which can be Small, Medium or Large) I have to create 2 * 2 * 3 classes like BigBlackCircle, SmallWhiteSquare and so on. And I can’t change figure’s color or shape in runtime. I think it’s not the right way.

Trying to understand the problem I found out that I can still have single class for all figures. Then I store color, shape, size as fields and add kind of DisplayManager class responsible for drawing. I can have different implementations of DisplayManager for different drawing algorithms.

public class Figure {
    public Shape shape;
    public Color color;
    public Size size;
    public DisplayManager display;

    public void draw() {
        display.draw(this);
    }
}

public class DisplayManager {
    public void draw( Figure figure ) {
        // drawing based on figure's shape, color and size
    }
}

But this way I come back to the problem from step 1: I have to deal with a lot of ifs in draw() method. Could anyone explain the correct way here? How should I design the classes to save flexibility of my application?

  • 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-29T04:31:37+00:00Added an answer on May 29, 2026 at 4:31 am

    Instead of declaring DisplayManager.draw(Figure), declare Figure.draw(DisplayManager). Implement for DisplayManager some methods so it could be used from Figure [drawPoint(), drawLine(),….]

    And for each extension of Figure, implement its own draw(DisplayManager) which uses the specific instance of given DisplayManager

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

Sidebar

Related Questions

I think I have some basic understanding problem so maybe someone's able to help
I have some small problem in understanding Value Objects in Flex... I'm trying to
i'm starting to work with php basics and i have some problem understanding how
hello everyone I have some problem with understanding this piece of the code: import
I have very basic understanding problem of Content types. I went through lot of
I have a problem understanding some code fragment from Matlab Signal processing Toolbox fir2()
I have some problem understanding the compound assignment operator and the assignment operator in
I seem to have a problem understanding how to conditionally test a boolean value
I think I have a problem in understanding the proper way of using MVC.
I have an understanding problem of how the following code works: XMLInputFactory xif =

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.