1, Purpose of bridge connection
Although the bridge pattern is not a frequently used pattern, being familiar with this pattern is very helpful to understand the object-oriented design principles, including the "open close" principle and the combination / aggregation Reuse Principle. Understanding these two principles will help to form correct design ideas and cultivate good design styles.
The purpose of bridge pattern is to "decouple abstraction from implementation so that they can change independently". This sentence is very short, but people who read it for the first time are likely to think for a long time and don't understand its meaning.
This sentence has three key words, namely abstraction, realization and decoupling. Understanding the concepts represented by these three words is the key to understanding the meaning of the bridge model.
~Abstraction
Abstracting the common and essential features from many things and abandoning their non essential features is abstraction. For example, apples, bananas, raw pears, peaches, etc. their common characteristic is fruit. The process of getting the concept of fruit is an abstract process. To be abstract, we must compare. Without comparison, we can't find the parts that are essentially common. Common features refer to those features that can distinguish one kind of things from others. These distinguishing features are also called essential features. Therefore, extracting the common features of things is to extract the essential features of things and abandon the non essential features. Therefore, the process of abstraction is also a process of tailoring. When abstracting, the same and different depend on what angle to abstract from. The angle of abstraction depends on the purpose of analyzing the problem.
Usually, if a group of objects have the same characteristics, they can be described by a common class. If some classes have the same characteristics, they can often be described by a common abstract class.
~Realization
The concrete implementation given by abstraction is realization.
An instance of a class is the instantiation of the class, and a concrete subclass is the instantiation of its abstract superclass.
~Decoupling / decoupling
The so-called coupling is a strong correlation between the behavior of two entities. Removing their strong correlation is the decoupling, or decoupling. Here, decoupling refers to separating the coupling between abstraction and realization, or changing the strong correlation between them into weak correlation.
The so-called strong association refers to the association that has been determined at the compile time and cannot be changed dynamically at the run time; The so-called weak association is an association that can be determined dynamically and changed dynamically during operation. Obviously, in the Java language, inheritance relationship is strong correlation, while aggregation relationship is weak correlation.
To change the inheritance relationship between two roles into an aggregation relationship is to change the strong relationship between them into a weak relationship. Therefore, the so-called decoupling in the bridge pattern refers to the use of aggregation relationship rather than inheritance relationship between the abstraction and implementation of a software system, so that they can change relatively independently. This is the purpose of the bridge model.
Recommend an article https://blog.csdn.net/csflvcxx/article/details/86756148
Well written. Make a record here
At the beginning of learning java, when talking about inheritance, the teacher always likes to use an example to explain, that is, drawing. Here is a brush that can draw squares, rectangles circular (everyone knows how to do this, so I won't explain). But now we need to color these shapes. There are three colors: white, gray and black. Here we can draw figures in 3 * 3 = 9: white square, white rectangle and white circle... Here we almost know that there are two solutions:
Scheme 1: provide various color versions for each shape. Scheme 2: combine colors and shapes according to actual needs. If we adopt the scheme to realize it, can we also understand it in this way? Provide various shapes for each color? This is totally OK. As follows:
For the two figures in, we will be very clear about this problem: if we add ellipses, do we have to add three colors? If we are adding a green, we will add four shapes. Continue to add. Continue to add... Each addition will add several classes (if you add color, you will add several classes of shape, and if you add shape, you will add several classes of color). In this case, I don't think every programmer wants it! Then let's look at scheme 2.
The solution provided by scheme 2 is to provide two parent classes, one is color and the other is shape. Both the color parent class and the shape parent class contain corresponding subclasses, and then combine the color and shape as needed.
For the dimensions with several changes, we generally adopt scheme 2 to realize it, which is not only conducive to reducing the number of classes in the system, but also conducive to system expansion. For the application of scheme 2, we call it bridging mode.
The bridge pattern separates the abstract part from its implementation part, so that they can change independently.
Bridging mode transforms inheritance relationship into association relationship. It reduces the coupling between classes, reduces the number of classes in the system and reduces the amount of code. It is not easy to understand the sentence of separating the abstract part from its implementation part. In fact, this does not separate the abstract class from its derived classes, but the abstract class and its derived classes are used to implement their own objects. It's still incomprehensible. Let's first recognize what is abstraction, what is realization, and what is decoupling. Abstraction: the concept is to extract one or several characteristics of a complex object and only pay attention to other characteristics. In object-oriented, it is the process of extracting the common properties of objects to form classes. Realization: the concrete implementation of abstraction. It is an inverse process with abstraction, and realization is the further concretization of abstract things. Decoupling: decoupling is to separate the coupling between abstraction and realization, or change the strong association between them into weak association, and change the inheritance relationship between two roles into association relationship. For that sentence: separate the abstract part from its implementation part and apply it to the Dahua design pattern, that is, the implementation system may have multiple angle classifications, and each angle may change. Then separate this multi angle classification, let them change independently and reduce the coupling between them. The so-called decoupling in the bridging mode refers to the use of association relationship (combination or aggregation relationship) rather than inheritance relationship between the abstraction and implementation of a software system, so that the two can change relatively independently. This is the purpose of the bridging mode.
Two, mode structure
The following figure is the UML structure diagram of bridge mode:
The bridging mode mainly includes the following roles:
Abstraction: Abstract class. RefinedAbstraction: Extend abstract classes. Implementor: Implement class interfaces. ConcreteImplementor: Concrete implementation class.
The pattern scene is the one we use to draw. Its UML structure diagram is as follows:
3, The most basic code example
The first is the shape class: this class is an abstract class, which mainly provides the method of drawing shape: shape java
public abstract class Shape { Color color; public void setColor(Color color) { this.color = color; } public abstract void draw(); }
Then there are three shapes.
public class Circle extends Shape{ public void draw() { color.bepaint(circular"); } }
public class Rectangle extends Shape{ public void draw() { color.bepaint("rectangle"); } }
public class Square extends Shape{ public void draw() { color.bepaint("square"); } }
Color interface: color java
public interface Color { public void bepaint(String shape); }
public class White implements Color{ public void bepaint(String shape) { System.out.println("off-white" + shape); } }
public class Gray implements Color{ public void bepaint(String shape) { System.out.println("Grey" + shape); } }
public class Black implements Color{ public void bepaint(String shape) { System.out.println("black" + shape); } }
Client: client java
public class Client { public static void main(String[] args) { //white Color white = new White(); //square Shape square = new Square(); //White square square.setColor(white); square.draw(); //rectangle Shape rectange = new Rectangle(); rectange.setColor(white); rectange.draw(); } }
Operation results:
White square White rectangle
4, Advantages and disadvantages
advantage:
1. Separate the abstract interface and its implementation. Improved better solutions than inheritance.
2,The bridging mode improves the scalability of the system. If one of the two change dimensions is extended arbitrarily, there is no need to modify the original system. 3,Implementation details are transparent to customers and can be hidden from users.
Disadvantages:
1. The introduction of bridging mode will increase the difficulty of system understanding and design. Because the aggregation association relationship is based on the abstraction layer, developers are required to design and program for the abstraction.
2. Bridging mode requires to correctly identify two independent changing dimensions in the system, so its application scope has certain limitations.
5, Usage scenario
1. If a system needs to add more flexibility between the abstract role and concrete role of components to avoid establishing static inheritance relationship between the two levels, they can establish an association relationship at the abstract level through bridging mode.
2,The bridging mode is especially suitable for those systems that do not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance. 3,A class has two independently changing dimensions, and both dimensions need to be extended.
6, Summary
1. The bridge mode realizes the decoupling of abstraction and realization. They are independent of each other and will not affect each other.
2,For two independently changing dimensions, the bridge pattern is perfect. 3,Changes to "concrete abstract classes" will not affect customers.