Two Functions of the interface

Source: Internet
Author: User
Two Functions of the interface

I. Binding

C # What is the function of the interface? First, let's take a look at the fact that C # interfaces, C # interfaces are something that makes C # beginners easy to confuse. They seem very simple to use and define interfaces that contain methods, but there is no specific method to implement the code, and then the code of all the methods to implement the interface in the class that inherits the interface, but when I didn't really realize the role of interfaces, I thought that using interfaces is an extra option. Of course, it is absolutely wrong if you think so. The employees invited by Bill Gates Microsoft are smarter than gates, can their C # Have such advantages ?! As for the functions of interfaces, one of them on the internet is actually a simple analysis that gives us a good understanding.

Example of C # interface function explanation:

 
 
  1. public interface IBark  
  2. {  
  3.     void Bark();  

Defines a class that inherits from IBark and must implement the Bark () method.

 
 
  1. Public ClassDog: IBark
  2. {
  3. PublicDog ()
  4. {}
  5. Public VoidBark ()
  6. {
  7. Consol. write ("Wang ");
  8. }
  9. }

Then, declare an instance of Dog and call the Bark () method.

 
 
  1. Dog wangcai =NewDog ();
  2. Wangcai. Bark ();

Just imagine, if you want to call the Bark () method, you just need to declare such a method in Dog (). What should you do with interfaces. because the interface does not have the specific implementation of Bark. the implementation must be in Dog. so isn't the interface used here all the more?

Some people say this: in terms of Interface Definition, interfaces are actually a protocol and a constraint between classes. take the above example as an example. all classes that inherit the IBark interface must implement the Bark () method. from the perspective of a user (a user using a class), if he knows that a class inherits from the IBark interface, he can call the Bark () method with confidence, instead of how the Bark () method is implemented. for example, we have another class.

 
 
  1. Public ClassCat: IBark
  2. {
  3. PublicCat ()
  4. {}
  5. Public VoidBark ()
  6. {
  7. Consol. write ("meow ");
  8. }
  9. }

When users use Cat or Dog classes and know that they inherit from IBark, they can directly call the Bark () method without the specific implementation in the class, because the two classes certainly have specific implementations of the Bark () method.

From the design point of view. several Classes need to be written in a project. Because these classes are complex and have a large workload, each class requires one staff member to write. for example, A programmer decides the Dog class, and B programmer writes the Cat class. these two classes are unrelated, but because users need to implement a method for "calling. this requires a constraint on them. they all inherit from the IBark interface to facilitate unified management. the other is convenient to call. of course, you can achieve the same purpose without using interfaces. in this case, such constraints are not so obvious. If such classes have Duck classes and so on, it is inevitable that some people will miss this method when there are many such classes. therefore, the interface is more reliable and more binding.

Ii. extensibility of Peptides
Assume that our company has two types of programmers: VB programmers, which refer to programmers who write programs using VB, and clsVBProgramer programmers. Delphi programmers refer to programmers who write programs using Delphi, it is represented by the clsDelphiProgramer class. Each class has a WriteCode () method. Definition:

Class clsVBProgramer ()
{
WriteCode ()
{
// Write code in VB;
}
}

Class clsDelphiProgramer ()
{
WriteCode ()
{
// Write the code in Delphi;
}
}

Now the company has a project that requires a programmer to write a program.

Class clsProject ()
{
WritePrograme (clsVBProgramer programer) // write code with VB
{
Programer. WriteCode ();
}
WritePrograme (clsDelphiProgramer programer) // reload method, use Delphi to write code
{
Programer. WriteCode ();
}
}
// In the main program, we can write as follows:
Main ()
{
ClsProject proj = new clsProject;
// Use VB to write code
ClsVBProgramer programer1 = new clsVBProgramer;
Proj. WritePrograme (programer1 );
// Use Delphi to write code
ClsDelphiProgramer programer2 = new clsDelphiProgramer;
Proj. WritePrograme (programer2 );
}

However, if the company has another C # programmer, how can we modify this program so that it can implement the function of writing a program using C? We need to add a new clsCSharpProgramer class, and re-load the WritePrograme (clsCSharpProgramer programer) method in this clsProject class. This is a lot of trouble. If there are still C programmers, C ++ programmers, and JAVA programmers. It's too much trouble!

However, if you use an interface, it will be totally different:
First, declare a programmer interface:

Interface IProgramer ()
{
WriteCode ();
}

Then declare two classes and implement the IProgramer interface:

Class clsVBProgramer (): IProgramer
{
WriteCode ()
{
// Write code in VB;
}
}

Class clsDelphiProgramer (): IProgramer
{
WriteCode ()
{
// Write the code in Delphi;
}
}
// Modify the clsProject class as follows:
Class clsProject ()
{
WritePrograme (IProgramer programer)
{
Programer. WriteCode (); // write code
}
}

Main ()
{
ClsProject proj = new clsProject;
IProgramer programer;
// Use VB to write code
Programer = new clsVBProgramer;
Proj. WritePrograme (programer );
// Use Delphi to write code
Programer = new clsDelphiProgramer;
Proj. WritePrograme (programer );
}

If programmers such as C #, C, C ++, and JAVA are added, we only need to add their related classes, and then add them in main () and then click OK. Great scalability! Add a CSharp programmer as follows:

// The clsProject class and IProgramer do not need to be changed. You only need to write a CSharp programmer class.
Class clsCSharpProgramer (): IProgramer // remember to implement the IProgramer Interface
{
WriteCode ()
{
// Write code in CSharp language;
}
}
// The usage is as follows:
Main ()
{
ClsProject proj = new clsProject;
IProgramer programer;
// Use CSharp to write code
Programer = new clsCSharpProgramer;
Proj. WritePrograme (programer );
}

In this way, if we encapsulate the clsProject class into a component, then when our users need to expand the function, we only need to make small modifications externally to implement it, it can be said that there is no need to modify the components we have already closed! Is it very convenient and powerful!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.