I think, for you to use the object-oriented programming language programmer, "interface" this noun must not be unfamiliar, but I do not know if you have such doubts: what is the purpose of the interface? What is the difference between it and an abstract class? Can you use abstract classes instead of interfaces? And, as programmers, you must often hear the phrase "interface-oriented programming," What does it mean? What is the connotation of the mind? What is the relationship with object-oriented programming? This article will answer these questions.
1. What is the relationship between interface-oriented programming and object-oriented programming?
First, interface-oriented programming and object-oriented programming are not lateral, it is not an independent programming idea that is more advanced than object-oriented programming, but is attached to the object-oriented ideology, which belongs to its part. Or, it is one of the essence of thought in object-oriented programming system.
2. The nature of the interface
An interface, on the surface, consists of a collection of several method definitions without the principal code, with a unique name that can be implemented by a class or other interface (or, optionally, inherited). It may look like this in form:
interface InterfaceName { void Method1 (); < Span style= "color: #0000ff;" >void method2 (int para1); void method3 (string para2,string &NBSP;PARA3); }
So what is the nature of the interface? Or what the meaning of the interface exists. I think it can be considered from the following two points of view:
1) An interface is a set of rules that specify a set of rules that a class or interface that implements this interface must have. Embodies the nature "if you are ... You must be able to ... "the idea. For example, in nature, people can eat, that is, "If you are human, you must be able to eat". So the simulation to the computer program, there should be a IPerson (the custom interface name from the "I") interface, and there is a method called Eat (), and then we stipulate that each class that represents "human", must implement the IPerson interface, which simulates the nature "If you are human, you must be able to eat" This rule. From here, I think you can see a little bit of object-oriented thinking. One of the core of object-oriented thinking is to simulate the real world and abstract the real world into classes, and the whole program relies on each kind of instance to communicate with each other and to cooperate with each other to complete the system function, which is very accord with the real world running condition and the essence of object oriented thought.
2) The interface is an abstract representation of the same thing on a certain granularity view. Note here I emphasize that in a certain granularity view, because the concept of "homogeneous thing" is relative, it differs because of the granularity view.
For example, in my eyes, I am a person, and a pig there is an essential difference, I can accept my classmates and I are similar this statement, but can not accept me and a pig is the same. However, if in the eyes of a zoologist, I and pig should be the same, because we are animals, he can think that "man" and "pig" have achieved ianimal this interface, and he in the study of animal behavior, I and pig will not be treated separately, but from the "animal" This larger grain size study, But he would think I was fundamentally different from a tree. now a geneticist, the situation is different, because the organism can inherit, so in his eyes, I not only with the pig no difference, and a mosquito, a bacterium, a tree, A mushroom or even a SARS virus is no different, because he would think that we have achieved idescendable this interface (note:descend vi. heredity), that we are all heritable things, he will not study us separately, And all living things will be studied in the same kind, in his eyes there is no human and virus, only the genetic material and non-hereditary material. But at least, I have a difference with a stone.
But the unfortunate thing happened, one day, on the earth appeared a great man, his name is Lenin, he is familiar with Marx, Engels of dialectical materialism thought after the masterpiece, quite experience, so he made a famous definition: the so-called material, is to be reflected in the consciousness of the objective reality. At this point, I and a stone, a trace of air, an idiom and transmission of mobile phone signal electromagnetic field has no difference, because in Lenin's eyes, we are can be reflected in the consciousness of the objective reality. If Lenin was a programmer, he would say: the so-called matter is all the instances that are generated by all classes that simultaneously implement the "Ireflectabe" and "Iesse" two interfaces. (Note: Reflect v. reflect esse N. Objective reality)
You might think that my example above is a nonsense, but that's what the interface is all about. Object-oriented thinking and one of the core is called polymorphism, what is polymorphism? To put it bluntly is to treat the same things indiscriminately at a certain granularity view level. And the reason to do this is because there is an interface exists. Like the geneticist, he knew that all living creatures had idescendable interfaces, and that, as long as they were creatures, there must be descend (), so that he could study together, rather than study each creature and end up exhausted. Maybe I can't give you a visual impression of the nature and function of the interface. Then in the following example and in the analysis of several design patterns, you will be more intuitive to experience the connotation of the interface.
3. Overview of interface-oriented programming
through the above, I think we have an understanding of the concept of interface and interface, then what is interface-oriented programming? My personal definition is: This article first here. Finally, I would like to say one more word: the essence of object-oriented is the simulation of reality, this is also the soul of this article. Therefore, more from the reality of thinking about object-oriented things, to improve the system analysis and design ability has great spleen benefits.
In the next article, I will use an example to illustrate the basic methods of interface programming and the third, I will parse some of the interface-oriented programming ideas in classic design patterns and parse them. The interface-oriented idea in the net layered architecture.
Supplement to this article:
carefully read everyone's reply, very happy to discuss technical issues with you. Thanks to the affirmation of friends, but also to express their opinions and questioned friends, which prompted me to think more deeply about something, hoping to take this progress. Here I would like to add something to discuss some of the more concentrated questions in the replies. 1. About "interface" in "interface-oriented Programming" and "interface" in specific object-oriented languages two words see a friend. The word "interface" in "interface-oriented programming" should be larger than the interface in a simple programming language. I think it makes sense. I did not write that very well here. I think "interface" in an object-oriented language refers to a specific code structure, such as an interface defined in C # with the interface keyword. The "interface" in "interface-oriented programming" can be described as a kind of structural part that hides concrete underlying classes and implements polymorphism from the perspective of software architecture and from a more abstract level. In this sense, if an abstract class is defined and the purpose is to achieve polymorphism, then I think it is reasonable to refer to this abstract class as an "interface". But is it unreasonable to use abstract class to realize polymorphism? Discussed in the second article below.
< Span style= "color: #808080;" > as a general rule, I think that the concept of two "interfaces" is both different and interrelated. " 2. About abstract classes and Interfaces
< Span style= "color: #808080;" > See in the reply this is a more intense discussion of the problem. I'm sorry I didn't consider this issue in the article. My personal understanding of this problem is as follows: If you look at these two concepts from a specific code, it is easy to blur them and even feel that the interface is superfluous, because the single function, in addition to multiple inheritance (C #, java), the abstract class seems to completely replace the interface. However, does the presence of an interface exist to achieve multiple inheritance? Of course not. I think the difference between abstract classes and interfaces is the use of motivation. Abstract classes are used for reuse of code, and the motivation for using interfaces is to achieve polymorphism. So, if you're hesitant about using interfaces or abstract classes somewhere, think about what your motives are.
< Span style= "color: #808080;" >
All in all, the difference between an interface and an abstract class lies primarily in the motivation used, not in itself. And whether a thing should be defined as an abstract class or an interface depends on the context of the specific environment. Furthermore, I think another difference between an interface and an abstract class is that the abstract class and its subclasses should be a general and special relationship, and the interface is just a set of rules that its subclasses should implement. (Of course, sometimes there may be general and special relationship, but we use the purpose of the interface is not here) For example, transportation is defined as abstract class, cars, airplanes, ships defined as sub-categories, is acceptable, because cars, airplanes, ships are a special means of transport. Another example is the IComparable interface, which simply says that the class that implements this interface must be able to be compared, which is a rule. If the car class implements the IComparable, just saying that there is a way in our car to compare two car instances, which may be more expensive than which car, or bigger than which, it does not matter, but we can not say that "the car is a special can be compared", which is not in the grammar.
Java interface (basic idea one)