The*_*uck 3 c# implementation design-patterns interface
我可以理解为什么要针对接口而不是实现进行编程.但是,在以下示例中(我发现了很多):
public interface ISomething
{
void BlahOne(int foo);
void BlahTwo(string foo);
}
public class BaseSomething : ISomething
{
public void BlahOne(int foo)
{
//impl
}
public void BlahTwo(string foo)
{
//impl
}
}
public class SpecificSomethingOne : BaseSomething
{
public void SpecificOne()
{
//blah
}
}
public class SpecificSomethingTwo : BaseSomething
//and on..
Run Code Online (Sandbox Code Playgroud)
目前的例子是我游戏中基于组件的实体系统.(我有IComponent,Component,PosComponent等).
但是,我看不出有ISomething的理由.名称可能看起来更好,但它似乎没有目的.我可以一直返回BaseSomething.
当你有一个基本实现一切使用时,有没有理由有一个接口?(我可以看到IComparable或IEnumerable的用途)
编辑:对于略有不同的情况(但仍然相关,不需要一个不同的问题),如果我假设我有所有的这个结构,如果我ISomething用于参数类型和变量相比,会有很大的区别BaseSomething吗?
And*_*bel 13
我更喜欢"懒惰设计" - 从BaseSomething您需要时提取界面.在此之前,请保持简单并跳过它.
现在我可以想到只有一个实现时有一个接口的两个原因: