Good Case For Interfaces

前端 未结 17 1971
眼角桃花
眼角桃花 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:18

    Code decoupling. By programming to interfaces you decouple the code using the interface from the code implementing the interface. This allows you to change the implementation without having to refactor all of the code using it. This works in conjunction with inheritance/polymorphism, allowing you to use any of a number of possible implementations interchangeably.

    Mocking and unit testing. Mocking frameworks are most easily used when the methods are virtual, which you get by default with interfaces. This is actually the biggest reason why I create interfaces.

    Defining behavior that may apply to many different classes that allows them to be used interchangeably, even when there isn't a relationship (other than the defined behavior) between the classes. For example, a Horse and a Bicycle class may both have a Ride method. You can define an interface IRideable that defines the Ride behavior and any class that uses this behavior can use either a Horse or Bicycle object without forcing an unnatural inheritance between them.

    0 讨论(0)
  • 2020-12-14 05:20
    • You can implement multiple interfaces. You cannot inherit from multiple classes.
    • ..that's it. The points others are making about code decoupling and test-driven development don't get to the crux of the matter because you can do those things with abstract classes too.
    0 讨论(0)
  • 2020-12-14 05:22

    The argument against them is thus: If a class is properly setup (with its public and private members) then an interface is just extra overhead because those that use the class are restricted to public members. If you need to have an interface that is implemented by more than 1 class then just setup inheritance/polymorphism.

    Consider the following code:

    interface ICrushable
    {
      void Crush();
    }
    
    public class Vehicle
    {
    }
    
    public class Animal
    {
    }
    
    public class Car : Vehicle, ICrushable
    {
      public void Crush()
      {
         Console.WriteLine( "Crrrrrassssh" );
      }
    }
    
    public class Gorilla : Animal, ICrushable
    {
      public void Crush()
      {
         Console.WriteLine( "Sqqqquuuuish" );
      }
    }
    

    Does it make any sense at all to establish a class hierarchy that relates Animals to Vehicles even though both can be crushed by my giant crushing machine? No.

    0 讨论(0)
  • 2020-12-14 05:22

    Well, my 1st reaction is that if you've to explain why you need interfaces, it's a uphill battle anyways :)

    that being said, other than all the reasons mentioned above, interfaces are the only way for loosely coupled programming, n-tier architectures where you need to update/replace components on the fly etc. - in personal experience however that was too esoteric a concept for the head of architecture team with the result that we lived in dll hell - in the .net world no-less !

    0 讨论(0)
  • 2020-12-14 05:26

    Appologies as this doesn't answer your question regarding a case for Interfaces.

    However I suggest getting the person in question to read..

    Head First Design Patterns

    -- Lee

    0 讨论(0)
提交回复
热议问题