I think, for the programmers who use object-oriented programming languages, " 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 ();
void Method2 (int para1);
void Method3 (string para2,string 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 ... " 's philosophy.
For example, in nature, people can eat, that is " If you are human, you must be able to eat " iperson (in custom, the interface name is determined by the "I" start) interface, and there is a method called eat () " person " iperson interface, which simulates the nature of " 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 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 has an essential difference, I can accept that my classmates and I are similar, but I must not accept that I and a pig are similar. But if, in the eyes of a zoologist, I and the pig should be the same, because we are all animals, he can think of " people " " pig " ianimal This interface, while studying animal behavior, will not treat me and the pig separately, but from the " animal " This larger granularity is studied, but he will think that I have an essential difference from a tree.
Now for a geneticist, the situation is different because the organism can inherit, so in his eyes, Not only did I not differentiate myself from pigs, but with a mosquito, a bacterium, a tree, a mushroom and even a sars The virus makes no difference, because he thinks we've all implemented the id escendable This interface (Note: descend vi. heredity), that is, we are all heritable things, he will not study us, but will all creatures as the same kind of research, 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 there is a great man, his name is Lenin, he is familiar with Marx, Engels ' thought of dialectical materialism, after a great 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 material, that is, all simultaneously implements the "Ireflectabe" "Iesse" reflect v. reflects esse N. objective)
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 realized the idescendable interface, so as long as the creature, there must be descend() This method, so he can be unified research, Rather than study each creature separately 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 interface and the concept of interface, then what is interface-oriented programming? My personal definition is: in the system analysis and architecture, distinguish between hierarchy and dependency, each level is not directly to its upper layer to provide services (that is, not directly instantiated in the upper layer), but by defining a set of interfaces, only to the upper layer exposes its interface function, the upper layer for the lower layer is only interface dependence, Rather than relying on specific classes .
the benefits of doing so are obvious, and first of all, a great benefit to system flexibility. When the lower layer needs to change, as long as the interface and interface functions are not changed, the upper layer does not have to make any changes. You can even replace the entire lower layer without altering the upper code, as if we were replacing a WD 60G hard drive with a Seagate 160G hard Drive, Other parts of the computer do not have to make any changes, but the original hard drive to unplug, the new hard disk plug on the line, because the rest of the computer is not dependent on the specific hard disk, and only rely on an IDE interface, as long as the hard disk implementation of this interface, you can replace it. From this point of view, the interface in the program is very similar to the interface in reality, so I always think that the word interface (interface) is really in the likeness!
Another advantage of using interfaces is that developers of different parts or levels can work in parallel, like creating a hard drive CPU , do not wait for the display, as long as the interface is consistent, reasonable design, can be developed in parallel, thereby improving efficiency.
This article first came 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'll use an example to illustrate the basic methods of interface programming.
in the third chapter, I will analyze some of the interface-oriented programming ideas in the classic design pattern and analyze . NET interface-oriented thinking in layered architectures.
1. " interfaces " in "interface-oriented programming" " interface " two words in a specific object-oriented language
see a friend put forward"interface-Oriented programming"in the"Interface"the word should be more than the simple programming languageInterfaceLarger range. I think it makes sense. I did not write that very well here. I think that in the object-oriented language"Interface"refers to a specific type of code structure, such asC #in useInterfacethe interface defined by the keyword. and"interface-Oriented programming"in the"Interface"It can be said that from the point of view of software architecture, from a more abstract level refers to the kind of structural parts used to hide specific underlying classes and achieve polymorphism. In this sense, if an abstract class is defined and the purpose is to achieve polymorphism, then I think that this abstract class is also called"Interface"is reasonable. But is it unreasonable to use abstract class to realize polymorphism? Discussed in the second article below.
as a general rule, I think two " Interface " are not only different from each other, but also related to each other. The interface in interface-oriented programming is a thought-level architectural component for polymorphism, software flexibility, and maintainability, while the language-specific " Interface " Is the means by which the parts of the idea are implemented into code.
2. about abstract classes and interfaces
See in the reply this is a more intense discussion of a problem. I'm sorry I didn't consider this issue in the article. My personal understanding of this problem is as follows:
if a single from the specific code, the two concepts are easy to blur, and even feel that the interface is redundant, because single from the specific function, in addition to multiple inheritance ( C # , Java , abstract classes seem to completely replace interfaces. 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.
see a friend ofIPersonThe questioning of this interface, my personal understanding is thatIPersonThis interface should not be defined, the key to see the specific application is how a situation. If we have a project that hasWomenand theMans, both inherit Person, andWomenand theMansMost methods are the same, there is only one wayDOSOMETHINGINWC() Different (examples are vulgar, you forgive me), then of course define aAbstractpersonThe abstract class is reasonable because it can include all other methods, and the subclass defines onlyDOSOMETHINGINWC(), greatly reducing the amount of repetitive code.
However, if our program Women and the Mans Two classes basically have no common code, and there is a Personhandle classes need to instantiate them and do not want to know that they are male or female, and simply treat them as human beings and achieve polymorphism, then it is necessary to define interfaces.
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 exampleIcomparableinterface, it simply says that the class that implements this interface must be able to be compared, which is a rule. IfCarThis class implements theIcomparable, just to say that ourCarthere is a method that can be used for twoCarexample, it may be more expensive than which car, it may be bigger than which car, it doesn't matter, but we can't say"cars are a special kind of comparison", it doesn't work in grammar.
Java interface-oriented programming