Author: Roaring vest Source: http://www.cnblogs.com/arthurliu/
In object-oriented design, we often hear or use the concept of aggregation and coupling. The objective of object-oriented design is to design high-aggregation and Low-coupling programs. However, everyone may have their own answers to what aggregation is and what coupling is. In other words, most people are vague about the concept of aggregation and coupling. I hope to help new students and students in school. I declare that this article is my personal understanding of aggregation and coupling. Part of the content is excerpted from the Internet. If it is inappropriate, I would like to ask you to correct it.
Since the two concepts of aggregation and coupling have always appeared in the form of "high aggregation and low coupling", when we first came into contact with object-oriented design, I always think that aggregation and coupling are the opposite. That is to say, as long as high aggregation is achieved, low coupling is achieved naturally. Although this understanding is not wrong, I have not considered the cause.
Let's take a look at the definition of aggregation:Cohesion is a measurement of the degree of association between components in a module.
There are multiple meanings worth considering. First, aggregation is the internal measurement of a module, which is also the reason why we call aggregation cohesion in many cases. Second, the module appears in a broad sense. It may be a subsystem, a function module, or a class in the function module. The degree of aggregation varies from different levels. As to why the difference is made, I will explain it later. Third, the components of the module include the behavior and status of the module. To achieve high aggregation, the internal behavior of the module must be closely related to the internal status of the module. Generally speaking, a module only provides one independent function, and there is no operation or status unrelated to the function in the module.
For example
Practical Guide to Structured Systems Design)
There are two cities, sidtown and fredborough. The road connecting the two cities is always congested from one day to the other. After investigation by related departments, we found that two companies, better mousetrap and zokko soda, and the better mousetrap factory, were built in sidtown, employees in the factory all reside in fredborough, so a large number of employees depart from fredborough every morning to sidtown and return in the evening. Similarly, zokko soda's transportation vehicles need to travel between the bottle manufacturing and filling factories every day.
Obviously, if better mousetrap's factory and employee's place of residence are both in the same city, and zokko soda's two factories are both built in another city, the traffic between cities will be significantly improved.
Comparing the two figures, the above two cities have traffic problems because each city has relatively low "aggregation": unrelated two companies appear in the same city, in this way, the internal traffic utilization rate is relatively low, while the traffic between cities is overloaded.
Let's take a look at the definition of coupling:Couping is a measurement of the degree of association between modules.Compared with the cohesion of aggregation, coupling focuses on the associations between a module and other modules. In fact, from the previous example, we have inevitably mentioned the coupling problem: due to the close relationship between the two cities, the traffic congestion between the cities has been caused. Another potential problem is that if a traffic problem occurs in one of the cities, the other city will also be affected. Our pursuit of low coupling is to minimize the association between two modules. Changes in one module have as little impact on other modules as possible.
Let's talk about another example in our daily life. I believe that most of the post-80s have played a handheld game machine, which contains a game of Tetris. Although this kind of game machine is popular, it will not be long before it gradually fades out of the market, because this kind of game machine can only play with Tetris. When we get tired of playing, this game machine is just like a waste.
In the same period, Nintendo launched a red and white machine that has been popular for nearly 20 years. Its long life in the gaming machine market is not as good as the quality of the game machine itself, but because of the endless stream of games developed based on the red and white machine, there are countless classics. At that time, even now, I had no idea about soul and super Mary. Games with red and white machines are not stored in game consoles. When a new game is launched, you only need to purchase a new card band. This is the relatively independent design of game consoles and card bands, so that game designers do not need to care about the Implementation Details of game consoles, as long as they follow the interfaces (slots) provided by the game consoles ). Many game design vendors also have a share of the huge market. Most players may not know that Soldado is not a product launched by Nintendo. Rather, konami, a world-famous live football series, was transplanted from an arcade to a red-and-white machine in 1988.
Back to the coupling topic, because the early handheld game machine built the game itself into the machine, the relationship between the game and the machine is too close, so the game is tired of playing, the game machine is useless. The game machine has a problem and the game machine cannot play any more. The relationship between red and white machines and game consoles is relatively independent. As long as they all follow the established agreement, they can develop and change independently. The game card breaks down. Other games can run on the same machine. If your own game machine breaks down, you can also play the card on a friend's game machine. After the development of the red and white machine, even the game machine's handle is pluggable. If the handle is broken, you only need to change the handle.
At this point, we should have a preliminary understanding of aggregation and coupling. So how do we look at the application of aggregation and coupling in practice? How can our programs achieve high convergence and low coupling?
As mentioned above, the degree of aggregation and coupling varies from different levels. In the sidtown and fredborough examples, from the perspective of the city level, the second design completely achieves the goal of High Cohesion and low coupling. However, if we look at different areas of the city, this design is not cohesive enough. If we keep investigating, I'm afraid all the staff of better mousetrap will be on the production line. The pursuit of high cohesion will inevitably lead to a single module function, and the number of modules will expand sharply. Therefore, when designing and implementing programs, we must consider the degree of aggregation and coupling between modules. If you are interested, you can also study the aggregation and coupling metrics.