什么是不变性,为什么我要为此担心呢? [英] What is immutability and why should I worry about it?

查看:163
本文介绍了什么是不变性,为什么我要为此担心呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇关于不变性的文章,但是仍然不太熟悉这个概念.

I've read a couple of articles on immutability but still don't follow the concept very well.

我最近在这里创建了一个线程,其中提到了不变性,但是由于这本身就是一个话题,所以我现在正在制作一个专用线程.

I made a thread on here recently which mentioned immutability, but as this is a topic in itself, I am making a dedicated thread now.

我在过去的线程中提到,我认为不变性是使对象成为只读对象并赋予其较低可见性的过程.另一位成员说,这与这实际上没有任何关系.

I mentioned in the past thread that I thought immutability is the process of making an object read only and giving it low visibility. Another member said it didn't really have anything to do with that. This page (part of a series) uses an example of an immutable class/struct and it uses readonly and other concepts to lock it down.

在此示例中,状态的定义究竟是什么?状态是我尚未真正掌握的概念.

What exactly is the definition of state in the case of this example? State is a concept which I haven't really grasped.

从设计准则的角度来看,一个不变的类必须是不接受用户输入并且实际上只会返回值的类?

From a design guideline perspective, an immutable class must be one which does not accept user input and really would just return values?

我的理解是,仅返回信息的任何对象都应该是不可变的并且被锁定",对吗?因此,如果要使用该方法在专用类中返回当前时间,则应该使用引用类型,因为它将使用该类型的引用,因此我将从不变性中受益.

My understanding is that any object which just returns information should be immutable and "locked down", right? So if I want to return the current time in a dedicated class with that one method, I should use a reference type as that will work a reference of the type and thus I benefit from immutability.

推荐答案

什么是不变性?

  • 不可移植性主要应用于对象(字符串,数组,自定义Animal类)
  • 通常,如果类的版本不可变,则也可以使用可变版本.例如,Objective-C和Cocoa都定义了NSString类(不可变)和NSMutableString类.
  • 如果对象是不可变的,则在创建对象后将无法对其进行更改(基本上是只读的).您可以将其视为只有构造函数才能更改对象".
  • What is Immutability?

    • Immutability is applied primarily to objects (strings, arrays, a custom Animal class)
    • Typically, if there is an immutable version of a class, a mutable version is also available. For instance, Objective-C and Cocoa define both an NSString class (immutable) and an NSMutableString class.
    • If an object is immutable, it can't be changed after it is created (basically read-only). You could think of it as "only the constructor can change the object".
    • 这与用户输入没有直接关系;甚至您的代码也无法更改不可变对象的值.但是,您始终可以创建一个新的不可变对象来替换它.这是一个伪代码示例;请注意,在许多语言中,您可以简单地执行myString = "hello";而不是像下面那样使用构造函数,但是为了清楚起见,我将其包括在内:

      This doesn't directly have anything to do with user input; not even your code can change the value of an immutable object. However, you can always create a new immutable object to replace it. Here's a pseudocode example; note that in many languages you can simply do myString = "hello"; instead of using a constructor as I did below, but I included it for clarity:

      String myString = new ImmutableString("hello");
      myString.appendString(" world"); // Can't do this
      myString.setValue("hello world"); // Can't do this
      myString = new ImmutableString("hello world"); // OK
      

      您提到一个仅返回信息的对象";这并不能自动使其成为不变性的良好候选者.不可变的对象往往总是返回与其构造时使用的相同的值,因此我倾向于说当前时间并不是理想的选择,因为该时间会经常变化.但是,您可能有一个用特定时间戳创建的MomentOfTime类,并且将来总是返回该时间戳.

      You mention "an object that just returns information"; this doesn't automatically make it a good candidate for immutability. Immutable objects tend to always return the same value that they were constructed with, so I'm inclined to say the current time wouldn't be ideal since that changes often. However, you could have a MomentOfTime class that is created with a specific timestamp and always returns that one timestamp in the future.

      • 如果将对象传递给另一个函数/方法,则不必担心在函数返回后该对象是否具有相同的值.例如:

      • If you pass an object to another function/method, you shouldn't have to worry about whether that object will have the same value after the function returns. For instance:

      String myString = "HeLLo WoRLd";
      String lowercasedString = lowercase(myString);
      print myString + " was converted to " + lowercasedString;
      

      如果lowercase()的实现在创建小写版本时更改了myString怎么办?第三行不会给您想要的结果.当然,好的lowercase()函数不会这样做,但是如果myString是不可变的,则可以保证这一事实.这样,不可变的对象可以帮助实施良好的面向对象的编程实践.

      What if the implementation of lowercase() changed myString as it was creating a lowercase version? The third line wouldn't give you the result you wanted. Of course, a good lowercase() function wouldn't do this, but you're guaranteed this fact if myString is immutable. As such, immutable objects can help enforce good object-oriented programming practices.

      使不可变对象成为线程安全对象更容易

      It's easier to make an immutable object thread-safe

      如果要获取对象的所有实例变量并在纸上写下它们的值,那将是该对象在给定时刻的状态.程序的状态是给定时刻其所有对象的状态.状态随着时间的推移而迅速变化;程序需要更改状态才能继续运行.

      If you were to take all of an object's instance variables and write down their values on paper, that would be the state of that object at that given moment. The state of the program is the state of all its objects at a given moment. State changes rapidly over time; a program needs to change state in order to continue running.

      不变的对象随着时间的流逝具有固定的状态.一旦创建,不可变对象的状态就不会改变,尽管程序的整体状态可能会改变.这样可以更轻松地跟踪正在发生的事情(并查看上面的其他好处).

      Immutable objects, however, have fixed state over time. Once created, the state of an immutable object doesn't change although the state of the program as a whole might. This makes it easier to keep track of what is happening (and see other benefits above).

      这篇关于什么是不变性,为什么我要为此担心呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆