File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Immutable objects.

 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the article Mutable and Immutable Objects in the newsletter and found it very informative. It made me realize how easy it is for an object reference to slip thru the cracks and cause your immutable object to become mutable. What was not addressed in the article though was the use of factory methods to get instances of immutable objects and declaring the default constuctor private. Of course this would only concern classes that were Strongly Immutabe. I've seen it done both ways. If you only have one or two ways to construct a strongly immuatable object doesn't it make more sense to use factory methods to get instances?
Michael Morris
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. Hopefully I'll get this included in the follow up article and in the next newsletter as long as the baby isn't early
Factory methods are great, even better since they can promote instance sharing and the ability to use == comparison, but I find it only really works with relatively simple immutable objects. More complex immutable objects are a different topic, but basically I'm going to say they're combersme and almost unusable without defending my point of view here
Of course 'Enumerated type' types and other types with finite values are also great when accessed as immutables via factories.
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David. I'm in the middle of redisigning a structural steel detailing application (actually I'm porting everthing to JAVA from a mixture of languages) and have a lot of simple classes that encapsulate values peculiar to that domain space. Most of them are constucted with a single double (oxymoron? ) so I've been using factory methods to get instances.
Michael Morris
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic