R
Now you probably don't have to, You'll face it someday later when you're ready. Don't get too excited and go on, very often the programmers have to skip and get back to the topics. The point is, you know there's a thing like the interface, and it's a way to hide real class performance.Through the interfaces, you:do not obliterate the logic of your application with specific classes and can easily change in certain classes without touching others, add functionality or replace one class completely different without touching all the annex. That is, abstracts are being implemented;You can design the application logic on the interfaces, leaving the application for later and using the plugs;Apply so-called multiple inheritance; If you're the boss, you can easily get your thoughts to the subordinates.I'll give a few examples where the interface is used, but I'll say, they're not best representatives. I'm learning too. ) Design of a zooLet's design a small zoo program, and the zoo is tanning.1. First of all, he's got to be inhabited by animals who can walk around him.To describe the life cycle of the zoo, I use ZooLivecycle: class ZooLivecycle
{
public void allPetWalk()
{
}
}
So, we have animals who can walk, let's determine their possibilities through the interfaces and make it work.interface IWalk
{
void walk();
}
class Cat : IWalk
{
public void walk()
{
Console.WriteLine("cat walk");
}
}
class Dog : IWalk
{
public void walk()
{
Console.WriteLine("dog walk");
}
}
class Fish : IWalk
{
public void walk()
{
Console.WriteLine("fish cant walk");
}
}
class Bird : IWalk
{
public void walk()
{
Console.WriteLine("bird can fly");
}
}
For all animals to go, it's easier to transfer them to the zoo class with all the mass and go through the cycle.class ZooLivecycle
{
public void allPetWalk(IWalk[] arr)
{
for (int i = 0; i < arr.Count(); i++)
arr[i].walk();
}
}
So now we can call for our realization.class Program
{
static void Main(string[] args)
{
ZooLivecycle zoo = new ZooLivecycle();
IWalk[] walks = new IWalk[]{new Cat(), new Dog(), new Fish(), new Bird()};
Console.WriteLine("\n*Pet Walk*");
zoo.allPetWalk(walks);
Console.Read();
}
}
Now try to do this without the interfaces, mainly the function of allPetWalk, although there's a way that's a common parent. It is possible to try to refer the reference as public void allPetWalk(Object[] arr) And then bring the type, but it's very bad practice, because you lose control of the type and have to add checks, which is not the best solution. 2. Let's move on, we decided to add a surveillance system, that's a camera that can transmit information.interface IObservation
{
void analyze();
}
class Videocamera : IObservation
{
public void analyze()
{
Console.WriteLine("In zoo all ok");
}
}
We're getting information in the zoo class:class ZooLivecycle
{
public void allPetWalk(IWalk[] arr)
{
for (int i = 0; i < arr.Count(); i++)
arr[i].walk();
}
public void zooAnalyze(IObservation[] arr)
{
for (int i = 0; i < arr.Count(); i++)
arr[i].analyze();
}
}
That's a challenge.class Program
{
static void Main(string[] args)
{
ZooLivecycle zoo = new ZooLivecycle();
IWalk[] walks = new IWalk[]{new Cat(), new Dog(), new Fish(), new Bird()};
IObservation[] analyze = new IObservation[] { new Videocamera() };
Console.WriteLine("\n*Pet Walk*");
zoo.allPetWalk(walks);
Console.WriteLine("\n*Analyze zoo*");
zoo.zooAnalyze(analyze);
Console.Read();
}
}
3. Now let's put a pig on if someone still used a parent's class instead of an interface.We're running a posser who can both go to the zoo and transmit information. There is no cross-inheritance in the C# language, but some interfaces can be inherited instead.class HelperPet : IWalk, IObservation
{
public void walk()
{
Console.WriteLine("helppet walk");
}
public void analyze()
{
Console.WriteLine("helppet analyze");
}
}
That's all I had to add. We can keep using our zoo without texting.class Program
{
static void Main(string[] args)
{
ZooLivecycle zoo = new ZooLivecycle();
IWalk[] walks = new IWalk[]{new Cat(), new Dog(), new Fish(), new Bird(), new HelperPet()};
IObservation[] analyze = new IObservation[] { new Videocamera(), new HelperPet() };
Console.WriteLine("\n*Pet Walk*");
zoo.allPetWalk(walks);
Console.WriteLine("\n*Analyze zoo*");
zoo.zooAnalyze(analyze);
Console.Read();
}
}
Now let us look at real examples that are very often used. Very often, interfaces are used as descriptive models from which they are created. So you're not tied to specific classes. In a very C# language, there are even interfaces that do conduct. You'll face it very often if you move to WPF or ASP.NET MVC of the Annex.Use foreach with its own classesLet's improve our zoo and add an animal storage class. Although it's quite voluminous, I'm just https://msdn.microsoft.com/ru-ru/library/system.collections.ienumerable.aspx and he just made his point. public interface IWalk
{
void walk();
}
public class Animals : IEnumerable<IWalk>
{
private IWalk[] _pets;
public Animals(IWalk[] pArray)
{
_pets = new IWalk[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_pets[i] = pArray[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public PetsEnum GetEnumerator()
{
return new PetsEnum(_pets);
}
IEnumerator<IWalk> IEnumerable<IWalk>.GetEnumerator()
{
return (IEnumerator<IWalk>)GetEnumerator();
}
}
public class PetsEnum : IEnumerator
{
public IWalk[] _pets;
int position = -1;
public PetsEnum(IWalk[] list)
{
_pets = list;
}
public bool MoveNext()
{
position++;
return (position < _pets.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public IWalk Current
{
get
{
try
{
return _pets[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
Теперь я могу делать так:
class Program
{
static void Main(string[] args)
{
ZooLivecycle zoo = new ZooLivecycle();
Animals animals = new Animals(new IWalk[]{new Cat(), new Dog(), new Fish(), new Bird(), new HelperPet()});
Console.WriteLine("\n*Pet Walk*");
zoo.allPetWalk(animals);
Console.Read();
}
}
class ZooLivecycle
{
public void allPetWalk(Animals animals)
{
foreach(IWalk animal in animals)
animal.walk();
}
}
Without using the interface, you won't be able to use your class foreach, although it's actually done very quickly. At the same time, the method foreach works with any objects, and if not interfaces, it would be more difficult to achieve that effect. Methods such as Add, Remove, etc. can now be added to Animals.And in the end, you can put the plugs and pass the test data.If the classes are complex enough, for example, one of them gets the data from the base, and the other handles the data, then you can write the wrong class. I mean, instead of real data, you're transferring a class that doesn't use an OBD connection, and then you can rewrite it. You can work normally with both one and the other grades differently.