As the name implies, a bridge connects two independent road bridges, and the two roads can maintain their own independence. In programming, the bridge in the "bridge" mode is just an image metaphor. According to the definition of the bridge mode, the Bridge Mode implements a topic (either a class or a design concept) the separation of abstract parts and implementation parts (some are defined as the separation of abstract and specific behavior ). The base class of the abstract part and the base class of the implementation part are the functions of the bridge (interface docking). The subclass of the abstract part and the subclass of the implementation part are the routes on both sides of the bridge, they can develop independently (CHANGES) (the reason for being relatively independent is that such changes cannot be separated from the goal of achieving bridging ).
We know that for object research, we can use this model to express objects: attributes and behaviors. Behavior and attributes are the characteristics of objects. The attributes describe the static features of objects, while the behavior expresses the dynamic features of objects. Generally, dynamic features are closely related to static features and affect each other, static features constrain dynamic features (behavior). For example, if a person is a child (attribute), many of his behaviors are restricted. In turn, if a person eats (behavior ), it also grows, affecting height and weight (attributes ). Because the features of objects change, if we want to express this change, first, we need to abstract these objects according to our actual needs and extract the required features, remove secondary or unnecessary features. However, this is only the basis of the research object-Abstraction. We still need to express the changes of important features.
For static features, the expression of changes is relatively simple. You only need to modify the feature value, and it is also easy to implement, as long as the objects at different levels keep their own feature values. However, behavior features are different, because behavior features are not only subject to static features, but also are affected by the environment. It is very difficult to express the changes of all static features and Behavioral Features in classification mode. We only examine the changes of one static feature and one behavior feature, which is the most basic situation, considering the existing technical system, we have two methods to achieve this:
A) use single inheritance of classes to form a classification level. For example, a person is an abstract class, has an important feature gender, and runs an important behavior, of course, different gender running behaviors are different. With inheritance, we can derive two sub-classes: men and women. This behavior can also be divided into men's and women's run. Of course, if you only study the relationship between run and gender, the problem can be solved. As mentioned above, behavior is not only restricted by static features, but also affected by the environment, such as running in different regions. In this case, it is obviously not advisable to perform the classification based on inheritance. First, the class system is too complicated, second, using the environment to classify objects is obviously not a desirable method, because the environment is external.
B) It is implemented through multi-inheritance of classes. for changes to multiple features, this method can reduce the number of classes. For the above example, we can use the abstract of an object and the abstract of the environment (such as the place of origin). People can be divided into men, women, and environments into sand and plastic runways. To describe a man running in the sand, we will inherit from the sand and men to form a new subclass. This method seems acceptable, but there are many problems. First, it does not conform to the object classification principle (the classification of the study object is generally based on the object itself, rather than based on the environment ), in the class system, objects and environments should naturally be separated. Second, technical implementation is also troublesome and violates the single responsibility principle.
So how can we solve this problem? Let's analyze the static and dynamic features of the class. We found that most of the changes to static features do not need to be implemented by using a derived class, you only need to modify and set static feature data. Only static features that have a significant impact on behaviors and belong to our main research scope need to be derived from this attribute, (For example, gender), and changes in behavior characteristics are not only affected by static characteristics, but also by the external environment, but also affect the external environment, for example, the person's running behavior is different in different venues (such as sand and cement). If we adopt the inheritance method to achieve this change, the first is the disadvantage mentioned above, 2. Logically, it is difficult for us to accept the class-derived approach based on the behavior environment (although technically acceptable ), it is difficult to think of a person running in sand and cement as a different class (person), but we can abstract the site, we entrust people running in a certain place of origin to the place of origin for implementation. We keep a reference to the venue in the person class, in addition to implementing the necessary logic in the running method, you can call the specific running behavior in the field. Because both parties can implement their own changes in an inherited manner, we can temporarily assign specific venues for actual use. This reduces the number of classes and makes the logic more simple and flexible. The following is a simple example class diagram:
From the above example, we can see that adding a subclass of a person or adding a place of origin does not need to change the original structure, which is in line with the design's principle of opening and closing.
Summary:
A. The Bridge Mode separates behavioral abstraction from Behavioral implementation in the class, and achieves the development of the class itself and the behavior implementation. In theory, all features can be separated by such delegation to achieve abstraction and implementation. However, I do not recommend such extensive applications, we can use inheritance to implement changes that are mainly influenced by static features. This mode is used only for behavior features that are greatly affected by external environments.
B. it is not necessary to keep a reference to the implementation class of the abstract class in the bridge mode. For the above example, you can complete this combination by running parameterization, in this way, the relationship between people and origin can be reduced from association to simple dependency.
C. The bridge mode provides a method to reduce classes and provides a solution to the variation of classes along multidimensional features.
Note: The Bridge mode can realize independent changes of the system in multiple dimensions, which requires that the changes of the system in these dimensions be independent, if these dimensions are correlated or dependent on each other, the relationship in the system will be too complex, there will be too much communication between them, or even cannot be realized, such as human behavior, breathing, and speaking, these behaviors are related to each other, so it is difficult to use the bridge mode. In this case, a mathematical model is required.
========================================================== =
Previously written. Make a slight modification.