Variable or immutable class?

I read in some design books that an immutable class improves scalability and its good practice to write an immutable class where possible. But I believe that such an immutable class increases the distribution of objects. Is it good to go to an immutable class or is it better to go to a static class (a class with all static methods) to improve scalability?

+9
java immutability design scalability
Aug 16 '09 at 16:44
source share
8 answers

Immutable classes contribute to the distribution of objects, but if you want security, mutable objects will increase the distribution of objects because you need to return copies, not the original, so that the user cannot modify the returned object.

Regarding the use of classes with all static methods, this is not really an option in most cases where immutability can be used. Take this example from an RPG:

public class Weapon { final private int attackBonus; final private int accuracyBonus; final private int range; public Weapon(int attackBonus, int accuracyBonus, int range) { this.attackBonus = attackBonus; this.accuracyBonus = accuracyBonus; this.range = range; } public int getAttackBonus() { return this.attackBonus; } public int getAccuracyBonus() { return this.accuracyBonus; } public int getRange() { return this.range; } } 

How exactly do you implement this with a class that contains only static methods?

+5
Aug 16 '09 at 19:43
source share
β€” -

The main advantage of immutable is that you can expose internal data members unchanged because the caller cannot modify them. This is a huge problem, say java.util.Date . It is modified, so you cannot return it directly from the method. This means that you are doing all kinds of protective copies . This increases the proliferation of objects.

Another important advantage is that immutable objects, by definition, do not have synchronization . What is where scalability problems arise . Writing multithreading code is complicated. Immutable objects are a good way (mostly) to work around the problem.

As for the "static classes", in your comment, I believe that these are classes with factory methods , as is usually described. This is an unrelated picture. Both mutable and immutable classes can have either public constructors or private constructors with static factory methods. This does not affect (im) the variability of the class, since the mutable class is the one whose state can be changed after creation, while the unchanged state of the class cannot be changed after the instance is created.

Static factory methods may have other advantages. The idea is to encapsulate the creation of an object.

+12
Aug 16 '09 at 16:47
source share

As cletus says , immutable classes simplify class development and processing in synchronized methods.

They also simplify collection processing, even in single-threaded applications. An immutable class will never change, so the key and the hash code will not change, so you will not ruin your collections.

But you must keep in mind the life cycle of the model you are modeling and the "weight" of the constructor. If you need to change a thing, immutable objects become more complex. You should replace them, not change them. Not scary, but worth considering. And if the constructor takes non-trivial time, this is also a factor.

+1
Aug 16 '09 at 17:08
source share

One thing to keep in mind: if you intend to use class instances as keys in a HashMap, or if you intend to put them in a HashSet, it is safer to make them immutable.

HashMap and HashSet rely on the fact that the hash code for the object remains constant while the object is on the map or specified. If you use the object as a key in a HashMap or put it in a HashSet and then change the state of the object so that hashCode () returns a different value, then you mislead a HashMap or HashSet and you get strange things; for example, when you iterate over a card or install an object, there is, but when you try to get it, it is as if it is not.

This is due to the way HashMap and HashSet work internally - they organize objects by hash code.

This article by Java concurrency guru Brian Goetz gives a good overview of the pros and cons of immutable objects.

+1
Aug 16 '09 at 18:44
source share

Continuity is usually used to achieve scalability, since immutability is one of the factors when it comes to parallel programming in java. Therefore, although you indicate that there may be more objects in an β€œimmutable” solution, this may be a necessary step to improve concurrency.

Another equally important use of immutability is to use design intent; the one who created the invariable class planned to put the changing state in another place. If you start mutating instances of this class, you probably violate the original intent of the project - and who knows what the consequences may be.

0
Aug 16 '09 at 17:25
source share

Consider, for example, string objects. Some languages ​​or class libraries provide mutable strings; some do not.

A system that uses immutable strings can perform certain optimizations that cannot be changed with mutable strings. For example, you can make sure that there is only one copy of any unique string. Since the size of the overhead object is usually much smaller than the size of any non-trivial string, this is a potentially huge memory saving. There are other potential space-saving options, such as interning substrings.

In addition to potential memory savings, immutable objects can increase scalability by reducing competition. If you have a large number of threads accessing the same data, immutable objects do not require complex synchronization processes for secure access.

0
Aug 16 '09 at 17:45
source share

Another consideration for the subject. Using an immutable object allows you to cache them and not re-create them every time (i.e. strings), this helps a lot in the performance of your application.

0
Aug 16 '09 at 17:52
source share

I think if you want to use the same object among different variables, it should be immutable.

For example:

 String A = "abc"; String B = "abc"; 

The String object in Java is immutable. Now both A and B point to the same string "abc". Now

 A = A + "123"; System.out.println(B); 

he should output:

 abc 

Since String is immutable, A will simply point to the new string object "abc123" instead of changing the previous string object.

0
Aug 17 '09 at 4:39
source share



All Articles