Mutable vs Immutable object

View previous topic View next topic Go down

Mutable vs Immutable object

Post by BIT0112-Rokon on Fri Mar 04, 2011 12:11 am

Im trying to get my head around mutable vs immutable object. Using mutable object gets a lot of bad press. But immutable object are not free from its pitfalls. I have asked one of friend about it. Lets see what he said.

Conversation with crusk8


(08:08:44 PM) Bazlur Rahman: hey

(08:08:53 PM) crusk8: hey

(08:08:59 PM) Bazlur Rahman: how are u ?

(08:09:39 PM) Bazlur Rahman: do u know difference between mutable and immutable object ?

(08:12:20 PM) crusk8: I was just looking at the idea on wikipedia. looks like the idea for immutable objects is to create an object, set to what you want, use it, and then let it alone

(08:13:16 PM) Bazlur Rahman: i have some basic idea about it ... but question is when to use them?

(08:14:16 PM) Bazlur Rahman: mutable object can be changed using mutator, and immutable object cant be changed... this is the main motivation ..right ..

(08:15:19 PM) crusk8: yeah. I guess you could use an immutable objects to modify other objects, but, for safety, that immutable object itself couldn't be modified

(08:17:30 PM) Bazlur Rahman: yup.. dont you think, immutable object is responsible for less performance.. every time, new object is created, old object is dropped, reference is changed, garbage collector is called...

(08:18:27 PM) crusk8: yeah. if you're going to use an immutable object. you should use it cases where that object will almost never change

(08:20:10 PM) Bazlur Rahman: but is it good practice using immutable object always? because it is thread safe, I dont need to do locking, it is protected,

(08:20:49 PM) Bazlur Rahman: and sometime it has good performance... like we dont need to make defensive copy ..

(08:21:39 PM) crusk8: you probably wouldn't need to do locking on that object. for languages like c++ it would be const protected

(08:21:55 PM) crusk8: you only need to do locking when that object tries to modify other objects

(08:24:05 PM) Bazlur Rahman: so whats the point of using mutable or immutable object. should I always immutable object ?

(08:26:39 PM) crusk8: mutable objects are ones that need to be modified a lot. immutable ones are that rare if ever need to be modified. an immutable object could act like a controller for the mutable objects

(08:26:58 PM) crusk8: it can change them but the mutable objects can't change it

(08:29:47 PM) Bazlur Rahman: Im still confused, in which situation, I should use immutable object..? for security issues ?

(08:30:45 PM) crusk8: yeah. you're the limiting the ways that an object can modified. only through reallocating the object

(08:31:03 PM) crusk8: not through mutators

(08:34:12 PM) Bazlur Rahman: hmm but it has some complexity while implementing immutable object I think ..

(08:35:23 PM) crusk8: I'm not sure if immutable means that object can't modify its own internal variables. while processing something internal settings can;t be changed

(08:35:43 PM) crusk8: or just that the outside world can't change things directly

(08:36:12 PM) crusk8: I suppose only data could be local to the function





Im not convinced completely.



By the way,
Here Im gonna tell you some advantage and disadvantage of immutable object.

Advantage:



Protection : you can send and immutable object to any class without worrying about it being altered by that class and you never have to make defensive copy.


Thread safe: Its fully thread safe, any number of threads can access them simultaneously, without any synchronization.



Disadvantage :


Constructor injection : it requires constructor injection while you are gonna implement inversion of control. It makes complexity here. Even I dont like constructor injection. Setter that is mutator is good choice of mine. But in immutable object, there is no way to use mutator.


Implementation complexity : its very much boring to implement immutable object.

This is what I know.


Is there anyone who can clear me all about immutable object?

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Mutable vs Immutable object

Post by BIT0102-Mohaimin on Fri Mar 04, 2011 12:44 am

I have a feeling about mutability of objects. Very general purpose objects (like Numbers, String), specially the primitive ones should be immutable. But classes with big responsibilities should have mutable objects. Because their states are supposed to change frequently in the program. Initializing them is costly, better idea is to alter a little part of it.

For example, imagine a windowed java program. Resizing of the window sets two (may be some more internally) integers, not a big deal. But if JFrame were immutable, we would have to reinitialize the whole object.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum