Object-Oriented Programming

Source: Internet
Author: User
Software Software Crisis)
Software technology is always evolving and changing, and new tools and technologies are emerging one after another. This requires the software industry and software engineers to constantly seek new ways of software design and development. Due to the increasing complexity of software systems and the increasing competition in the software industry, this requirement becomes more urgent. To overcome the software crisis caused by such requirements, the following problems must be solved:
1. in system design, how does one express the real entity of the problem?
2. How to design a system with an open interface?
3. How can we ensure the reusability and extensibility of modules )?
4. How to develop a module that can tolerate future changes?
5. How can I improve the software productivity and reduce the software overhead?
6. How to manage the schedule?
7. How to improve software quality?
8. How to industrialize the software development process?
When a software product is released when it is not completed, in use, or with various errors, the problem occurs. In addition, changes in user requirements have become an important issue. Multiple reports on software implementation show that prior to the release and use of software products, you need to carefully evaluate the quality. The following quality factors should be taken into account in general status evaluation:
1. Correctness)
2. Maintainability)
3. Reusability)
4. Openness and Interpretability)
5. Portability)
6. Security)
7. Integrity)
8. User friendliness)

Software Evolution)
Ernest Tello, a famous author in the AI field, has compared the evolution of software technology to the growth of trees. Like a tree, Software Evolution has obvious stages, which are called layers ). Over the past 40 years, these layers have been gradually established, and each layer has evolved from the previous layer. Figure 1 shows the process. However, the tree metaphor fails in the life cycle of the layer. In software systems, each layer continues to play a role, while in the tree, only the top layer is useful.
Object-Oriented Programming (OOP) is a new method to complete programming. Since the invention of the computer, the methods of programming have changed dramatically to adapt to the increasing complexity of the program. After the Assembly Language is invented, programmers can finally use symbols to represent those machine commands, so that they can write longer and more complex programs. As programs continue to grow, advanced languages are introduced to provide programmers with more tools to deal with increasing complexity. The first widely used language is FORTRAN. However, although FORTRAN has taken a major first step, it is hard to say that the code written with it is clear and easy to understand.

The idea of structured program design was born in 1960. C, Pascal, and other languages strongly advocate this method of program design. Structured Programming languages make it easy to write complex programs. However, once a project reaches a certain scale, the situation will become uncontrollable even if structured programming methods are used.
Every major breakthrough in the development of programming methods enables programmers to cope with greater complexity. In every step on this road, new methods have applied and developed the best ideas of previous methods. Today, the scale of many projects has been further developed. To solve this problem, the object-oriented programming method came into being.
Before introducing Object-Oriented Programming in detail, let's take a brief look at the process-oriented programming methods. In process-oriented programming methods, problems are considered as a series of tasks to be completed, such as reading, computing, and printing. Many functions are used to complete these tasks. The focus of the problem is on functions. Figure 2 shows a typical process-oriented program structure. The layered decomposition technique is used to identify a series of tasks to be completed to solve specific problems.

The basic task for process-oriented programming is to compile a sequence of commands executed by computers and organize these commands in the form of functions. Generally, we use flowcharts to organize these actions and describe the control flow from one action to another.
When we focus on developing functions, we seldom pay attention to the data used by multiple functions ). What happened to the data? What impact does the functions that use the data have on them?
In multi-function programs, many important data is stored in the global data zone, so that they can be accessed by all functions. Each function can have its own local data. Figure 3 shows the relationship between functions and data in a process-oriented program.

Object-Oriented Programming Model
The main starting point of the invention of the object-oriented programming method is to make up for some shortcomings in the process-oriented programming method. OOP regards data as a basic element in program development and does not allow them to flow freely in the system. It closely links data with functions that operate on the data, and prevents data from being accidentally changed by external functions. OOP allows us to break down the problem into a series of entities-these entities are called objects and then build data and functions around these entities. The structure of data and functions in Object-Oriented Programming is shown in Figure 4.

The data of an object cannot access functions of other objects, and the function of an object can access functions of other objects.
Some notable features of object-oriented programming include:
· The focus of program design is data rather than process;
· Programs are divided into so-called objects;
· The data structure is designed to express the characteristics of objects;
· Functions Act as operations on an object's data and are closely integrated with the data structure;
· Data is hidden and cannot be accessed by external functions;
· Objects can be communicated through functions;
· New data and functions can be easily added as needed;
· Follow the bottom-up design method during the program design process.
Object-Oriented programming is a new concept in programming patterns, which may mean different content for different people. Therefore, before proceeding to the following content, we 'd better define object-oriented programming. Our definition of object-oriented programming is "object-oriented programming is a method that provides data and functions with independent memory space, these data and functions can be used as templates to create copies of similar modules as needed. Such a programming method is called object-oriented programming ."
From the above definition, we can see that an object is considered to be an independent interval in the computer memory, which stores data and a group of operations that can access data. Because the memory interval is independent from each other, objects can be applied to multiple different programs without modification.

What Is Object-Oriented Programming?
Object-Oriented Programming (OOP) technology draws on good ideas in structured programming and combines these ideas with some new and powerful ideas, this provides a completely new method for your program design. Generally, in an object-oriented programming style, you break down a problem into correlated subsets, each of which contains relevant data and functions. At the same time, you will divide these subsets into different levels in some way, and an object is a defined type of variable. When you define an object, you create a new data type implicitly.

Basic concepts in Object-Oriented Programming
As a term, "Object-Oriented" has different interpretations in different groups of people. Therefore, it is necessary to understand some concepts that are widely used in object-oriented programming. In this section, we will discuss the following:
1. Object)
2. Class)
3. Data abstraction action)
4. Inheritance)
5. Dynamic binding)
6. Data encapsulation)
7. Polymorphism)
8. Message passing)

In an object-oriented system, objects are the basic entities in the runtime. It can be used to indicate a person or a bank account, a data table, or anything else that needs to be processed by a program. It can also be used to represent user-defined data, such as a vector, time, or list. In object-oriented programming, the problem analysis is generally based on the natural relationship between objects and objects. As mentioned above, an object occupies a certain amount of space in the memory and has an associated address, just like the record structure in Pascal and C.
When a program is running, objects interact with each other by sending messages. For example, a program contains a "customer" object and an "account" object, and the customer object may send a message to the account object to query its bank accounts. Each object contains the data and the code used to operate the data. Even if you do not understand the data and code details of each other, objects can still interact with each other. All you need to know is the type of message that the object can accept and the type of response returned by the object, although different people will implement them in different ways.

As we mentioned earlier, the object contains the data and the code to operate on the data. All data and code contained in an object can form a user-defined data type through classes. In fact, an object is a variable of class type. Once a class is defined, we can create multiple objects of this class. Each object is related to a group of data, and the type of this group of data is defined in the class. Therefore, a class is the abstraction of objects of the same type. For example, mango, apple, and orange are all fruit objects. A class is a user-defined data type, but in a programming language, it performs the same behavior as the built-in data type. For example, the syntax for creating a class object is the same as that for creating an integer object. If fruit is defined as a class, the statement
Fruit mango;
A fruit Class Object mango is created.

Data abstraction and Encapsulation
Packaging data and functions in a single unit (called a class) is called encapsulation. Data encapsulation is the most typical feature of a class. Data cannot be accessed by external users, but can only be accessed by functions encapsulated in the same class. These functions provide interfaces between object data and programs. The concept of preventing direct access to data by programs is called "data hiding ".
Abstraction refers to a behavior that only shows the core features and does not describe the background details. Class uses abstract concepts and is defined as a series of abstract attributes such as size, weight, and price, and functions that operate on these attributes. Class encapsulates all the core attributes of the object to be created. Because classes use the concept of data abstraction, they are called abstract data types (ADT ).

The encapsulation mechanism binds data and code together to avoid external interference and uncertainty. It also allows object creation. Simply put, an object is a logical entity that encapsulates the data and the code that operates the data.
In an object, some code and/or data can be private and cannot be accessed by the outside world. In this way, the object provides different levels of protection for internal data to prevent unexpected changes or incorrect use of the private part of the object in unrelated parts of the program.

Inheritance is a method that allows an object of a certain type to obtain the attributes of an object of another type. It supports the concept of classification by level. For example, the bird belongs to birds. As shown in figure 5, the classification principle is that each subclass has a common feature of the parent class.

In OOP, the concept of inheritance well supports code reusability. That is to say, we can add new features to an existing class without changing this class. This can be achieved by deriving a new class from this existing class. This new class will have the features of the original class and new features. The charm and strength of the Inheritance Mechanism lies in the fact that it allows programmers to take advantage of existing classes (close to needs, rather than fully compliant with needs) and can modify the class in some way, it does not affect other things.
Note that each subclass only defines the unique features of this class. If there is no hierarchical classification, each type must explicitly define all its features.

Polymorphism is another important concept of OOP. Polymorphism means that things have different forms of capabilities. For example, an operation may have different behaviors for different instances. This behavior depends on the type of data to be operated. For example, for an addition operation, if the operated data is a number, it sums two numbers. If the operated data is a string, it connects two strings.
Figure 6 shows how a function processes different numbers and types of parameters. Just as a word has different meanings in different contexts.

Polymorphism allows objects with different internal structures to share the same external interface. This means that although the specific operations on different objects are different, they can be called in the same way through a public class. Polymorphism is widely used in the process of inheritance.
Object-Oriented Programming Languages Support polymorphism. The term is "one interface multiple method (one interface, multiple implementations )". Simply put, the polymorphism mechanism allows a group of related but different actions to be triggered through the same interface. In this way, the Code complexity can be reduced. The compiler determines what actions should be taken under a specific situation without manual intervention by the programmer.
In multi-function programs, many important data is declared as global variables so that they can be accessed by all functions. Each function can have its own local variables. Global variables are easily changed by a function. In a large program, it is difficult to tell which variables are used by each function. If we need to modify the structure of an external data, we need to modify all functions that access this data. This can easily cause bugs.
Another serious defect in structured programming is that it cannot simulate the real world well. This is because the functions are process-oriented, rather than actually corresponding to the various elements in the problem.
Process-oriented programming has the following features:
· Emphasize (algorithm );
· Large programs are separated into many small programs, which are called functions;
· Most functions share global data;
· Data is open from one function to another. Function converts data from one form to another.
The top-to-bottom programming method is used.

Dynamic binding
Binding refers to linking a process call with the corresponding code. Dynamic binding means that the code associated with a given process call is known only at runtime. It is closely related to polymorphism and inheritance. A function call with a multi-state reference depends on the dynamic type of the reference.
Consider the "draw" method in figure 6. Through inheritance, each object has this process. However, different objects have different algorithms. Therefore, the draw process must be redefined in each class. During the runtime, the code corresponding to the currently referenced object will be called.

Message transmission
An Object-Oriented Program consists of many objects, which need to communicate with each other. Therefore, in the object-oriented programming language, the main steps of programming are as follows:
1. Create a class that defines the object and its behavior;
2. Create an object by class definition;
3. establish communication between objects.
Objects communicate with each other through sending and receiving information, which is similar to information transmission between people. The concept of information transfer makes direct simulation of the real world easier to establish system communication.
For a specific object, a message is a process of request execution. Therefore, the receiving object of a message calls a function (process) to produce the expected results. The message content includes the name of the object receiving the message, the name of the function to be called, and necessary information.
An object has a lifecycle. They can be created and destroyed. As long as the object is in its lifetime, it can communicate with it.

Advantages of OOP
OOP has many advantages, both for program designers and users. Object-oriented provides solutions to many problems in software product expansion and quality assurance. This technology can greatly improve the productivity of programmers, improve the quality of software, and reduce their maintenance costs. Its main advantages are shown below:
1. through inheritance, We can greatly reduce unnecessary code and extend the usage of existing code;
2. We can build our programs on standard modules (here the so-called "standard" refers to the protocol between programmers) without starting from scratch. This can reduce software development time and improve production efficiency;
3. The concept of data hiding helps programmers protect their programs from external code attacks;
4. Multiple instances of an object can exist simultaneously without mutual interference;
5. Objects in the problem space can be directly mapped to the program;
6. Object-based projects can be easily divided into independent parts;
7. The data-centric design method allows us to grasp more details of models that can be implemented;
8. The object-oriented system can be upgraded from small to large;
9. The description of the Message Passing technology and external system interface used for inter-object communication is simpler;
10. Easier control of software complexity.
When we need to organically combine all the features mentioned above into an object-oriented system, the relative importance between them depends on the type of project and the preferences of programmers. Many things must be taken into consideration to gain some of the above advantages. For example, the object library must be reusable. Technology continues to develop, and existing products will be updated quickly. If reuse is not implemented, strict control and management are required.
Easy-to-use development software is often difficult to compile. Object-Oriented programming tools are expected to solve this problem.

Object-Oriented Programming Language
Object-oriented technology is not the privilege of a specific language. Like structured programming, OOP can be implemented in many languages such as C and Pascal. However, when the program grows bigger, the program design work will become poor and messy. A programming language that supports the concept of OOP can simplify everything.
A language must support several major OOP concepts before it can be called object-oriented. Based on the supported OOP features, languages can be divided into the following two types:
1. Object-based programming language;
2. object-oriented programming language.
Object-based programming languages only support encapsulation and object recognition.
An Object-Oriented Programming Language must support the following important features:
· Data encapsulation
· Data Hiding and access mechanisms
· Automatic object initialization and clearing
· Operator Overloading
The language that supports object style programming is called an object-based programming language. They do not support inheritance and dynamic binding.
Ada is a typical object-based programming language.
Object-Oriented programming not only includes the features of Object-based programming, but also supports inheritance and dynamic binding.
Application of OOP
The most promising application fields of OOP are as follows:
1. Real-Time System;
2. Simulation and modeling;
3. Face object database;
4. hypertext, hypermedia, and extended text;
5. AI and expert systems;
6. Neural Networks and parallel programming;
7. decision support and office automation system;
8. CIM/CAM/CAD system.

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.