• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design Issue - Need assistance

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going to refactor a class that has around 10 methods. In every method I am instantiating the same class, lets call it "Display class".

So its almost 10 instantiations of same instance. Wouldn't it be a better design to put that instantiation in a singleton class and invoke the singleton from all the 10 methods. Wouldn't it save a lot of memory in heap.

If singleton is NOT correct kindly let me know why and other work arounds for this type of issues.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Several questions need to be answered before you can determine if you can share a common instance of this class.

1. What is the lifetime of the object created in these methods? (Is it used after the method exists? Are any references to the object kept?)

2. Is the object expensive to create? Does it use a lot of memory? (What benefit are you trying to get from reusing the object?)

3. Could the single instance be accessed from multiple threads and, if so, is it thread-safe?

4. What are the implications of this class living through multiple invocations? Does it's state need to be reset? Can it be reset?

Some objects are very cheap to create and it's faster to create them rather than clean up after each use.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And even the instance is expensive to create, I'd very likely *not* use a singleton. Instead I might be thinking about handing the instance around or something. There also might be an abstraction missing.

Can you tell us more about what that instance is and how it is used? Perhaps show some code?
 
author
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
And even the instance is expensive to create, I'd very likely *not* use a singleton. Instead I might be thinking about handing the instance around or something. There also might be an abstraction missing.



I agree. I'll also point out that instantiation may be much faster than you think. On JVMs, the allocation of a single object is insanely fast compared to heap allocation in C or C++.

Michael Feathers
http://michaelfeathers.typepad.com
http://www.objectmentor.com
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry about this object,before you are sure creating them is expensive.
reply
    Bookmark Topic Watch Topic
  • New Topic