Good Case For Interfaces

前端 未结 17 2045
眼角桃花
眼角桃花 2020-12-14 04:29

I work at a company where some require justification for the use of an Interface in our code (Visual Studio C# 3.5).

I would like to ask for an Iron Clad reasoning t

17条回答
  •  被撕碎了的回忆
    2020-12-14 05:00

    Interfaces allow you to declare a concept that can be shared amongst many types (IEnumerable) while allowing each of those types to have its own inheritance hierarchy.

    In this case, what we're saying is "this thing can be enumerated, but that is not its single defining characteristic".

    Interfaces allow you to make the minimum amount of decisions necessary when defining the capabilities of the implementer. When you create a class instead of an interface, you have already declared that your concept is class-only and not usable for structs. You also make other decisions when declaring members in a class, such as visibility and virtuality.

    For example, you can make an abstract class with all public abstract members, and that is pretty close to an interface, but you have declared that concept as overridable in all child classes, whereas you wouldn't have to have made that decision if you used an interface.

    They also make unit testing easier, but I don't believe that is a strong argument, since you can build a system without unit tests (not recommended).

提交回复
热议问题