Good Case For Interfaces

前端 未结 17 1975
眼角桃花
眼角桃花 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).

    0 讨论(0)
  • If your shop is performing automated testing, interfaces are a great boon to dependency injection and being able to test a unit of software in isolation.

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

    An interface declares a contract that any object implementing it will adhere to. This makes ensuring quality in code so much easier than trying to enforce written (not code) or verbal structure, the moment a class is decorated with the interface reference the requirements/contract is clear and the code won't compile till you've implemented that interface completely and type-safe.

    There are many other great reasons for using Interfaces (listed here) but probably don't resonate with management quite as well as a good, old-fashioned 'quality' statement ;)

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

    Please forgive me for the pseudo code in advance!

    Read up on SOLID principles. There are a few reasons in the SOLID principles for using Interfaces. Interfaces allow you to decouple your dependancies on implementation. You can take this a step further by using a tool like StructureMap to really make the coupling melt away.

    Where you might be used to

    Widget widget1 = new Widget;
    

    This specifically says that you want to create a new instance of Widget. However if you do this inside of a method of another object you are now saying that the other object is directly dependent on the use of Widget. So we could then say something like

    public class AnotherObject
    {
        public void SomeMethod(Widget widget1)
        {
            //..do something with widget1
        }
    }
    

    We are still tied to the use of Widget here. But at least this is more testable in that we can inject the implementation of Widget into SomeMethod. Now if we were to use an Interface instead we could further decouple things.

    public class AnotherObject
    {
        public void SomeMethod(IWidget widget1)
        {
            //..do something with widget1
        }
    }
    

    Notice that we are now not requiring a specific implementation of Widget but instead we are asking for anything that conforms to IWidget interface. This means that anything could be injected which means that in the day to day use of the code we could inject an actual implementation of Widget. But this also means that when we want to test this code we could inject a fake/mock/stub (depending on your understanding of these terms) and test our code.

    But how can we take this further. With the use of StructureMap we can decouple this code even more. With the last code example our calling code my look something like this

    public class AnotherObject
    {
        public void SomeMethod(IWidget widget1)
        {
            //..do something with widget1
        }
    }
    
    public class CallingObject
    {
        public void AnotherMethod()
        {
            IWidget widget1 = new Widget();
            new AnotherObject().SomeMethod(widget1);
        }
    }
    

    As you can see in the above code we removed the dependency in the SomeMethod by passing in an object that conforms to IWidget. But in the CallingObject().AnotherMethod we still have the dependency. We can use StructureMap to remove this dependency too!

    [PluginFamily("Default")]
    public interface IAnotherObject
    {
        ...
    }
    
    [PluginFamily("Default")]
    public interface ICallingObject
    {
        ...
    }
    
    [Pluggable("Default")]
    public class AnotherObject : IAnotherObject
    {
        private IWidget _widget;
        public AnotherObject(IWidget widget)
        {
            _widget = widget;
        }
    
        public void SomeMethod()
        {
            //..do something with _widget
        }
    }
    
    [Pluggable("Default")]
    public class CallingObject : ICallingObject
    {
        public void AnotherMethod()
        {
            ObjectFactory.GetInstance<IAnotherObject>().SomeMethod();
        }
    }
    

    Notice that no where in the above code are we instantiating an actual implementation of AnotherObject. Because everything is wired for StructurMap we can allow StructureMap to pass in the appropriate implementations depending on when and where the code is ran. Now the code is truely flexible in that we can specify via configuration or programatically in a test which implementation we want to use. This configuration can be done on the fly or as part of a build process, etc. But it doesn't have to be hard wired anywhere.

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

    I don't understand how its extra overhead.

    Interfaces provide flexibility, manageable code, and reusability. Coding to an interface you don't need to worry about the concreted implementation code or logic of the certain class you are using. You just expect a result. Many class have different implementation for the same feature thing (StreamWriter,StringWriter,XmlWriter)..you do not need to worry about how they implement the writing, you just need to call it.

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

    You've been given a set of guidelines that your bosses have thought appropriate for your workplace and problem domain. So to be persuasive about changing those guidelines, it's not about proving that interfaces are a good thing in general, it's about proving that you need them in your workplace.

    How do you prove that you need interfaces in the code you write in your workplace? By finding a place in your actual codebase (not in some code from somebody else's product, and certainly not in some toy example about Duck implementing the makeNoise method in IAnimal) where an interface-based solution is better than an inheritance-based solution. Show your bosses the problem you're facing, and ask whether it makes sense to modify the guidelines to accommodate situations like that. It's a teachable moment where everyone is looking at the same facts instead of hitting each other over the head with generalities and speculations.

    The guideline seems to be driven by a concern about avoiding overengineering and premature generalisation. So if you make an argument along the lines of we should have an interface here just in case in future we have to..., it's well-intentioned, but for your bosses it sets off the same over-engineering alarm bells that motivated the guideline in the first place.

    Wait until there's a good objective case for it, that goes both for the programming techniques you use in production code and for the things you start arguments with your managers about.

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