Problems solved by object-oriented thinking and three characteristics of Object-oriented Thinking

Source: Internet
Author: User
Bytes. But why can object-oriented solutions solve these problems? The reasons for the history of object-oriented development are as follows: 1. Computers help people solve problems. However, after all, a computer is a machine and only follows the code written by people, the results are finally obtained after step-by-step execution. Therefore, no matter how complicated the program is, the computer can easily cope with it. Structured Programming is the code written according to the computer's thinking, however, people cannot maintain and expand the complicated logic. 2. structured design is to design and construct an application system with the goal of functionality, the real world composed of objects has to be mapped to the solution space composed of functional modules. This conversion process deviates from the basic idea of observing and solving problems. It can be seen that structured design cannot solve the reuse, maintenance, and expansion problems when designing the system. It also leads to complicated logic and obscure code. So people thought, could they let computers directly simulate the real environment and design corresponding applications using human ideas, habits, and steps to solve problems? Such a program is easier to understand when people are reading it, and there is no need to switch back and forth between the real world and the program world. At the same time, people found that objects in the real world are the protagonists in the problem domain. Objects refer to objective object entities and subjective abstract concepts. Objects have attributes and behaviors, the object is stable, the behavior is unstable, and there are various relationships between objects. Therefore, object-oriented programming is more stable than behavior-oriented programming, in the face of frequent changes in demand, behavior is often changed, and objects do not need to be changed. Therefore, we encapsulate the behavior. In this way, we only need to change the behavior, the main architecture remains stable. As a result, object-oriented has produced the system maintainability, scalability, and reusability that people are pursuing. How is this reflected in Object-Oriented Systems? First, let's look at the three features of object-oriented: encapsulation: Finding and encapsulating changes, you can modify or expand the encapsulated changes without affecting other parts, this is the basis of all design patterns, that is, encapsulation changes. Therefore, the role of encapsulation solves the scalability of the program. Inheritance: Child classes inherit the parent class and can inherit the methods and attributes of the parent class to achieve polymorphism and code reuse. This also solves the reusability and scalability of the system, but inheritance destroys encapsulation, because it is open to sub-classes, modifying the parent class will lead to changes to all sub-classes. Therefore, inheritance damages the scalability of the system to a certain extent, so inheritance must be used with caution, only a clear IS-A relationship can be used, and inheritance in the Process of Program Development refactoring, rather than the use of inheritance at the beginning of program design, many object-oriented developers abuse inheritance, as a result, the later Code cannot solve the changes in requirements. Therefore, combination is preferred, rather than inheritance, which is an important experience in object-oriented development. Polymorphism: multiple implementation methods of interfaces are polymorphism. The interface is an abstraction of behavior. As mentioned in encapsulation just now, find and encapsulate the changes. But after encapsulation, how can we adapt to the following changes? This is exactly the role of the interface. The main purpose of the interface is to provide general processing services for irrelevant classes. We can imagine it. For example, the bird can fly, but Superman can also fly. Through the flying interface, we can allow both the bird and Superman to implement this interface, which realizes the maintainability and scalability of the system. Therefore, object-oriented systems can achieve system maintainability, scalability, and reusability. Object-oriented is a programming idea. At first, "object-oriented" refers to the use of encapsulation, inheritance, polymorphism, and other design methods in programming, however, the idea of Object-oriented has already involved various aspects of software development. For example, the concept of Object-Oriented Analysis (OOA), object-oriented design (OOD ), how many steps does Object-Oriented Programming (OOP) take in the face of object programming? Object-oriented is an idea that allows us to turn our thinking and focus to the objects in reality when analyzing and solving problems, and then clarify the relationships between these objects through UML tools, finally, we use an object-oriented language to implement the connection between such objects and objects. It consists of three major steps: Object-Oriented Analysis (OOA), object-oriented design (OOD), and object-oriented programming implementation (OOP. 1. First, analyze the demand. First, do not think about how to use the program to implement it. First, analyze what the stable and unchanged objects in the demand are and what the relationships between these objects are. 2. Expand the model based on the requirements analyzed in the first step into a cost-effective, modular, low-coupling, and high cohesion model. 3. How can we change the process-oriented model to object-oriented thinking using object-oriented models? When we get used to process-oriented programming, we find that there is no need for Object-Oriented in the Process of the program. The main reason is that the thinking has not changed. When a programmer obtains a requirement, the first response is how to implement it. This is a typical process-oriented thinking process and may soon be implemented. Object-oriented is the object. The first step is not to consider how to implement the requirements, but to analyze the needs. It is to find the objects based on the needs and then find the relationships between these objects. Therefore, the key point of process-oriented and object-oriented thinking transformation is to design in the first step. After obtaining the requirement, do not consider how to implement it, but use UML modeling, then implement it according to the UML model. This transformation may require a process. The differences between object-oriented and Object-based methods: "object-oriented" and "Object-based" both implement the concept of "encapsulation", but the object-oriented method implements "inheritance and polymorphism ", objects are not implemented. Therefore, in our web development and general layer-3 development, Object-based implementation is basically implemented, because only encapsulation is implemented, but inheritance and polymorphism are not used, but this is also normal, in web development, most of the functions are simple addition, deletion, modification, and query. The bll layer in the middle is basically the return of a New DAL instance, and the relational database has done a lot of work for us, therefore, most of the work is to simply read and display it. Therefore, when there is no complicated requirement, there is nothing wrong with the process-based approach, and there is nothing wrong with the process-oriented approach. practicality is the overwhelming factor. Three object-oriented features? Http://www.cnitblog.com/lily/archive/2006/02/23/6860.aspxsealing and sealing are the best. Encapsulation is one of the characteristics of object-oriented, and is the main feature of object and class concepts. Encapsulation refers to encapsulating objective objects into abstract classes. classes can only perform trusted class or object operations on their own data and methods to hide untrusted information. One of the main functions that inherit the object-oriented programming language (OOP) is "inheritance ". Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class. The new class created by inheritance is called a subclass or a derived class ". The inherited class is called "base class", "parent class", or "super class ". The process of inheritance is from general to special. To implement inheritance, You can implement it through "inheritance" and "Composition. In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can only have one base class. To implement multi-inheritance, You can implement multi-level inheritance. Inheritance concepts can be implemented in three ways: Implementation inheritance, interface inheritance, and visual inheritance. Implementation Inheritance refers to the ability to use the attributes and methods of the base class without additional encoding; interface inheritance refers to the ability to only use the names of attributes and methods, but the subclass must provide implementation; visual Inheritance refers to the ability of sub-forms (classes) to use the appearance of base Forms (classes) and implement code. When considering inheritance, note that the relationship between the two classes should be "Belong. For example, if the employee is a person and the manager is a person, both classes can inherit the person class. However, the LEG class cannot inherit the person class, because the leg is not a person. An abstract class only defines the general attributes and methods that will be created by the subclass. When creating an abstract class, use the keyword interface instead of class. The OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (Inheritance and synthesis) → designing and implementing classes and instances. Polymorphisn is a technology that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. Implemented through the virtual hosting mechanism (late binding, late-binding ). There are two methods to achieve polymorphism: overwrite and reload. Overwrite refers to the method by which the subclass redefines the virtual function of the parent class. Overload means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, maybe the parameter types are different, or both are different ). In fact, the concept of overloading is not "Object-Oriented Programming". The implementation of overloading is that the compiler modifies the name of a function with the same name based on different parameter tables of the function, then these functions with the same name become different functions (at least for the compiler ). For example, there are two functions with the same name: function func (P: integer): integer; and function func (P: string): integer ;. The modified function names of the compiler may be: int_func and str_func. For the call of these two functions, it has been determined between the compilers that it is static (remember: It is static ). That is to say, their addresses are bound during compilation (early binding). Therefore, overloading is irrelevant to polymorphism! "Overwrite" is related to polymorphism ". When the subclass redefined the virtual function of the parent class, the parent class pointer is dynamic based on the different subclass pointer assigned to it (remember: Dynamic !) The call of this function belongs to the subclass. Such a function call cannot be determined during compilation (the address of the virtual function of the subclass called cannot be provided ). Therefore, such a function address is bound at runtime (later bound ). The conclusion is that overload is only a language feature and is irrelevant to polymorphism and object orientation! Reference Bruce Eckel: "Don't be stupid. If it's not late, it's not a polymorphism ." So what is the role of polymorphism? We know that encapsulation can hide implementation details to modularize the code. inheritance can expand existing code modules (classes). They are all for the purpose of code reuse. Polymorphism aims to achieve another purpose-interface reuse! Polymorphism is used to ensure that a certain attribute of an instance of any type in the family tree is called correctly when the class is inherited and derived.

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.