Mutable vs immutable objects

前端 未结 12 1564
囚心锁ツ
囚心锁ツ 2020-11-22 09:17

I\'m trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I\'m hav

相关标签:
12条回答
  • 2020-11-22 09:31

    If you return references of an array or string, then outside world can modify the content in that object, and hence make it as mutable (modifiable) object.

    0 讨论(0)
  • 2020-11-22 09:34

    Shortly:

    Mutable instances is passed by reference.

    Immutable instances is passed by value.

    Abstract example. Lets suppose that there exists a file named txtfile on my HDD. Now, when you are asking me to give you the txtfile file, I can do it in the following two modes:

    1. I can create a shortcut to the txtfile and pass shortcut to you, or
    2. I can do a full copy of the txtfile file and pass copied file to you.

    In the first mode, the returned file represents a mutable file, because any change into the shortcut file will be reflected into the original one as well, and vice versa.

    In the second mode, the returned file represents an immutable file, because any change into the copied file will not be reflected into the original one, and vice versa.

    0 讨论(0)
  • 2020-11-22 09:36

    Check this blog post: http://www.yegor256.com/2014/06/09/objects-should-be-immutable.html. It explains why immutable objects are better than mutable. In short:

    • immutable objects are simpler to construct, test, and use
    • truly immutable objects are always thread-safe
    • they help to avoid temporal coupling
    • their usage is side-effect free (no defensive copies)
    • identity mutability problem is avoided
    • they always have failure atomicity
    • they are much easier to cache
    0 讨论(0)
  • 2020-11-22 09:37

    A mutable object is simply an object that can be modified after it's created/instantiated, vs an immutable object that cannot be modified (see the Wikipedia page on the subject). An example of this in a programming language is Pythons lists and tuples. Lists can be modified (e.g., new items can be added after it's created) whereas tuples cannot.

    I don't really think there's a clearcut answer as to which one is better for all situations. They both have their places.

    0 讨论(0)
  • 2020-11-22 09:40

    Immutable means can't be changed, and mutable means you can change.

    Objects are different than primitives in Java. Primitives are built in types (boolean, int, etc) and objects (classes) are user created types.

    Primitives and objects can be mutable or immutable when defined as member variables within the implementation of a class.

    A lot of people people think primitives and object variables having a final modifier infront of them are immutable, however, this isn't exactly true. So final almost doesn't mean immutable for variables. See example here
    http://www.siteconsortium.com/h/D0000F.php.

    0 讨论(0)
  • 2020-11-22 09:45

    Unmodifiable - is a wrapper around modifiable. It guarantees that it can not be changed directly(but it is possibly using backing object)

    Immutable - state of which can not be changed after creation. Object is immutable when all its fields are immutable. It is a next step of Unmodifiable object

    Thread safe

    The main advantage of Immutable object is that it is a naturally for concurrent environment. The biggest problem in concurrency is shared resource which can be changed any of thread. But if an object is immutable it is read-only which is thread safe operation. Any modification of an original immutable object return a copy

    source of truth, side-effects free

    As a developer you are completely sure that immutable object's state can not be changed from any place(on purpose or not). For example if a consumer uses immutable object he is able to use an original immutable object

    compile optimisation

    Improve performance

    Disadvantage:

    Copying of object is more heavy operation than changing a mutable object, that is why it has some performance footprint

    To create an immutable object you should use:

    1. Language level

    Each language contains tools to help you with it. For example:

    • Java has final and primitives
    • Swift has let and struct[About].

    Language defines a type of variable. For example:

    • Java has primitive and reference type,
    • Swift has value and reference type[About].

    For immutable object more convenient is primitives and value type which make a copy by default. As for reference type it is more difficult(because you are able to change object's state out of it) but possible. For example you can use clone pattern on a developer level to make a deep(instead of shallow) copy.

    2. Developer level

    As a developer you should not provide an interface for changing state

    [Swift] and [Java] immutable collection

    0 讨论(0)
提交回复
热议问题