有什么区别?和= ??在飞镖?

发布于 2025-01-25 09:44:14 字数 1365 浏览 1 评论 0原文

在下面的代码以实现Singleton设计模式中,我们采取了两种不同的方式。 其中一个创建singleton,但每次我们调用它时,另一个都会创建一个对象。 ??= ??在DART中编写的代码中有什么区别?

class ExampleStateByDefinition extends ExampleStateBase {
  static ExampleStateByDefinition? _instance;

  ExampleStateByDefinition._internal() {
    initialText = 'A new "ExampleStateByDefinition" instance has been created.';
    stateText = initialText;
    print(stateText);
  }

  static ExampleStateByDefinition? getState() {
    return _instance ?? ExampleStateByDefinition._internal();     //here we use ??
  }
}

上面的代码每次调用时都会创建一个对象,因此让我们查看另一个变体:

class ExampleStateByDefinition extends ExampleStateBase {
  static ExampleStateByDefinition? _instance;

  ExampleStateByDefinition._internal() {
    initialText = 'A new "ExampleStateByDefinition" instance has been created.';
    stateText = initialText;
    print(stateText);
  }

  static ExampleStateByDefinition? getState() {
    _instance ??= ExampleStateByDefinition._internal();        //Here we use ??=
    return _instance;
  }
}

上面的代码在设计模式:可重复使用的面向对象软件的元素

In the code below to implement the Singleton design pattern, we approach two different ways.
One of them creates Singleton but the other creates an object each time we call it.
What's the difference between ?? and =?? in the code below written in dart?

class ExampleStateByDefinition extends ExampleStateBase {
  static ExampleStateByDefinition? _instance;

  ExampleStateByDefinition._internal() {
    initialText = 'A new "ExampleStateByDefinition" instance has been created.';
    stateText = initialText;
    print(stateText);
  }

  static ExampleStateByDefinition? getState() {
    return _instance ?? ExampleStateByDefinition._internal();     //here we use ??
  }
}

The above code creates an object each time we call it, so let us look at another variant:

class ExampleStateByDefinition extends ExampleStateBase {
  static ExampleStateByDefinition? _instance;

  ExampleStateByDefinition._internal() {
    initialText = 'A new "ExampleStateByDefinition" instance has been created.';
    stateText = initialText;
    print(stateText);
  }

  static ExampleStateByDefinition? getState() {
    _instance ??= ExampleStateByDefinition._internal();        //Here we use ??=
    return _instance;
  }
}

The above code implements Singleton by definition in Design Patterns: Elements of Reusable Object-Oriented Software

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2025-02-01 09:44:14

?? =操作员是复合分配运算符。
就像target + = 1相当于target = target + 1(但使用target仅评估一次,如果是复杂的表达式),
target ?? =表达式等效于target = target ??表达式(但是使用Target仅一旦评估了 如果target是非 - null null 代码>)。

因此,不同之处在于,第一个代码可能不起作用,第二个代码可以。

代码:

 return _instance ?? ExampleStateByDefinition._internal();

检查_Instance是否是非 - null,如果是,则返回_instance的值。如果是null,它将评估并返回exkplestatebydefinition._internal()
no-在其中分配给_instance。该代码可能无法执行其打算做的事情 - 填充一个值。

代码:

 _instance ??= ExampleStateByDefinition._internal();  
 return _instance;

或其更简化的版本:

 return _instance ??= ExampleStateByDefinition._internal();  

还将检查_instancenull,并返回其值(如果不是)。
如果_instancenull,它还评估exkplestateByDefinition._internal(); and ,然后将其分配给_instance _instance < /code>,并返回值。

下次您到处时,_Instance将是非null,而懒惰的缓存作品。

The ??= operator is a compound assignment operator.
Just like target += 1 is equivalent to target = target + 1 (but with target only evaluated once, if it's a complicated expression),
target ??= expression is equivalent to target = target ?? expression (but with target only evaluated once and the assignment not even happening if target is non-null).

So, the difference is that the first code probably doesn't work, the second one does.

The code:

 return _instance ?? ExampleStateByDefinition._internal();

checks whether _instance is non-null, and if so, it returns the value of _instance. If it is null, it evaluates and returns ExampleStateByDefinition._internal().
No-where in that does it assign to _instance. So, _instance is always going to be null, and the code is probably failing to do what it intended to do—caching a value.

The code:

 _instance ??= ExampleStateByDefinition._internal();  
 return _instance;

or its more streamlined version:

 return _instance ??= ExampleStateByDefinition._internal();  

will also check if _instance is null and return its value if it isn't.
If _instance is null, it also evaluates ExampleStateByDefinition._internal();, and then it assigns it to _instance, and returns the value.

The next time you come around, _instance will be non-null, and the lazy caching works.

尹雨沫 2025-02-01 09:44:14

???? = null-wawe 运算符

差异

两个运算符之间的差异是前一个评估的表达式,而后者也分配了表达式的结果。

这就是为什么您的第一个示例每次都会返回新实例,因为您从未分配 将其归为静态变量。

??

使用?当您想评估并返回表达式时,如果另一个表达式解析为null。

以下表达式:

exp ?? otherExp;

类似于此表达式:

((x) => x == null ? otherExp : x)(exp);

?? =

使用?? =当您要为对象分配值时,如果该对象为null。否则,返回对象。

以下表达式:

obj ??= value;

与此表达式相似:

((x) => x == null ? obj = value : x)(obj);

请参见 null - DART中的操作员供参考。

Both ?? and ??= are null-aware operators.

Difference

The difference between the two operators is that the former only evaluates an expression while the latter also assigns the result of the expression.

This is why your first sample returns a new instance each time since you never assign it to your static variable.

??

Use ?? when you want to evaluate and return an expression IFF another expression resolves to null.

The following expression:

exp ?? otherExp;

Is similar to this expression:

((x) => x == null ? otherExp : x)(exp);

??=

Use ??= when you want to assign a value to an object IFF that object is null. Otherwise, return the object.

The following expression:

obj ??= value;

Is similar to this expression:

((x) => x == null ? obj = value : x)(obj);

See Null-aware operators in Dart for reference.

故事和酒 2025-02-01 09:44:14

基于 dart tour

分配仅当变量为null时,我们使用?? =操作。

expr1 ?? expr2如果expr1不是null,请返回其值;否则,评估并返回expr2的值。

原因??操作每次调用对象时都会评估它,都会创建一个新对象。

Based on dart tour.

To assign only if the assignment to the variable is null we use ??= operation.

expr1 ?? expr2 if expr1 is not null, return its value; otherwise, evaluates and return the value of expr2.

Cause ?? operation evaluates each time we call the object, it creates a new object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文