The idea of interface-oriented programming

Source: Internet
Author: User

I think, for everyone using object-oriented programming language programmers, the term " interface " is certainly not unfamiliar, but I do not know whether you have such a doubt: what is the use of interfaces? What is the difference between it and an abstract class? Can you use abstract classes instead of interfaces? And, as programmers, you must always hear " Interface-oriented programming "the phrase, then what does it mean? What are the thoughts? 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 of all, interface programming and object-oriented programming is not a peer, it is not more advanced than object-oriented programming an independent programming idea, but attached to the object-oriented ideology, belong to its part. Or, it is one of the core ideas of object-oriented programming system.

2. The nature of the interface

interface, on the surface, is 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 be in form as follows:

  

The following is a reference fragment:
Interface InterfaceName
{
void Method1 ();
void Method2 (int para1);
void Method3 (String para2,string para3);
}
So what is the nature of the interface, or what is the meaning of the interface? I think the following two perspectives can be considered:

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 ... The idea that you must be able to ... "

For example, in nature, people can eat, that is, "If you are human, you must be able to eat." So when you simulate a computer program, there should be a iperson (in a Habit, interface name by "I", 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 also see some object-oriented thinking things. One of the core of object-oriented thinking is to simulate the real world, to abstract things in the real world into classes, the whole program relies on each class instance to communicate with each other and complete the system function, which accords with the real world's operation condition and the essence of object-oriented thought.

2 interface is an abstract representation of similar things in a certain granularity view. Notice here I emphasize that in a certain granularity view, because the concept of "similar things" is relative, it is different because of the granularity view.

For example, in my eyes, I am a person, and a pig is essential difference, I can accept my classmates and I are similar to this argument, but I can not accept that I and a pig is the same. But if, in the eyes of a zoologist, I and the pig should be of the same kind, because we are all animals, he can think that "man" and "pig" have realized the interface of IAnimal, while he is studying animal behavior, will not treat me and the pig separately, and will be from the "animal" This larger particle size study, But he would think that I was fundamentally different from a tree.

Now it's a geneticist, the situation is different, because biology can inherit, so in his eyes, I not only and pig no difference, and a mosquito, a bacterium, a tree, a mushroom and even a SARS virus is no different, because he would think we have achieved idescendable this interface ( Note: Descend VI. Heredity), that we are all heritable things, that he does not study us separately, and that all creatures are studied as one, and that in his eyes there are no people or viruses, only inheritable substances and not inheritable substances. But at least I have a difference with a stone.

But the unfortunate thing happened, one day, the Earth appeared a great man, his name is Lenin, he is familiar with Marx, Engels thought of the dialectical materialism, quite a lot of experience, so he made a famous definition: the so-called material, is the consciousness of the objective reality. At this point, I and a stone, a little air, an idiom and transmission cell phone signal electromagnetic field has no difference, because in Lenin's eyes, we can be reflected by the consciousness of the objective reality. If Lenin were a programmer, he would say, "a substance is an instance of all classes that implement both" Ireflectabe "and" Iesse "two interfaces. (Note: Reflect v. reflect esse N. Objective reality)

You might think that my example above is nonsense, but that's what the interface is all about. The object-oriented thought and one of the core is called polymorphism, what is polymorphism? To put it bluntly is to treat the same thing without discrimination on the level of a granular view. And the reason to dare to do so is because there is an interface. Like the geneticist, he understands that all creatures have idescendable interfaces, and that as long as they are creatures, there must be descend (), so that he can unify the study without studying each creature and eventually die.

It may not give you a visual impression of the nature and function of the interface. Then in the following examples and in the analysis of several design patterns, you will be more intuitive to experience the meaning of the interface.

3. Interface-Oriented Programming overview

Through the above, I think we have an understanding of the interface and the ideological content of the interface, then what is interface-oriented programming? My personal definition is: in system analysis and architecture, distinguish between hierarchy and dependencies, each level is not directly to its top service (that is, not directly instantiated in the upper level), but by defining a set of interfaces, Exposing its interface functionality only to the upper layer, the upper layer is only an interface dependency on the lower layer, not a specific class.

The benefits of doing so are obvious, and first of all, it is good for system flexibility. When the lower level needs to change, as long as the interface and interface function unchanged, the upper layer does not make any changes. You can even replace the entire lower layer without changing the upper layer of code. Just like we replace a WD 60G hard disk with a Seagate 160G hard drive, the other parts of the computer do not have to make any changes, but the original hard drive, the new hard drive plug on the line, because the rest of the computer is not dependent on the specific hard disk, Instead of relying on only one IDE interface, as long as the hard drive implements this interface, it can be replaced. 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 a likeness!

Another advantage of using the interface is that different parts or levels of developers can work in parallel, just like the hard disk to build the CPU, and do not have to build a monitor, as long as the interface is consistent, reasonable design, can be developed in parallel, and thus improve efficiency.

This article first came here. Finally, I would like to say a word: the essence of object-oriented is to simulate reality, which can be said to be the soul of my article. Therefore, more from the reality of the object-oriented things, to improve the system analysis and design capacity of the spleen benefits.

In the next article, I'll use an example to illustrate the basic approach to interface programming.

And the third one, I will parse some of the classical design patterns in the interface-oriented programming ideas, and analysis. NET layered architecture in the interface-oriented thinking.

Add to this article:

Having read your reply carefully, I am very glad to discuss technical issues with you. Thanks for giving a certain friend, but also for the friends who made the comments and questions, which prompted me to think more deeply about something, hoping to make progress. Here I would like to add something to discuss some of the more focused answers.

1. About "interface" in "interface-oriented Programming" and "interface" two words in a specific object-oriented language

See a friend. The word "interface" in "interface-oriented programming" should be larger than the interface range in a simple programming language. After thinking, I felt very reasonable. What I'm writing here really doesn't make sense. I think that "interface" in object-oriented languages refers to a specific code structure, such as the interface defined by the interface keyword in C #. "Interface" in "interface-oriented programming" can be said to be a structural part that hides the concrete underlying class and implements polymorphism from the perspective of software architecture and from a more abstract level. In this sense, if you define an abstract class and the purpose is to implement polymorphism, then I think it is reasonable to refer to this abstract class as an "interface". But is it unreasonable to use abstract classes to achieve polymorphism? In the second discussion below.

In a nutshell, I think the two "interface" concepts are different and interconnected. The interface in "interface-oriented programming" is a thought-level architecture component for implementing polymorphism, improving software flexibility and maintainability, while the "interface" in a specific language is the means by which parts of this thought are specifically implemented into the code.

2. About abstract classes and interfaces

This is a more heated question to see in the reply. I'm sorry I didn't think about the problem in the article. My personal understanding of this problem is as follows:

If you look at the specific code, it is easy to blur the two concepts, and even feel that the interface is redundant, because from the specific function, in addition to multiple inheritance (C#,java), the abstract class seems to completely replace the interface. But does the interface exist to implement multiple inheritance? Of course not. I think that the difference between an abstract class and an interface 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 are hesitant to use an interface or abstract class for a particular place, think about what your motives are.

See a friend of IPerson this interface query, my personal understanding is that IPerson this interface should not be defined, the key to see how the specific application of the situation. If we have women and man in our project, and we all inherit person, and most of women and man are the same, there is only one way DOSOMETHINGINWC () different (the example is vulgar, you forgive), Then of course it's reasonable to define a Abstractperson abstract class, because it can include all the other methods, and subclasses only define DOSOMETHINGINWC (), greatly reducing the amount of duplicate code.

However, if the women and the man two classes in our program basically do not have common code, and there is a personhandle class that needs to instantiate them, and do not want to know that they are male or female, but simply treat them as human beings and implement polymorphism, then it is necessary to define them as interfaces.

All in all, the difference between an interface and an abstract class lies primarily in the motivation to use, not in itself. And a thing that is defined as an abstract class or an interface depends on the context of the specific environment.

Furthermore, I think the other difference between an interface and an abstract class is that an abstract class and its subclasses should be a general and special relationship, whereas an interface is just a set of rules that its subclasses should implement. (Of course, sometimes there may be a general and special relationship, but we do not use the purpose of the interface here) such as the definition of transport vehicles, cars, airplanes, ships defined as subclasses, 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 compare, which is a rule. If the car class implements IComparable, just say, there is a method in our car to compare the two car instances. It may be more expensive than a car, and it may be bigger than that, but it doesn't matter, but we can't say that "cars are a special kind of comparison", which doesn't make sense in grammar.

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.