Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Equal immutable objects

 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When do we need to create two or more equal immutable objects?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, generally, never, at least not knowingly. But to avoid it, you usually need a cache of all the objects you've created, and keeping the cache, and searching through it to find the appropriate object to reuse, can be more computationally expensive than creating a new one -- at least for small, simple objects.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yosi,
Just as Ernest suggested, you might want to have some logic to avoid such scenarios. In that case, you might want to encapsulate the class creation logic in a class factory and not instantiate the objects directly. This might be overkill for simple projects but certainly worth the pain if you have to apply complex logic to determine the class/subclass that you need to create under certain preconditions and dont want to repeat the logic everywhere. There is more to the factory pattern, but you might use it to yor advantage in situations like this too.
Kindof like:
instead of doing :
MyObject o = new MyObject(params...);
do:
MyObject o = MyObjectFactory.CreateMyObject(params...);
CreateMyObject will contain the logic of determinining a valid MyObject to return.
-ortimus
"Simplicity is virtue"
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic