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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:07:01+00:00 2026-05-12T15:07:01+00:00

From what I`ve learned, it is no good if you frequently use downcasting in

  • 0

From what I`ve learned, it is no good if you frequently use downcasting in class hierarchies.
I agree with that, but what are exceptions from this rule if any?
This is where my design of graphical editor shows thin: I have two hierarchies, where geometric figures hierarchy decoupled from graphic primitives one. Like this:

public class GeometricPrimitive {...}
public class RectangeGeometric: Geometric Primitive {...} 
public class GraphicPrimitive {...}
public class Rectangle: GraphicPrimitive {
 private RectangleGeometric figure;
 ...
}

So, every concrete graphic figure class encapsulates instance of concrete geometry class.
Is that approach is the right one, or should I prefer more generical one? – unfortunately, downcasting would be used in this case:

public class GraphicPrimitive {
   protected GeometryPrimitive figure; 
   ....
}

public class Rectangle: GraohicPrimitive {

        public Rectangle(Color c, TwoDPoint leftHighPoint, TwoDPoint rightLowPoint): 
            base(new RectangleGeometric(leftHighPoint.Point2D, rightLowPoint.Point2D), c) {   }

        #region Geometric Properties

        public TwoDPoint LeftTopCorner {
            get { return new TwoDPoint(base.color, (base.figure as RectangleGeometric).LeftTopCorner); }
        }

        public TwoDPoint RightBottomCorner {
            get { return new TwoDPoint(base.color, (base.figure as RectangleGeometric).RightBottomCorner); }
        }
  • 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-12T15:07:01+00:00Added an answer on May 12, 2026 at 3:07 pm

    While your question lacks some of the larger context about your application that would help with giving a specific answer, I’ll try by giving you some ideas of how I would implement this using your code for inspiration.

    I would start by inverting the relationship GeometryPrimitive and GraphicPrimitive. I see the the GeometryPrimitive hierarchy as the domain objects that make up your abstract scene graph and the GraphicPrimitive hierarchy as low level view components that translate a GeometryPrimitive into a set of pixels appropriate for drawing onto some kind of graphics context. The GeometryPrimitive subclasses hold all the state information necessary to describe themselves but no logic for translating that description into pixels. The GraphicPrimitive subclasses have all the pixel pushing logic, but no internal state. In effect, the GraphicPrimitive hierarchy represents a hierarchy of Command Objects.

    In the GeometryPrimitive base class, include an abstract method called GetGraphicPrimitive(). In the GraphicPrimitive base class include an abstract method called Draw(Graphics g).

    Within each GeometryPrimitive, include the appropriate GraphicPrimitive for drawing the object and an accessor method for accessing it. To draw the entire scene, walk your structure of GeometryPrimitive objects, asking each one for its GraphicPrimitive and then invoking the Draw() method.

    abstract class GeometryPrimitive
    {
        public abstract GraphicsPrimitive GetGraphicsPrimitive();
    }
    
    abstract class GraphicsPrimitive
    {
        public abstract void Draw(Graphics g);
    }
    
    class RectangleGeometryPrimitive : GeometryPrimitive
    {
        public Point TopLeft {get; set;}
        public Point BottomRight {get; set;}
    
        private RectangleGraphicPrimitive gp;
    
        public RectanglePrimitive(Point topLeft, Point bottomRight);
        {
            this.TopLeft = topLeft;
            this.BottomRight = bottomRight;
            this.gp = new RectangleGraphicsPrimitive(this);
        }
    
        public GraphicsPrimitive GetGraphicsPrimitive()
        {
            return gp;
        }
    }
    
    class RectangleGraphicsPrimitive : GraphicsPrimitive
    {
        private RectangleGeometryPrimitive p;
    
        public RectangleGraphicsPrimitive(RectangleGeometryPrimitive p)
        {
            this.p = p;
        }
    
        public void Draw(Graphics g)
        {
            g.DrawRectangle(p.TopLeft, p.BottomRight);
        }
    }
    
    class CircleGeometryPrimitive : GeometryPrimitive
    {
        public Point Center {get; set;}
        public int Radius {get; set;}
    
        private CircleGraphicPrimitive gp;
    
        public RectanglePrimitive(Point center, int radius);
        {
            this.Center = center;
            this.Radius = radius;
            this.gp = new CircleGraphicsPrimitive(this);
        }
    
        public GraphicsPrimitive GetGraphicsPrimitive()
        {
            return gp;
        }
    }
    
    class CircleGraphicsPrimitive : GraphicsPrimitive
    {
        private CircleGeometryPrimitive p;
    
        public CircleGraphicsPrimitive(CircleGeometryPrimitive p)
        {
            this.p = p;
        }
    
        public void Draw(Graphics g)
        {
            g.DrawCircle(p.Center, p.Radius);
        }
    }
    

    As you can see above, no downcasting is required to draw GeometryPrimitives to the screen. With proper use of inheritance, you can also share GraphicsPrimitive objects between different GeometryPrimitives. For example, SquareGeometryPrimitive and RectangleGeometryPrimitive can both use RectangleGraphicsPrimitive if SquareGeometryPrimitive derives from RectangleGeometryPrimitive.

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

Sidebar

Related Questions

UPDATE: I recently learned from this question that in the entire discussion below, I
I've learned from the latest Java developments, that throwing a RuntimeException and handling it
I have learned from various tutorial that If a client can reasonably be expected
As I just learned from this question , .NET regexes can access individual matches
To preface this, the most I know about text encoding I learned from Joel
From what I learned, a MySQL MASTER server is one that can both read
From this question I learned Double.NaN is not equal to itself. I was verifying
Basically from C++ FAQ I learned that: A virtual function allows derived classes to
If we've learned anything from HTML/CSS it's that, declarative languages (like XML) do a
I've learned enough to begin writing programs from scratch, but I'm running into 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.