有什么区别?和= ??在飞镖?
在下面的代码以实现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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
?? =
操作员是复合分配运算符。就像
target + = 1
相当于target = target + 1
(但使用target
仅评估一次,如果是复杂的表达式),target ?? =表达式
等效于target = target ??表达式
(但是使用Target
仅一旦评估了和 和如果target
是非 -null null 代码>)。
因此,不同之处在于,第一个代码可能不起作用,第二个代码可以。
代码:
检查
_Instance
是否是非 -null
,如果是,则返回_instance
的值。如果是null
,它将评估并返回exkplestatebydefinition._internal()
。no-在其中分配给
_instance
。该代码可能无法执行其打算做的事情 - 填充一个值。代码:
或其更简化的版本:
还将检查
_instance
是null
,并返回其值(如果不是)。如果
_instance
是null
,它还评估exkplestateByDefinition._internal();
and ,然后将其分配给_instance
_instance < /code>
,并返回值。
下次您到处时,
_Instance
将是非null
,而懒惰的缓存作品。The
??=
operator is a compound assignment operator.Just like
target += 1
is equivalent totarget = target + 1
(but withtarget
only evaluated once, if it's a complicated expression),target ??= expression
is equivalent totarget = target ?? expression
(but withtarget
only evaluated once and the assignment not even happening iftarget
is non-null
).So, the difference is that the first code probably doesn't work, the second one does.
The code:
checks whether
_instance
is non-null
, and if so, it returns the value of_instance
. If it isnull
, it evaluates and returnsExampleStateByDefinition._internal()
.No-where in that does it assign to
_instance
. So,_instance
is always going to benull
, and the code is probably failing to do what it intended to do—caching a value.The code:
or its more streamlined version:
will also check if
_instance
isnull
and return its value if it isn't.If
_instance
isnull
, it also evaluatesExampleStateByDefinition._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.??
和?? =
是 null-wawe 运算符。差异
两个运算符之间的差异是前一个评估的表达式,而后者也分配了表达式的结果。
这就是为什么您的第一个示例每次都会返回新实例,因为您从未分配 将其归为静态变量。
??
以下表达式:
类似于此表达式:
?? =
以下表达式:
与此表达式相似:
请参见 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.
??
The following expression:
Is similar to this expression:
??=
The following expression:
Is similar to this expression:
See Null-aware operators in Dart for reference.
基于 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.