The basic object-oriented thinking

1. Object-oriented and process-oriented differences

Let me talk about my object-oriented and process-oriented understanding. Also a problem solving process, object-oriented, this is seen as a process of interaction between the objects; and oriented process, the problem is divided into n th process, and then performing a certain order. To facilitate understanding For chestnut ( already appreciated, you may be skipped ) .

Problem Description: Bob with the key to open the door, and then turn on the light,

Process-oriented approach is decomposed into 2 a process, open the door, turn on the lights. One method which is the most likely to think.

Object-oriented approach Yes. Encapsulated . 4 objects, people, keys, doors, lights.

The specific implementation, to talk about here, after all, just talk about object-oriented thinking.

 

2. The principles of object-oriented package to be followed

High cohesion:

It refers to the internal cohesion of things closely extent. In object-oriented programming, it is the closeness of the relationship between the various elements within the object. High cohesion in pursuit of things as much as possible to complete only a single function, in order to reduce the complexity of the thing itself. After reducing the complexity of things, and relationships with other things will be easier. The ultimate goal is to avoid a situation indeed affect the whole body.

 

Low coupling:

Coupling refers to the closeness of the connection between things. In object-oriented programming, it is the relationship between the object and the closeness of the object. Low coupling pursuit of things that try to keep their independence, in order to achieve avoid indeed affect the whole body.

For example: When the building blocks to ride high, we want to put the building blocks somewhere dug out. If the coupling between the building and the building is too high, is likely to occur, he dug up a whole building will collapse; but if the coupling is low, we can easily put that place the building blocks out of you, without affecting the rest of the .

This idea also applies in programming. Embodied in object-oriented design is to minimize the interference between the object and the object. With interference is more appropriate word can not be found, because the relationship between objects and sometimes very complex.

Then how to reduce the coupling, my favorite approach is to complete the objects in their own thing, try not to be done by other objects.

By way of specific example, Bob with key lock,

When unlocking, Xiaoming to first key into the keyhole, and then turn the key. If these two methods to open the door into the process of human, it will cause the degree of coupling between people, keys and door increased, and if these two methods to unlock the key into the process when people open the door, only the key to unlocking method call, without having to focus on the specific process of unlocking.

Why such a design? If we change a lock, the keys with this voice, this time master's voice became key, then, unlocking process has changed again, if the former design approach, we have to change people's unlock methods can be applied new changes; the latter, we need to change people's unlocking method, simply call the corresponding key to unlocking can be.

 

3. Object-Oriented Design Personal Experience

change:

Its modification, it is better to add. That is, when there is a method does not meet the needs of the time, rather than modifying the parameters of this method / return value, as a new method to meet increasing demand. This improves compatibility forward.

 

Scalability:

When the object has passed the relationship between the objects, as far as possible to pass an object designed to be polymorphic. This can improve scalability.

 

Abstract classes and interfaces:

An abstract things, generally packaged as an abstract class; common method of certain objects, generally packaged as an interface.

 

Guess you like

Origin blog.csdn.net/qq_42239765/article/details/82558489