문제

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;
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top