OO three steps
Note: The article ownership returns to Ni Shuo, welcome any form of reprint, but please indicate the original article author and the location: http://nishuo.35123.net. And welcome to visit MatrixCPP ------- -----------------------------------
OO three steps
Foreword: Object-Oriented Programming, hereinafter referred to as OOP) is a Simula language originated in the 1960s, and has developed approach to approximately 30 years. Its own theory has been perfect and is implemented by multiple object-oriented programming languages (OOPL below OOPL), following the ORITED CGRAMING LANGUAGE. If UNIX system is regarded as a cultural roots in system software, the SmallTalk language is undoubtedly the same position in the OOPL field and UNIX. Due to many reasons, most of the domestic programming staff did not have a deep OOP and OOPL theory. Many people have learned from the beginning to work for many years, and they are only touched by static types such as C / C , Java, VB, Delphi. Pure OOP ideas and smallTalk as an OOPL roots, and very little dynamic type language, do not know that there are still some programming languages that can be binded to variables. These comparisons are an important part of the theory of OO, and the information in China is not much. Therefore, write some of the OO learning experience as a series of articles (a total of three, some of the basic but easily misunderstanding of OOP, the second article mainly illustrates various OOPL evolution and development and OOP The theory support, the third part is mainly the status of the model and components in OOP and the future of OOP). Because the article describes only their own understanding of OOP / OOPL, it is inevitable, just hope The role of the brick introduction to everyone.
Analysis of OO's cornerstone
From abstract
Booch once sentenced the complexity in the software development process in his own OO field. And the most fundamental weapon for handling complexity is abstraction. The broad abstraction represents a simplified description or specifications of complex systems, in order to highlight the nature of the system, deliberately ignore the non-qualitative details. "A concept only determines this concept as an abstract concept when it can be independent, understand and analyze the mechanisms that are ultimately used. Booch also gives the narrow abstract definition in the OO area: "Abstraction indicates an object of the object and other objects, so it provides a clear definition of the conceptual boundary associated with the perspective." Depending on the perspective of different observation, we can give an abstract level of different levels for OOP. Typically, facing a typical object-oriented program, [budd 2002] divides it into five abstraction layers, covering the analysis, design and programming of OOP:
1, on the highest level abstraction, the program is considered a collection of objects composed of many interactions and obeying the contract. Objects cooperate with each other to complete the computing task. A typical representative of this abstract level is Design Pattern.
2, the second abstraction layer is an object set unit, which is an object between a group definition. In the programming language level, Java is Packages, C is Name Space. Typical representatives at this abstract level are modular ideas.
3. The third abstraction layer is representative of a typical OOP mode: a client / server model, which is mainly used to abstract the mutual exchange between two objects. A typical representative at this abstract level is the message mechanism between the objects (Message Passing). 4, the fourth abstraction layer is a set of similar objects to define a class as a template for generating objects. The class defines the external use interface of the object and the internal inheritance interface required to inherit the object, and the typical representative of this abstract level is interface programming. Interface programming.
5. The fifth abstraction layer is an implementation of the method and member variable required for a class. Here is the OOP final and POP (ProCedure-Oriented Programming).
Of course, we can divide a finer abstract level according to the respective observation angles, such as the POP theory of the fifth layer, and we can further divide the control abstraction (three fully squeezed chart forever calculation models need Control structure) and data abstraction (ADTS), etc., and continue (if your imagination is rich enough :).
What is OOP?
Many of OOP has come to Simula language and get more expansion in the wellness and standardization process of SmallTalk language and re-annotations for previous ideas. It can be said that OO thinking and OOPL is almost in synchronous development. Different from the actual computational model of the functional-programming and logic-programming (logic-programming), OOP has little to introduce precise mathematical description, but tends to establish An object model, it is approximately reflecting the relationship between the entities in the application, the essence is a computational model of the philosophy adoption of human cognitive things. As a result, there is a natural topic, that is what OOP is? [D & T 1988] [B.S 1991].. In OOP, the object acts as a computing body, has its own name, status, and an interface to accept external messages. In the object model, new objects, old object destruction, send messages, and response messages constitute the root of the OOP computing model.
There are two basic ways to generate objects. One is a new object based on prototype object. One is a new object based on class (Class). The concept of prototype has been used to explain the incremental characteristics of concept learning in cognitive psychology. The prototype model itself is an attempt to produce a variety of new objects based on a representative object. Actual application object. And prototype - commissioning is also one of object abstraction in OOP, one of the code sharing mechanism. A class provides a versatility description of one or more objects. From a formal point of view, the class is related to the type, so a class is equivalent to a collection of instances generated from this class. Such a view will also bring some contradictions, more typical is that in the inheritance system, the behavior between the subset (subclass) object and the parent set (parental) object may be difficult to achieve, this is OOP is often referenced - subType inexpension of subclass (Subclass) [budd 2002]. In the context of all objects, a new object model with metaclass is born on the basis of a class model. That is, the class itself is also an object of other classes. The above three fundamentally different views each define three class-based, prototype-based, and metaclass-based object-based object models. These three object models also lead to many different programming languages (if we temporarily put static and dynamic differences). Yes, we often contact C , Java is a class-based object model, but there are many OOPLs we have not contacted with a completely different object model, they are using another point of view Interpret the meaning of OOP. What is type (TYPE)?
The origin of type and type system and research and development are independent of OOP. In the FORTRAN language compiler implementation in the fifty-year, the type system has been used as a means of type inspection. The generalized type is generally defined as a constraint, that is, a logical formula. A variety of methods are produced in the type of research, such as [C & W 1985], etc. Algebraic Approach is a very good way of establishing a formal specification. A type in the algebraic corresponds to a series of elements, defining algebraic operations above. At the same time, the second-order λ calculation has been supported by the inheritance and template. In the above two methods, the type is considered to be a series of elements that satisfy the constraint conditions, more abstract, can put a type as a constraint condition, if we specify the constraint conditions, the corresponding defined element The more precise collection is, so the logical formulas is the most appropriate tool for the type of description type. Here, we don't want to inquire deeply for various mathematical models of type theory, we need to understand that the type of type (TYPE) and type theory This is extremely rich in programming languages, and The development of their own theory is not limited to OOP, but when both combined, it has a huge impact on our procedures.
Class (Class), Type, Interface
These three concepts are most frequent in OOP, and it is also easier to confuse. And the clarification of these three concepts is also the original intention of writing articles. Let us first look at the diasens of these three concepts -
"The fundamental unit of programming in Java programming language is the class, but the fundamental unit of the object-oriented design is the type.while classes define types, it is very useful and powerful to be able to define a type without defining a class [JAMS 2000]. Interface. "[JAMS 2000]. "IN C , a class is a user definite type" [b.s 1998].
"A type is a name used to denote a particular interface ...... An object may have many types, and widely different objects can share a type.Part of an object's interface may be characterized by one type, and other parts by other types.Two objects of the same type need only share parts of their interface.Interface can contain other interface as subset.We say that a type is a subtype of another if its interface contain the interface of its supertype.Often we speak of a subtype inheriting the interface "of its supertype" [gamma 1995]
Among them, four concepts appear: Class, type (TYPE), Interface, and Contract. Here we say the type and the type mentioned above is different, which is the type in the narrow OOP. In order to understand these concepts, I first divide three conceptual domains: one is for the real world, one is for a specific program design model (here is the OO design model), the last one is for the compiler . That is, the concept in the real world must have a means to map to the OO model, and the concept in the OO model should also have the same concepts in the compiler implementation. As a result, we can say that classes are the concepts in the real world, and traditional OOPL will provide Class keywords to represent support for real world simulation. And the interface is a concept corresponding to the class as an OO program design. In the OO design, what we have to do is designed and programmed for the interface, and the substantial meaning of the interface is a contract between the objects. The type is the corresponding concept of the compiler implementation for the corresponding concepts defined for classes and interfaces. It can be said that the class is the objective concept in the real world, is the material. The interface is the concept that the designer is defined, and the concept of the designer's heart is ideal. The type is a mapping concept of the two concepts of classes and interfaces, is also a material material. The type is mainly used to guide the predicate of the type of compiler, the class is a template for creating a real object, and the interface is the key concept in OO design. These three concepts distinguish between each other (located in different conceptual domains), and each other (all of which represent different conceptual domains representing the same concept). With the above understanding, let's take a look at the most common Java statement below: people a = new man ();
What did this represent? The programmer declares the compiler to the object variable A, and the object variable A itself points to a MAN class (Class) entity (in the compiler is the object variable a point to a type) MAN's entity). Let us return to [Jams 2000], which we can summarize the following: Declare a class or an interface to register a new type to the compiler, and this class or interface and type are shared A name. That is to say. All of the compiler can understand is type, and programmers' work is to translate the concepts in reality into design interface concepts, and the compiler has direct support, which is a class. Declaration or interface declaration is a type declaration in the compiler's understanding. But it is not necessarily true. A class can have multiple interfaces (a class is all likely to implement multiple contract conditions of the designer), and there may be multiple types (because the type is just the concept in this design domain in the compiler) .
Polymorphism, replacement principle, object cutting
The polymorphism has a rich connotation as one of the core mechanisms in OO. As the name suggests, the polymorphism is the meaning of a variety of forms. It mainly has three forms: function polymorphism, object variable polymorphism, generic polymorphism. Function polymorphism mainly includes function overload and overriding. Genericity is mainly provided with a method of creating a general purpose, which can be tailored at a specific occasion. Here, we have to take the object variable polymorphism. Before understanding the object variable polymorphism, we first understand the replacement principles of the OO core mechanism. One feature of the static type of OOPL is that the value of the value contained in a variable may not be equal to the type declared by this variable, which does not have such a feature in the traditional programming language, because we cannot declare Integer variables assign a variable value of strings. The case where the replacement principle is effectively described, the association with two different types is described --- type inheritance. BARBARA LISKOV has described this to describe the replacement principles and the association between the type of role: for each object S of the type S, there is a type T object T, for all program P defined according to the type T, if used The object S replaces the object T. The behavior of the program P remains unchanged, then the type S is the subtype of type T [Liskov 1988] After understanding the polymorphism and replacement principles, we can continue to understand inheritance and replacement principles A new point of view. It can be said that the introduction of inheritance and replacement principles affects almost all OOPLs, including type systems, value semantics / reference semantics, object memory spatial assignments, and more. Below, I will try to gradually open the various causations.
First consider, people a; how this code will be implemented in the compiler? It must be definitely first bound to object A first, and must be assigned a space for object a. At the same time, we create a subclass of people due to Man Is a people. According to the principle of polymorphism and replacement, we can of course allow object A to save a value of a MAN type (this is the performance of the replacement principle). This is an intuitive description, but there are some difficulties in the implementation of programming languages. We know that inheritance is an extension interface and implementation, then we will hardly guarantee that the MAN type does not extends to the people type, and once we make an extension, how can we use the space to store the people's object value? ?
People a;
Man b = new man ();
A = B;
Such a code will first cut the B object, and then store it to the A object space. However, this is not what we expect. Then, in order to support the inheritance, polymorphism, and replacement principles of OOP, it is necessary to avoid the occurrence of object cuts. What all the allocation space models we will use? Commonly used three ways:
1, only for the storage space of the base class PEOPLE, does not support object polymorphism and replacement principles. Such model memory is compact, and storage efficiency is high.
2. Allocate the maximum class object in the inheritance tree (here is the object value space of the Man class), such a model is simple, and the multi-state and replacement principle can avoid object cutting problems, but very wasteful memory space is very wasteful. obvious.
3. Only allocate the storage space required to save a pointer, and assign the space size required to assign the object actual type by the heap at runtime, which can also achieve the multi-state and replacement principles to avoid object cutting problems. (That is to say A is just a reference to an object, not a real object, and the generation of real objects must rely on programmer's explicit statement).
For the three memory models mentioned above, 1 and 3 are employed by some programming languages. I believe that everyone should start to understand. Yes, C as a successor of C language, forcing the pursuit of efficiency to force it to use the first minimum static spatial assignment, due to the operational efficiency based on the stack space is much higher than the operational efficiency based on the stack-based space, So C allows the object to be saved with the stack space, but also allows the heap space to save objects, which can be said that C is a memory model that mixed with 1 and 3, and C is based on 1 memory model object, that is, the stack The object of memory space is no way to reflect the principle of polymorphism and replacement (please think about what object in C is stack), and 3-memory model-based object will support polymorphism and replacement principles (I want to think about C What object is based on a heap). Here, we can finally open the first layer of fog, many people know that only pointers and references in C can support the polymorphic behavior of the object, but why? The best explanation is made. Java language has a great difference due to design concepts and C , which uses the third object model, all objects (except for basic type objects) are all based on heap assignments. This is also the reason why the Java language must adopt GC. A large part of the object in C is not required to manage (static space objects), and in Java, if the GC mechanism is not used, all objects need programmers to manage, and such development cost will be huge. not realistic. This also uncovered the second layer of fog. When we always argue if there is its value for GC when comparing C and Java language, it is not a simple discussion, but when you leave the so-called good and bad discussion, enter When the inherent object of its language itself stores nature, there may be a clear understanding of your own sound.
Let us continue to go, different object memory allocation models directly affect the meaning of the assignment of its programming language. In various programming languages, the assignment can give two different semantic explanations: copy semantics and pointer semantics. Obviously, since C supports two-phase hybrid object storage model (but the default storage method is a stack storage), the default assignment semantic in C is the former, but C provides a functional support for pointer semantics (in copy The constructor is customized in the = operator overload. The latter is used in Java. This is also the last fog we unveiled, and different object storage models directly lead to different assignments.
Object-oriented calculated model and calculated
Programming is to form an arithmetic device with the instruction required to calculate, regardless of how our program design thoughts and how to develop and improve the programming language, ultimately we have used the underlying computational mathematical model has not changed. However, advanced programming languages give us a change in its language environment to build a new, more abstract virtual calculation model. The object computing model introduced by the SmallTalk language has fundamentally changed the previous traditional computing model. The previous calculation model highlights the machine status during the order of order, and the current object calculation model has a collaborative result of the collaboration. The state of all the objects of the calculated object is constituted. Since the object itself has its own state, we can also see an object as a small computing machine. In this way, the object-oriented calculation model evolves into many small computing machines. As the fundamental calculation model in the field of calculations, the accurate points of the calculation: What is calculated, how large is calculated, and how much computational time and space storage size are over. The calculation model defines a computable range, which is defined which issues are solved, which issues are not solved. OOP provides programmers with new computing models, but it does not transcend von. The Mathematical calculation model represented by the Nogan system. So we can't expect OOP to help us solve more problems, or reduce the complexity of the operation. But OOP can help us to describe and solve the real problem with a manner that is more easily understood and accepted. end
This article is the first one of this series. It is a good discussion for many of the core concepts and mechanisms in OOP. The author I sincerely hope that the article can make everyone have more in-depth understanding of OOP, and understand OOP as It has developed approximately 30 years of programming ideas, and its own rich theoretical connotation is not only learning a few OOPLs. Finally, it is expected that this article can realize its original intention --- throwing bricks.
REFERENCE:
[D & T 1988]: Type Theories and Object-Oriented Programming By Scott Danforth and Chris Tomlinson on ACM Computing Surveys Vol.20 No.1 March 1988
[Liskov 1988]: Data Abstract and Hierarchy by Barbara Liskov On Sigplan NOTIices, 23 (5), 1988
[C & W 1985] On Understarden Types, Data Abstract, and Polymorphism by Cardelli. L And Wegner. P on ACM Computing Surveys Vol.17 No.4 DEC 1985
[B.S 1991] What is "Object-Oriented Programming"? (1991 Revised Version) by Bjarne Stroustrup AT & T Bell Lab Murray Hill, New Jersey 07974
[B.S 1998] "THE C Programming Language (Special Editon" by bjarne Stroustrup Addison Wesley 1998
[Gamma 1995] "Design Pattern" by Eric Gamma etc Addison Wesley 1995
[Booch 94]: "Object-Oriented Analysis and Design With Application (Sec Editon)" by grady booch addison weey 1994
[Jams 2000]: "The Java programming language (Third Editon)" by Ken Arnold, Jams Gosling, David Holmes Addison Wesley 2000 [Budd 2002]: "Introduction to Object-Oriented programming (Third Editon)" by Timothy A.Budd Addison Wesley 2002
Reference book:
"C programming language" Bjarne Stroustrup, Yang Zongyan Translation Machinery Industry Press, 2002
"Design mode" Erich Gamma is waiting for Li Yingjun and other translation machinery industry publishing house 2000
"Object-Oriented Software Development Principle" Anton Yuan Zhaoshan and other Translation Machinery Industry Press 2003
"Object-Oriented Programming" TIMOTHY A.BUDD is Huang Mingjun and other translation machinery industry publishing house 2003
"Object-Oriented Analysis and Design" GRADY BOOCH "Translation Machinery Industry Press in Feng Boqin 2003
"Object-Oriented Software Construction" (English version. Second Edition) Bertrand Meyer, Machinery Industry Press, 2003
Posted on August 08, 2004 11:42 AM