Object-oriented Thinking

Source: Internet
Author: User

This article summarizes and collects object-oriented ideology and materials, and tries to deepen the understanding of object-oriented.

Object-Oriented Methodology: Chapter 10 from C ++ Primer

Object-oriented is a conceptual method of programming. The most important feature of object-oriented is abstraction, encapsulation and data hiding, polymorphism, inheritance, and code reusability. The following example shows the differences between process-oriented and object-oriented programming. You need to write a program to record the statistics of the softball team, the involved data includes the player name, the number of hits, the number of hits, and the hit rate.

If we adopt a process-oriented programming method, we need to input the relevant data of these teams first, so we may need a function to process the input, the input may be from the file or from the console. After obtaining data, we need to perform some calculations, such as the hit rate, which may need another function for calculation. After the computation is complete, the output result is displayed. A function is required to format and output the result. When a new game ends, the results will be updated and we don't want to try again. Therefore, a function is designed to update the existing data and output results. Now, how can we express this data? Organizes the information and data of each member into a struct and uses a structure array. Simply put, when adopting a process-oriented programming method, consider the steps followed first and then how to represent the data.

What if we adopt an object-oriented programming method? We should first consider data: not only how data is expressed, but also how data is used. What are we tracking, of course, players. Therefore, an object is required to represent all aspects of a player. This is a basic data unit-an object that represents the basic information and statistics of contestants. We need some methods to process objects: first, we need to add information data to this object, and second, we need some calculation data (such as hit rate) methods, the calculation method should be completed automatically without user interference. In addition, there must be a new method for updating data. Therefore, there are three types of interactions between users and data: initialization, update, and report results-this is the interface. In short, when we adopt the OOP method, we first consider the user's point of view-to describe the data required by the object and to describe the operations required for user-to-data interaction. After describing the interface, you need to determine how to implement the interface and data storage. Finally, use a definite method to implement the program.

 

My comments: In the process-oriented method, The first thing in our mind is something similar to a flowchart, while the first thing we use in OOP is something similar to an object relationship diagram.

 

From Baidu encyclopedia

Using traditional structured methods to develop large-scale software systems involves knowledge in different fields. When developing systems with vague requirements or dynamically changing requirements, the developed software system often cannot meet the needs of users. The stability, modifyability, and reusability of software developed using structured methods are poor. This is because structured methods are essentially functional decomposition, starting with a single process that represents the overall functions of the target system, from top to bottom, we constantly break down complex processing into sub-processing, so that layer-by-layer decomposition will continue until there are only a few sub-processing functions that are easy to implement, then, the corresponding tools are used to describe the processing of each lowest layer. Therefore, the structured method constructs the system around the "process" to implement the processing function. However, most of the changes in user requirements are for functions. Therefore, such changes are disastrous for process-based design. The system structure designed using this method is often unstable. Changes in user requirements often result in large changes in the system structure, which requires a large cost to achieve this change.

 

My comments: This design method based on software functions is difficult to cope with. It means that the customer's needs may be adjusted, and the software running environment may also change. Because the functions are divided by layers, this change often leaves us in the dilemma of getting started.

 

Object-oriented is an idea and a world outlook and methodology in the software field.

Object-oriented thinking involves various aspects of software development. For example, object-oriented analysis (OOA, Object Oriented Analysis), object-oriented design (OOD, object oriented design), and object-oriented programming implementation (OOP, object Oriented Programming ). Many articles about object-oriented are only about the issues that need to be paid attention to in object-oriented development or the better design methods used. Only by understanding what objects are and what object orientation is, can these articles help you to the greatest extent. I am afraid that the concepts of beginners and even those who have been engaged in related work for many years will be vague.

 

Many people do not distinguish between "Object-Oriented" and "Object-based. The three characteristics of object-oriented (encapsulation, inheritance, and polymorphism) are indispensable. Objects are usually used based on objects, but existing Object Templates cannot be used to generate new object types and generate new objects. That is to say, objects are not inherited. "Polymorphism" indicates a subclass object instance of the parent class. Without the concept of inheritance, there is no way to talk about "polymorphism ". Nowadays, many popular technologies are based on objects. They use encapsulated objects, call object methods, and set object attributes. However, they cannot allow programmers to derive new object types. They can only use methods and attributes of existing objects. So when you determine whether a new technology is object-oriented, you can usually use the last two features to determine. Both "Object-Oriented" and "Object-based" implement the concept of "encapsulation", but object-oriented implements "inheritance and polymorphism", while "Object-based" does not, indeed, it is very good.

People engaged in object-oriented programming can be divided into class library creators and class library users by division of labor ". Not all people who use the class library have the idea of object-oriented. They usually know how to inherit and derive new objects to use the class library. However, our thinking has not really been transferred, class Libraries are only object-oriented in form, but essentially an extension of library functions.

Object-oriented is an idea. It is a method for us to consider things. It usually shows that we solve the problem according to the process, or abstract the problem into an object to solve it. In many cases, we will solve the problem through the process without knowing it, instead of abstracting the problem to be solved as an object to solve it. Some people are engaged in process programming under the guise of object-oriented.

 

My comments: You must use the object-oriented method to analyze the problem at the beginning, rather than finding the desired object in the object library based on the required functions for derivative extension. Use these classes with procedural methods.

 

What hinders our understanding of Object-oriented thinking:

As the computer's brain, the CPU continuously reads and executes the next instruction cyclically. This process is so strict that it gives strong hints to computer students, we must ensure that every step of our code is correct. Therefore, in the beginner programming stage, we always focus on checking the logic process of the Code, checking whether the logic is correct, whether the condition branch is sufficient, and whether the algorithm steps are clear and correct. No way. Who is computer itself a process-oriented thing? We had to arrange our code for CPU review.

We must understand how computers work, and there is no reason to adopt a process-oriented approach in the initial stage of programming. However, this often leads us to consume more brain cells to get rid of the limitations of the process to achieve a true understanding of OOP. The real world is originally composed of various objects. In order to write a program, we need to understand it as a process, and then to better compile the program, we need to return to the reality-using OOP.

I think computers are process-oriented devices. From this perspective, our programs should also be process-oriented. However, programs are compiled by people, and the world is object-oriented in the eyes of people. This is a pair of contradictions. The key and advanced programming method to solve this contradiction-oop and compilers with good support for oop. Oop wants us to write programs in a more humane and realistic way, so that the generated programs can better adapt to changes in the objective world, And the compiler will generate the desired instruction process for the computer. In order to achieve this perfect conversion, the compiler has done a lot in silence, resulting in a lot of code and data that we don't see, so OOP will make the program relatively less efficient. Therefore, only when the computer performance reaches a certain level can we enjoy the benefits of OOP; under such conditions, this loss of performance is completely worthwhile, because OOP makes the program easier to write and maintain, what is the significance of designing programs that are hard to write and maintain just for unimportant performance improvements?

 

One is a process-oriented computer and the other is an object-oriented real world. Oop must seek interaction and balance between the two. With this starting point, we hope to understand all aspects of OOP.

 

Object-oriented nature and object-oriented programming language:

What is the nature of object-oriented? The answer is abstraction. It is the core idea of object-oriented methods to abstract the objects needed to solve the problem from the problem domain. The ability to properly abstract enough object types, especially the ability to abstract potential objects, is the key to determining the quality of software design. From a broader perspective, it is of great significance to abstract the complex problems we face, grasp the essence, and draw a highly refined logic model. From this perspective, abstraction is not limited to object abstraction, but also includes processes and higher-level system structures.

 

Encapsulation, inheritance, and polymorphism are object-oriented features, which can be achieved through object-oriented methods. Distinguish between features and nature.

 

The object-oriented programming language should provide support for implementing the nature and features of object-oriented. We say that a language is object-oriented. In fact, it is intended to provide support for Object-Oriented software. Therefore, the virtual functions, multi-inheritance, and object-oriented functions of C ++ are not necessarily related. However, this language is used to support polymorphism and inheritance.

 

Object-Oriented model:

The following describes various object models and their features through the introduction of various types of UML diagrams. -- Excerpt from the standard modeling language UML tutorial

 

The objective world is a complex and huge system. We need to examine it from different perspectives to truly understand it. In order to support the system from different perspectives, UML defines the following 5 types and 10 model diagrams.

The first type is the use case diagram. It describes the functions of the system from the user's perspective and points out the operators of each function.

The second type is static graphs, including class graphs, object graphs, and packet graphs. The class diagram is used to define the classes in the system, including the relationships between the descriptive classes (such as associations, dependencies, and aggregation) and the internal structure of the class, that is, the attributes and operations of the class. Therefore, a class diagram describes the static structure of the class in the system. It describes a static relationship and is valid throughout the lifecycle of the system. The object chart uses almost the same symbol as the class chart. The difference is that the object Chart Only displays the class object instance, not the actual class. An object graph is an instance of a class graph. Because the object has a lifecycle, the object graph is only valid for a certain period of time in the system. A package diagram consists of packages or classes. It mainly represents the relationship between packages or packages and classes. A package diagram is used to describe the hierarchical structure of the system.

The third type is behavior graphs, which describe the dynamic model of the system and the interaction between objects. A state chart describes the possible states of a class of objects and the conditions for State migration when an event occurs. A state chart is a supplement to a class chart. We do not need to draw a state chart for all classes, as long as there are multiple States for those classes, and its behavior is influenced by the outside world and will change the class to draw a state chart. The activity diagram describes the activities to be performed and the constraints of the activities that meet the requirements of the use case. The activity diagram can be used to conveniently represent and carry out the activities.

The fourth type is the interaction diagram, which describes the interaction between objects. A sequence diagram is used to describe the dynamic cooperative relationship between objects. It emphasizes the order in which messages are sent between objects and displays the interaction process between objects. The other is the cooperation diagram, which focuses on the collaboration relationship between objects. The cooperation diagram and sequence diagram are similar, showing the dynamic cooperation relationship between objects. In addition to information exchange, the cooperation diagram also displays the relationship between objects and objects. If the time and order are emphasized, the Order diagram should be used. If the communication relationship is emphasized, the cooperation diagram can be selected. These two graphs are collectively called the interaction diagram.

The fifth type is the implementation diagram, including the component diagram and configuration diagram. The component diagram describes the physical structure of the Code component and the dependency between the component. A component can be a resource code component, a binary component, or an executable component. It contains information about the logical class and implementation class. The component diagram helps you analyze and understand the degree of interaction between components. The configuration diagram defines the physical architecture of hardware and software in the system. It can display the actual computers and devices (expressed by nodes) and their connections, it can also display the dependency between the connection type and the component or component. Place executable parts and objects inside the node to display the correspondence between the node and the executable unit.

 

These graphs provide multiple graphical representations for system analysis and development. Their organic combination may analyze and construct a consistent system. From the perspective of applications, when using object-oriented technology to design a system, the first is to describe the requirements, and the second is to establish a static model of the system based on the requirements to construct the system structure, the third step is to describe the behavior of the system. The model created in step 1 and Step 2 is static, including the use case diagram and class diagram (including the package diagram), object diagram, component diagram, and configuration diagram. The model established in step 3 can be executed or represents the time sequence status or Interaction relationship during execution. It consists of four types of diagrams: status chart, activity chart, sequence chart, and ing.

 

Dependency and association:An association is a stronger relationship than a dependency. It indicates that two objects are naturally and essentially associated. They are divided into combination and aggregation. Dependency indicates that a class object may depend on another class object in some functions. Of course, Association is also a type of dependency from this perspective, however, UML provides clearer models by distinguishing them. I personally think there is another benefit of distinguishing these two relationships: associations are often highlighted in the class design stage, directly reflecting the model structure we designed, it is a class relationship that we need to pay special attention to. Most dependencies are often reflected in the implementation phase. In actual class diagrams, we do not pay much attention to most dependencies to highlight the key points.

 

 


 

 

 

 

 

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.