The most common word to learn object-oriented is "abstraction ". What is abstraction? I think there are two definitions: 1. Extract A simplified entity from something that is not a key factor. 2. parameterization.
We know things from the entity, and then extract some details to get abstract and simplified, classification and comparison to get cognition.
For example, if you know a stranger, first you need to know his social status, and then compare him with other people you know to find out what kind of attitude you want to take to him. This is also called Tagging and face-based. As a writer, I often want to overcome this kind of cognitive inertia by filling the details into the faces of the people to make them different. But programmers do not need to do this.
Parameterization discusses this problem from the technical point of view. Parameterization refers to a behavior that can be described by a function. Functions have independent variables and output values. When the behavior mode is defined as a parameterized function, the static relationship is converted into a dynamic relationship. This is equivalent to upgrading from a point and a plane to a group problem from an individual problem to an extended problem, so that the solution can be applied in a wider range.
Therefore, learning abstraction should be divided into two sides. On the one hand, information should be simplified, and on the other hand, dynamic relationships should be established.
The procedure is as follows:
I. Detailed analysis of specific problems.
2. extract key attributes and relationships from specific issues.
Iii. Dynamic Relationship.
Detailed rules:
Step 1: sort the information about the problem domain (Work domain), including the activity object, constraints, communication format, and communication channel.
Step 2. filter information to obtain the key information required for the problem.
Step 3. Change the closely linked static relationship to an object-independent dynamic relationship through parameterization.
Some people like to recognize problems through abstraction. I think it is wrong. It should be gradually abstracted from the specifics. abstraction is a product.
There is no abstraction starting from a specific problem, just a blank imagination.
Therefore, the first step to be emphasized is the information collection phase, also called the investigation phase and the demand analysis phase. What kind of entity can be abstracted.
Filtering information is a key step. Why is the problem complicated and hard to understand? This is because there are too many useless information that affects people's judgment. After information is extracted, useless information can be reduced. On the one hand, things with large surface differences can be attributed to the same category, and problems can be solved by analogy.
There are many parameterization methods, which are not necessarily implemented by using functions, but similar to functions. For example, the algebraic formula is to replace the specific quantity with symbols to form a formula with a wide range of applicability. In programming languages, variables, functions, objects, interfaces, and other advanced features are parameterized. If the purpose of the first step is to solve the current problem, this step is to solve all problems similar to the current problem. Of course, we do not need to do this step in programming, but if there are many such problems, we need to spend more time to abstract this step to improve reusability.
Object-Oriented Programming, people tend to like what is attributed to object, then programming. Objects and abstractions are two different things. objects are tools, while abstraction is methods. This tool does not need to be used. It cares about our specific problems. Therefore, objects cannot be considered as the first step in programming. Sometimes we only need a variable and an array to solve the problem. Why should we use a huge weapon like an object. What needs to be emphasized here is a matter of priority. The solution is to gradually transform from a low-level solution to an advanced solution. Only when the low-level solution is not applicable can it be upgraded to an advanced solution, instead of solving problems, I must use the most advanced and flexible solutions. Being Pragmatic is not like not learning.
I searched for a typical mathematical problem on the Internet for the case:
-----------------------------------
My wife went to the market to buy food. She bought a chicken for 8 yuan.
On the way back, her colleagues wanted to buy her chicken. So my wife sold the chicken for 9 yuan to her colleagues.
Later, she wanted me to eat chicken at noon and found a colleague who bought me 10 yuan.
At the door of the house, the neighbors came to the guests. When the food was cut off, my wife had to sell the chicken to him, and my wife sold the chicken to the neighbors for another 11 yuan.
Q: How much does my wife make?
-----------------------------------
The first step is to collect information, "Wife, market, chicken, road, colleague, money, noon, home, neighbor". The problem is how much money my wife earned.
The result of the first step will show a lot of information we don't know whether it is useful or not, which requires the second step to filter. Among them, wife, market, colleagues, and neighbors are key objects. Chicken and money are communication information. On the way, at noon, the door is useless information. Relationship: the relationship between the wife and the market, the colleagues and the wife, the wife and the colleagues, the neighbors and the wife.
In this step, you only need to write the program step in sequence to get the answer to the specific question.
Step 3: Wife, market, colleagues, and neighbors are actually a type of things. Apart from different names (different identifiers or different objects), they all know how to buy and sell them. Therefore, four relationships can be parameterized into one relationship: transaction (buyer, seller ). Then, you only need to define four objects:
Transaction type wife, market, colleagues, neighbors.
Transaction (wife, market );
Transaction (colleague, wife );
Transaction (wife, colleague );
Transactions (neighbors, wives );
Output (wife );
Step 1: Wife-8;
Step 2: Wife + 9;
Step 3: Wife-10;
Step 4: Wife + 11;
The result is two RMB.
If you want to output how much other objects earn, you can also use the output function of your wife. This is the benefit of using the object method to parameterize. If you do not have this step, you need to write four specific transaction codes.