Deep understanding of process and object-oriented thinking differences and their respective characteristics

Source: Internet
Author: User
Tags abstract inheritance wrapper

first, what is the process-oriented

Process-oriented is a way of thinking. When trying to solve the problem through the process, our focus is on the problem-solving process, the control of the process, the need for a large number of modules (modular thinking from the hardware, in C language is a function) to solve the problem of large problems, the programmer through the control module execution order to resolve the problem.

For example, when we solve a "how to put an elephant into a refrigerator." "The simplest solution to the problem is the process-oriented approach:
1, focus on the process, the big problem to solve small problems, to achieve the solution of each small problem
A. Open the refrigerator door
B. Load the elephant into the refrigerator
c, close the refrigerator door
2, through the control code, the control module execution, the execution order is a->b->c, the problem solves.

In daily life or daily programming, simple problems are solved by the process-oriented approach, but when the scale of the problem is slightly larger, if you want to describe the problem of 30,000 people eating, or to build an aircraft carrier model, the process-oriented thinking is not enough. And the Code reusability, extensibility, portability, flexibility, robustness of the process-oriented program will have problems in dealing with large-scale problems.

second, what is the object. What is object-oriented thinking. What are the benefits.

1. Objects and Classes

Everything is an object, object is any individual in nature, such as a book, a pen, a leaf, etc., and objects and objects are similar to each other, such as books (each book is an object) are made of paper, printed content (the nature of similarities), are used to see (the similarities in behavior), Abstracting these similarities is a class (the word abstract is difficult to understand, so it is easier to understand the common elements of sorting and extracting things), and instances of classes are objects.

2. Object Oriented thinking

Object-oriented basic elements are abstract, for example, just mentioned in the description of 30,000 people to eat the problem, the first thing to solve is 30,000 people describe the problem, 30,000 people are people, have a nose eye, all to eat, then we put "people" abstract out, their generality is extracted out, the description is easy too much. Each person's differences are given to inherit polymorphism to do it. The process of eating is very simple, with the process-oriented thinking to solve the good, so object-oriented is based on the process-oriented approach to solve the problem, object-oriented more suitable to solve the more complex problems.

The problem of building an aircraft carrier model has just been mentioned, and the process is certainly not possible. Object-oriented also has some problems, because an aircraft carrier is composed of too many components, each component is an object, then the program needs to control too many objects, and the object and object dependencies are extremely complex, and then through the control of the object's life cycle is unrealistic. At this point we think that if there is a reliable third party for us to manage the object is good, this is the famous spring, it by the object's dependencies into the XML, when we need an object, the spring container for us to create, and the object of dependency injection (dependency injection). The control of the object is shifted from the program to the spring container, the IOC (control inversion).

3, Object-oriented features in addition to abstraction, as well as encapsulation, inheritance, polymorphism and other characteristics

Encapsulation is a mechanism that binds code and code to manipulate data so that it is not subject to external interference or misuse. Encapsulation can be understood as a wrapper to protect against code and data being arbitrarily accessed by other code defined outside the wrapper. Access to the wrapper's internal code and data is controlled by a clearly defined interface. the benefit of encapsulating code is that everyone knows how to access the code, so that it can be used directly without having to consider the implementation details, without worrying about unforeseen side effects.

In Java, the most basic encapsulation unit is a class, and a class defines the behavior (data and code) that will be shared by a set of objects. Each object of a class contains the structure and behavior it defines, as if it were a mold cast. Therefore, an object is also called an instance of a class.

When you define a class, you specify the code and data that make up the class. In particular, the object defined by a class is called a member variable or an instance variable, and the code that operates the data is called a member method. Method defines how member variables are used, which means that the behavior and interfaces of the class are defined by the methods that manipulate the instance data:

Because the purpose of a class is to encapsulate complexity , the inside of a class has a mechanism to hide implementation complexity. So in Java, private and public access patterns are provided, and the public interface of the class represents everything that the external user should know or be aware of. Private method data is accessible only through the member code of the class. This ensures that no unwanted things can happen.

4. Inheritance

Inheritance refers to the process by which an object obtains a property from another object. Is the three principles of object-oriented programming, which supports the concept of hierarchical classification. For example, a Persian cat is a cat, a cat is a mammal, a mammal is a kind of animal. If you do not use hierarchical concepts, each object needs to clearly define all of its characteristics. With hierarchical categorization, an object needs to be defined only in its class as the individual properties that it becomes unique, and then inherits its common properties from the parent class. Therefore, it is the inheritance mechanism that allows an object to become a specific instance of a generic class. A subclass of depth inheritance inherits all of its properties from each ancestor in the class hierarchy, enhancing the reusable lines of code . and subclasses can have their own characteristics by overriding the parent class method or by defining their own methods, which greatly improves the program's Extensibility.

Inheritance and encapsulation can interact with each other. If a given class encapsulates some property, any of its subclasses will have the same attributes, plus all the attributes of each subclass. This is an important concept that object-oriented programs are linearly rather than geometrically grown in complexity. The new subclass inherits all the properties of all its ancestors. Subclasses and other code in the system do not produce unpredictable interactions.

5. Polymorphic

Polymorphism refers to a method can have only one name, but there are many patterns, that is, the program can define multiple methods with the same name, "one interface, multiple methods" to describe. You can reference a method by its parameters and type:
Run-time polymorphism refers to the method in which the program is run, and polymorphism is a technique that allows you to set the parent object to be equal to one or more of its sub-objects, and after the assignment, the parent can operate according to the attributes of the child object that is currently assigned to it.

6. Encapsulation, inheritance, multi-state combination use (summary)

In an environment of encapsulation, inheritance, polymorphism, programmers can write programs that are more convenient, robust, and extensible than process-oriented models.

Encapsulation: For the interface consumer, the encapsulation can encapsulate complexity, the user does not need to know the implementation details can be used, for the programmer, through encapsulation can prevent code and data by the external definition of other code arbitrary access, enhance the robustness of the code, and do not have to modify the public interface code to implement the program porting.

Inheritance: A carefully designed class hierarchy is the basis for code reuse and extension.

Polymorphic: Enables programmers to develop simple, easy-to-understand, easy-to-modify, more flexible code.

For example, in terms of inheritance, the driver relies on inheritance to drive different types of cars, whether the car is a car or a truck, a Mercedes or a Fiat, and the driver can find the steering wheel, handbrake, and gear shifters. After a period of driving, you can know the difference between manual and automatic files, because they are actually aware of the common super class: the transmission device.

From a package perspective, the driver always sees the encapsulated features. The brakes hide a lot of complexity, and the look is so simple that you can manipulate it with your feet. The implementation of the engine, handbrake, and tyre size has no effect on the definition of the brake class.

From the multi-state point of view, the braking system has a positive lock locked, the driver only with foot brake parking, the same interface can be used to control a number of different implementations (positive or locked).

So that the individual components are converted to the object of the car. Similarly, by using object-oriented design principles, programmers can combine the various components of a complex program to form a consistent, robust, maintainable program.

Reference:
1. Introduction to the first chapter of Java programming thought
2, Baidu Encyclopedia and Baidu know enthusiastic netizens
3, Shang Academy Blog

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.