简体   繁体   English

c#实现接口的抽象类

[英]c# Abstract Class implementing an Interface

I've seen the following code layout reading forums and other blog posts and adapted in order to ask a few questions. 我看过以下代码布局,阅读了论坛和其他博客文章,并进行了修改,以提出一些问题。

public interface IService<T>
{
    int Add(T entity);
    void Update(T entity);
}

public abstract class ServiceBase<T> : IService<T>
{
    public int Add(T entity) { ... }
    public void Update(T entity) { ... }
}

public interface ICarService : IService<Car>
{
}

public class SomeBaseClass : ServiceBase<Car>, ICarService
{
    public int Add(Car entity);
    public void Update(Car entity);
}

What I don't understand is the benefit of having the abstract class implmenting the interface. 我不明白的是,抽象类实现了接口的好处。 To me it just feels a bit repetitive and I cannot understand the benefit of having an abstract class implementing the interface. 对我来说,感觉有点重复,我无法理解使用抽象类实现接口的好处。

  1. Why doesn't the abstract class ServiceBase<T> just define as is without the need to inherit the IService interface? 为什么抽象类ServiceBase<T>不能按原样定义而无需继承IService接口? Is this doubling up the code? 这会使代码加倍吗?
  2. Why must the SomeBaseClass also implment the ICarService ? 为什么SomeBaseClass还必须实现ICarService Shouldn't the ServiceBase be sufficient? ServiceBase是否不够?

Ad 1: The additional abstract base class allows you to evolve the interface without breaking the implementation. 广告1:额外的抽象基类使您可以在不破坏实现的情况下改进接口。 Supposed there was no abstract base class, and you'd extend the interface, let's say by adding a new method. 假设没有抽象基类,那么您可以扩展接口,比如说添加一个新方法。 Then your implementation was broken, because your class does not implement the interface any longer. 然后您的实现被破坏了,因为您的类不再实现该接口。

Using an additional abstract base class you can separate this: If you add a new method to the interface, you can provide a virtual implementation in the base class and all your sub-classes can stay the same, and can be adopted to match the new interface at a later point in time. 使用附加的抽象基类,您可以将其分开:如果向接口添加新方法,则可以在基类中提供虚拟实现,并且所有子类都可以保持不变,并且可以被用来匹配新的类。界面在稍后的时间点。

Moreover, this combination allows you to define a contract (using the interface) and provide some default mechanisms (using the abstract base class). 此外,这种组合允许您定义合同(使用接口)并提供一些默认机制(使用抽象基类)。 Anyone who's fine with the defaults can inherit from the abstract base class. 任何使用默认值的人都可以从抽象基类继承。 Anyone who wants super-duper fine control about any little detail can implement the interface manually. 任何想要对任何小细节都进行超级双面胶精细控制的人都可以手动实现该界面。

Ad 2: From a technical point of view there is no NEED to implement the interface in the final class. 广告二:从技术角度来看,没有必要实施的最后类的接口。 But this, again, allows you to evolve things separately from each other. 但是,这再次允许您彼此独立地发展事物。 A CarService is for sure a Service<Car> , but maybe it's even more. CarService可以肯定是Service<Car> ,但可能还要更多。 Maybe only a CarService needs some additional stuff that should not go into the common interface nor into the service base class. 也许只有CarService需要一些其他的东西,这些东西不应该进入通用接口或服务基类。

I guess that's why ;-) 我想这就是为什么;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM