Why use interface programming?

Source: Internet
Author: User
Tags map class
Why use interface programming {reprint}
ProceedInterfaceOnly by introducing the history of the development of the language, can Mr. Lu Xun say that "the history of study Governance" and understand the past and present of the development of the language.Why?So many languagesWhy?Yes,Why?Will that happen?
After the emergence of computers, scientists have developed a variety of languages, from smalltalk, Pascal, Basic, C, C ++, java ,. net and so on, the pace of development of these languages can be seen as a historical development from process-oriented to object-oriented. When many object-oriented Books introduce their own history, they will introduce this history to readers and advocate how excellent OO programming is. The problem is that many people who have just started learning programs do not knowWhy?It is difficult for them to understand the virtual functions in the OO language,InterfaceAnd so on.
Before learning about this history, let's first introduce a concept ?? "Granularity", what is granularity? The author believes that the so-called granularity is actually the combined scale of code units in a program. We can give an example: gravel ?? Bricks ?? Building templates: we want to build a house. In fact, there are many construction methods. If you are not busy, you can use gravel to build a little bit or burn the gravel into bricks, bricks are used for building, and even doors, windows, and wall components of houses are directly purchased from the factory. These three different methods represent three different combination scales. Gravel is the smallest unit. It may be enough to build a small house, but there is no doubt that we must use a lot of "gravel", which is not easy to manage. Bricks aggregate a layer than gravel, it can be used to build a large house. The Housing template is of the highest size and can be used to quickly build a large-scale house. The differences and connections between these three scales are very similar to the programming concepts.
When learning Pascal in the early days, the teacher told us that the most basic unit of this process-oriented language is process and function, which are the smallest components in the program. Processes and functions can achieve the most basic code reuse. When we write code using some fixed functions and functions, we can call them in a program without having to write such a piece of code wherever necessary, which is obvious. In some small programs, the use of procedures and functions is appropriate, but in large and medium-sized programs, their drawbacks show that the granularity of processes and functions is too low, if one of our systems has 10000 functions and processes, our programmers will have to spend a lot of time searching for and maintaining them, the difficulty of managing 10000 unrelated functions and processes is obvious, just like that of 10000 enterprises. If there are no departments or positions, isn't this messy ?!
The emergence of object-oriented language is to solve this problem. Don't listen to the chaos of OO language. In fact, it is just for a reason ?? Improve programming granularity. The basic unit of object-oriented language is CLASS, which encapsulates many data members and member functions. The process increases the minimum component level, what we need to directly operate is not the process and function, but the classes at a higher level. We have divided 10000 people into many departments, and different departments are responsible for different matters, so that the company can finally get on the right track.
Is it all right if I have made a CLASS? Not necessarily, new problems may follow. Maybe we have many people in one department and can do many things. How can we achieve better management within the Department? For example, we have a class that provides many methods and attributes. These methods and attributes can be divided into a bunch of functions to serve different functions. However, our class does not manage these methods. In AO, map objects have many functions, such as Layer Management, element management, selection set management, and map display. Each function has many methods and attributes, these attributes and methods are disorganized, and there is no difference between them in a class. When our programmers need to find a method, they have to look for it one by one, which is inconvenient.
At this time,InterfaceInterface appears, C ++ inventor first proposed pure virtual function (in factInterface) The concept has been resisted and many people do not understand it.InterfaceWhat is the significance of this function? Why is there an empty shelf for nothing? It is a class, and it cannot produce an object; it is an object, and there is no method body to use.InterfaceA good thing is to classify the class. For map objects, we can do severalInterface, TheseInterfaceDefine methods, functions, and attributes for different functions, and map class to implement theseInterfaceIn this way, we can useInterfaceDefinition, implementation object. ThereforeInterfaceIs the definition of a series of related methods and attribute sets.
Dim pGraphicsContainer as iGraphicsContainer
PGraphicsContainer=application.doc ument. ActiveView. focusMap
PGraphicsContainer can only use the attributes and methods defined by pGraphicsContainer, rather than using management elements.InterfaceHow do we use other functions for defined methods and attributes? This is the so-called QI (Query Interface) function. FromInterfaceQuery anotherInterface.
Dim pGeoFeatureLayer as iGeofeatureLayer
PGeoFeatureLayer = pGraphicsContainer QI
Well, we have gone into the concept of COM after so many times. Before officially introducing COM, we have to come to the last point: the history of computer language development, in fact, it is a history that constantly improves the granularity of components and constantly improves the history of code reuse. Previously we used procedures and functions, and then we used classes. Now we useInterfaceFor the same purpose, let us find a balance between the specific and abstract components we operate on. Too specific, such as procedures and functions, there will be no framework; too abstract, such as classes, there will be no separate.

Sample Code:
Public interface IForm
{
Void Show ();
Void ShowDialog ();
}
Public class A: IForm
{
Public void Show ()
{
}
Public void ShowDialog ()
{
}
}
Public class B: IForm
{
Public void Show ()
{
}
Public void ShowDialog ()
{
}
}

Public class FormFactory
{
Public static IForm CreateInstance (string parm)
{

If (parm = "")
{
Return new ();
Else if (parm = "B ")
Return new B ();
}
Return null;
}
}
This is a logical Abstraction
This is a specific method
This is the philosophy of programming.

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.