• Post Reply Bookmark Topic Watch Topic
  • New Topic

Immutable  RSS feed

 
Tom Joiner
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a class like this:


Now I don't want ONE to be adjustable, I want it to be immutable, but only this one instance of the class. What is the best way to do this?

Here are some ideas..

Make an anonymous inner class, like this:


Another idea would be to put a test in the setValue like this:


What is the correct way to do this?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the preferred solution, for me, would be Don't Do This. Just make the class immutable. Of course I don't know all the details of what you're planning to do here, but my gut feeling is there's no reason a RumanNumeral would need to be mutable. And in this way you'll avoid a number of complications.

Now if for some reason you really do need both mutable and immutable versions of this class, I would want to identify them as different types. The immutable instance should not have a set() method, because that will just confuse other developers. Maybe you could make RomanNumeral an interface, with no set method, and then make MutableRomanNumeral an implementation of the interface which adds the set() method. Then ONE could be another implementation, either an ImmutableRomanNumeral class or an anonymous implementation of RomanNumeral. The key is that anyone who has a reference to ONE should not even think it has a set() method.

Another option might be to implement ONE with an anonymous class that overrides set() to throw an UnsupportedOperationException. I don't like this much, but it at least seems better than doing nothing in response to set(). If anyone is calling set() on ONE, they shouldn't, and I think it's much better to thor an exception that will make the problem obvious, than to just ignore the call without telling anyone. The latter just makes it harder to find the problem. But again, I'd prefer the solutions in the first or second paragraphs to this one.
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go with your first option. ie. use the inner class as a special case
for ONE, since you still want to allow mutability for other instances.
 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think throwing the exception is a better option than doing nothing, because it will convey atleast some kind of information than doing nothing..
i will go for that option...
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Perhaps going through this thread will help you a little further!

At the same, please don't forget to look at this simple but excellent article which speaks about the Mutable Vs Immutable aspects of the class and how to achieve the same!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!