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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:39:15+00:00 2026-06-16T20:39:15+00:00

It’s tricky trying to come up with a meaningful question title for this. I’ll

  • 0

It’s tricky trying to come up with a meaningful question title for this. I’ll explain exactly what I’m trying to do.

I have created an Android graphics library that builds a potentially large list of Path objects. I now have a need to attach some additional data to each Path. To do this, I have simply subclassed Path (call it PathSubclass) to provide the additional extended data fields. In general use cases, only very few of those Path objects will actually have extra data set on them. For example, there could be an ArrayList of several thousand Paths, but only a handful have additional data. As I develop the library, it is possible that the amount of extra data I want to attach to each Path will grow considerably. I appreciate this may be premature optimisation, but I am concerned that it is not efficient to subclass an object to contain some additional data when that additional data is very rarely populated and considerably large collections of that object are used.

What I thought of doing was perhaps having an ExtendedPathData class, separate from my Path subclass. Therefore each Path subclass is only bloated a little by the need for a mExtendedData member, which in many cases will be null, except for those very few Path objects that require the additional data. I suppose this is similar to how one would usually attach additional data to a View, using the setTag() method.

Is there any other approach I should consider, that is better for any reason?

  • 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-16T20:39:16+00:00Added an answer on June 16, 2026 at 8:39 pm

    You could use composition instead of inheritance.

    Idea 1

    A PathData class might contain a reference to a Path and a ExtendedPathData:

    // encapsulate properly in your code, this is
    // just a short sample
    class PathData {
      public Path path;
      public ExtendedPathData data;
    }
    

    This enables you to set the data member to null. Each PathData instance has a size of constantly two references. When the number of members in ExtendedPathData grows, the actual memory consumption grows only for PathData instances that have a non-null data.

    Idea 2

    You could shave off one more reference by using an interface with two implementations:

    interface PathData {
      Path path();
      ExtendedPathData data();
    }
    
    final class NullPathData implements PathData {
      Path path;
      public Path path() { return path; }
      public ExtendedPathData data() { return null; }
    }
    
    final class ExtendedPathData implements PathData {
      Path path;
      ExtendedPathData data;
      public Path path() { return path; }
      public ExtendedPathData data() { return data; }
    }
    

    It’s up to you to decide whether having two implementations is really worth the code, as you need a factory method here to decide what implementation to use. It’s one 4-byte-reference per instance…

    Further stuff

    If ExtendedPathData is immutable and many equal instances may exist, you could also pool them by exposing a static factory method that uses an instance pool instead of a public constructor.

    In case you’ve got Effective Java by Joshua Bloch, here are further references explaining the tips in detail (in a more general context):

    • Item 1 Consider static factory methods instead of constructors
    • Item 16 Favor composition over inheritance
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to loop through a bunch of documents I have to put

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.