Usefulness of immutable objects when the state of a program constantly changes

瘦欲@ 提交于 2019-12-02 04:34:06

"what benefits do such objects give us" you already answered that.

Regarding the "dynamic" part of your question, if you need to "change" an immutable object, you can create a new one from the old one:

Immutable oldObj = new Immutable(...);
Immutable newObj = new Immutable(oldObj.property1, "a new value for property 2");

If you find that you keep doing that repeatedly, then maybe you need to make the object mutable and add the relevant tread-safety features that are needed to be able to use that object in a concurrent environment.

Immutable objects allow you to cleanly communicate changes of state to different threads.

It is a good practice to use immutable objects to represent messages exchanged between threads. Once such message is sent, its payload can not be altered, which prevents many concurrency related bugs. If a thread needs to communicate some further changes, it just sends next messages.

amicngh

Immutable objects are very helpful when you need some static object whose state never changes .Greatest advantage are immutability , object semantics and smart pointers renders object ownership a moot point. Implicitly this also means deterministic behaviour in the presence of concurrency.

Java has already defined some Immutable classes like String Integer.

Other benefit is they always have "failure atomicity" (a term used by Joshua Bloch) : if an immutable object throws an exception, it's never left in an undesirable or indeterminate state .

Let say if you have a global cache of static objects like country codes , here you can apply Immutability.

Why do we need immutable class?

Immutable objects are really useful in cases like this, with a String object:

public class A  {
    private volatile String  currentName = "The First Name";

    public String getCurrentName()  {
        // Fast:  no synching or blocking!  Can be called billions of times by
        // billions of threads with no trouble.
        // (Does need to be read from memory always because it's volatile.)
        return currentName;
    }
    public whatever someMethod()  {
        ... code ...
        // Simple assignment in this case.  Could involve synchronization
        // and lots of calculations, but it's called a lot less than
        // getCurrentName().
        currentName = newName;
        ... code ...
    }
}
public class B  {
        ... in some method ...
        A  objA = something;
        // Gets "name" fast despite a billion other threads doing the same thing.
        String  name = objA.getCurrentName();
        // From this point on, String referenced by "name" won't change
        // regardless of how many times A.currentName changes.
        ... code with frequent references to objA
}

This allows complex data (or even simple data, this case) that must be consistent (if not precisely up-to-date) to be updated and delivered to anybody who wants it very quickly and in a thread-safe manner. The data delivered will soon become outdated, perhaps, but it will keep its value during the calling method and remain consistent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!