Interfaces as parameters

In what scenarios will someone pass (or receive) the interface as a parameter? Is this a really useful thing or just a fancy way to do something?

+4
source share
6 answers

This is an extremely useful thing.

Take any of the LINQ extension methods , for example. They don't care what is passed to them while it implements IEnumerable<T> . The idea is that they can all be applied to anything you can list using the foreach .

Imagine how pointlessly restrictive it would be if they all required you to pass arrays of T[] or List<T> , for example.


Here is just one very trivial illustration. Suppose LINQ extensions do not exist (which is actually a real possibility if I use .NET 2.0), and I want to write a Sum method.

I could write it like this:

 public static double Sum(List<double> values) { double sum = 0.0; foreach (double value in values) { sum += value; } return sum; } 

This is good and good, but pay attention to something here: I wrote a method to take a List<double> , which is a class with much more functionality than this code depends on . Where does he use Insert ? Where does he use RemoveAt ? FindAll ? Sort ? No, all this is not required. So what is really needed for this method to go through List<double> ?

Also, let's say I have double[] . Theoretically, I would have to use this parameter as the values parameter, since everything I do lists it with foreach ; but since I typed values like List<double> to pass the double[] method to my Sum , I would have to do this:

 double sum = Sum(new List<double>(myArray)); 

This is just a completely unnecessary new object that I built, just to invoke the code that really had to process my original object.

By writing methods that take interfaces as parameters, you make your code more flexible and more powerful, and you avoid imposing inappropriate restrictions (give me X, although I could just as easily do this with Y) when invoking the code.

+18
source

The easiest way to remember is all about programming for an interface, not about implementation. Say, for example, I have a method where I want to do something, for example.

 public void MakeNoise(IAnimal animal) { animal.MakeNoise(); } 

I don’t care what a particular implementation is, I just know that everything that is passed, I can call MakeNoise (). I am programming an interface, not an implementation.

 public class Dog : IAnimal { public void MakeNoise() { Console.WriteLine("Woof"); } } public class Cat : IAnimal { public void MakeNoise() { Console.WriteLine("Meow"); } } 

Interface programming is a major aspect of OOP; you will find them incredibly useful.

+5
source

Whenever you need abstraction.

One good example is the IEnumerable<T> and IQueryable<T> interfaces in the .NET Framework. They allow you to write extension methods that can be used against List<T >, Dictionary<TKey, TValue> or even T[] .

For example, you can take dependency injection. ASP.NET MVC typically uses a repository to access data:

 public class MyClassRepository { public MyClass GetById(int id) { // Some Implementation } } public class MyController { private MyClassRepository _repo; public class MyController() : base(new MyClassRepository()) { } public class MyController(MyClassRepository repo) { _repo = repo; } } 

Now, if you want to remake this repository for unit testing ... you are deceived. There is no easy way. Interfaces included!

 public interface IMyClassRepository { public MyClass GetById(int id); } public class MyClassRepository : IMyClassRepository { public MyClass GetById(int id) { // Some Implementation } } public class MyController { private IMyClassRepository _repo; public class MyController() : base(new MyClassRepository()) { } public class MyController(IMyClassRepository repo) { _repo = repo; } } 

Now, when you enter the interface, we can laugh at IMyClassRepository, but we consider it necessary for testing. Usually this includes a simple mock-up of the object with a specific behavior for reliable product results.

+2
source

Interfaces are very useful.

They help with code decoupling - for example, if you use the IList interface and pass it into your method and use it in your method, you can pass any collection that implements this interface, whether it is in BCL or not.

+1
source

One area where interfaces would make sense is to use a design pattern template .

For example, the observer template or proxy template, the visitor template, and others. You could probably opt out of using the interface, but I would suggest that you quickly decide that the code becomes much cleaner (as in a more modular, separated for reasons) than non-related code. It just helps promote the best code in these situations.

+1
source

Polymorphism!

They help you write code that does not discriminate against specific classes just because they do not inherit from a special base class. Your functions will be performers with equal opportunities.

0
source

Source: https://habr.com/ru/post/1314555/


All Articles