MVVM 基金会:定义为 RelayCommand 时为何返回 ICommand
我正在使用 MVVM Foundation 中的 SampleModel 项目。为什么 ICommand
返回时 _decrementCommand
声明为 RelayCommand
。我知道 RelayCommand
继承 ICommand
但只返回 RelayCommand
不会更清楚吗?
public ICommand DecrementCommand {
get { return _decrementCommand ?? (_decrementCommand = new RelayCommand(() => --this.Value)); }
}
RelayCommand _decrementCommand;
i am using the SampleModel project from MVVM Foundation.Why is ICommand
returned when _decrementCommand
is declared as RelayCommand
. I know RelayCommand
inherits ICommand
but won't is be clearer to just return a RelayCommand
?
public ICommand DecrementCommand {
get { return _decrementCommand ?? (_decrementCommand = new RelayCommand(() => --this.Value)); }
}
RelayCommand _decrementCommand;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用可以逃脱的最不特定类型的原则。具体或特定类型是一种负担,因为它们更有可能暴露调用者不需要了解的内容,并且更有可能需要更改(使它们成为实现细节)。
在这种情况下,如果您始终返回 ICommand,那么您可以更改底层命令类型,而不会中断调用者。您还可以预期,调用者不必了解您的函数的用途,而不必了解超出他们实际应了解的信息,这意味着他们不太可能违反 OO 原则。
This is the principle of using the least specific type you can get away with. Concrete or specific types are a liability because they are more likely to expose things that callers don't need to know about and they are more likely to need to be changed (making them an implementation detail).
In this case, if you always return ICommand, then you can change the underlying command type without breaking callers. You can also expect that the callers won't have to know more about what your function does than they actually should know, meaning that they will be less likely to break OO principles.