Statement: For reprint, please attach the link to the original text
Tip: The title sequence starts with 6, which corresponds to the order in which the various design pattern notes are published. For example, the adapter pattern, a common Java design pattern for beginners in the first article, starts with 5.
6. Bridge mode (Understanding)
6.1 Introduction to Bridge Mode
-
Bridge design pattern
- Adapter patterns are similar, including design patterns that are close to the same meaning in the future, because the great god tends to mix multiple patterns and match them according to different scenarios. Bridge design patterns are also structural patterns
- Separate the abstract parts from the implementation parts so that they can be changed independently
- In layman’s terms, to bridge other behaviors/dimensions through composition
-
Application scenarios
- The system needs to add more flexibility between the abstract and embodied roles of the component
- You don’t want to use a system where inheritance causes the number of system classes to increase dramatically
- Sometimes a class may have multiple varying dimensions, such as beer, with different capacity and brand, which can be developed by inheritance combination. If there are many dimensions, class expansion is easy to occur. Using bridge mode can solve this problem and decouple
-
Business background
We need to build a mobile phone, we know that mobile phones have a lot of brands, for apple, China, etc., from another dimension, color and has a variety of colors, red, yellow, blue, etc., that if describe these classes, the traditional way is directly through inheritance, requires much more special class, brand 2, and 3 colors, is the six classes, if subsequent to increase the brand even more, The number of classes will proliferate, the so-called class explosion. This problem can be solved using bridge mode, and it is much more flexibleCopy the code
6.2 Bridge Design Mode cases
-
Advantages of bridge mode
- Separation of abstraction and implementation
- Excellent expansion ability, in line with the principle of open and close
-
Disadvantages of bridge mode
- Increase the difficulty of understanding and designing the system
- Using aggregate associations at the abstraction level requires developers to design and program for abstractions, such as abstract class cars, which aggregate color classes, sort of like object adapters
-
Summary and contrast
-
According to GOF, the bridge pattern and the adapter pattern are used at different stages of design
- Bridge mode is used in the early stage of design, fine design, so that the system is more flexible
- The adapter pattern is used when, after the design is complete, it is found that classes and interfaces do not work together and need to be filled in
-
The adapter pattern is often used in situations where third-party apis work together. It is increasingly used in today’s world of functional integration, including for those of you who have heard of the design pattern, which is similar to the adapter pattern in some scenarios
-
Take the mobile phone as an example and go directly to the code:
/ * * *@Auther: csp1999
* @Date: 2020/11/09/21:07
* @Description: Abstract character phone */
public abstract class Phone {
/** * Bridges other behaviors by composition (core) */
protected Color color;
public void setColor(Color color) {
this.color = color;
}
/** * Cell phone method */
abstract public void run(a);
}
Copy the code
/ * * *@Auther: csp1999
* @Date: 2020/11/09/21:08
* @Description: Phone color interface */
public interface Color {
void useColor(a);
}
Copy the code
/ * * *@Auther: csp1999
* @Date: 2020/11/09/21:38
* @Description: * /
public class BlueColor implements Color {
@Override
public void useColor(a) {
System.out.println("Blue..."); }}/ * * *@Auther: csp1999
* @Date: 2020/11/09/11 *@Description: * /
public class RedColor implements Color {
@Override
public void useColor(a) {
System.out.println("Red..."); }}/ * * *@Auther: csp1999
* @Date: 2020/11/09/21:13
* @Description: * /
public class YellowColor implements Color {
@Override
public void useColor(a) {
System.out.println("Yellow..."); }}Copy the code
/ * * *@Auther: csp1999
* @Date: 2020/11/09 / *"@Description: * /
public class HWPhone extends Phone {
public HWPhone(Color color) {
super.setColor(color);
}
@Override
public void run(a) {
color.useColor();
System.out.println("Huawei phones..."); }}/ * * *@Auther: csp1999
* @Date: 2020/11/09 / "*@Description: instantiation of the phone */
public class ApplePhone extends Phone {
public ApplePhone(Color color) {
super.setColor(color);
}
@Override
public void run(a) {
color.useColor();
System.out.println("IPhone..."); }}/ * * *@Auther: csp1999
* @Date: 2020/11/09 / "*@Description: * /
public class SXPhone extends Phone {
public SXPhone(Color color) {
super.setColor(color);
}
@Override
public void run(a) {
color.useColor();
System.out.println("Samsung phone..."); }}Copy the code
Calling tests:
@Test
public void testBridge(a) {
HWPhone hwPhone = new HWPhone(new BlueColor());
hwPhone.run();
ApplePhone applePhone = new ApplePhone(new RedColor());
applePhone.run();
}
Copy the code
Output result:
Blue... Huawei phones... Red... IPhone...Copy the code
7. Composite Design Patterns (Understanding)
7.1 Introduction to The Composite Design Mode
-
Combined design pattern
- Also known as the partial-whole mode, objects are grouped into a tree structure to represent the partial-whole hierarchical structure, which can better achieve management operations
- The composite pattern enables users to manipulate both individual and composite objects in a consistent way
- The basic operations of partial-whole objects are mostly the same, but there should be some differences
- Core: Composite patterns can be represented by a tree
-
Application scenarios
- Bank head office, head office has front desk, logistics, network departments, etc., under the jurisdiction of local branches, there are also front desk, logistics, network departments, the smallest branch does not have sub-branches
- Companies, too, have subsidiaries under the head office, and most of the departments of each company are similar
- There are apis for adding and deleting folders and files, as well as hierarchical management relationships
- When you want to express a partial-whole hierarchy of objects
- When the object we are dealing with can generate a tree structure and we want to operate on nodes and leaves in the tree, it can provide a consistent formula regardless of whether it is a node or a leaf
-
role
- Component: This is an abstract interface that represents the root of a tree. Example: head line
- Composite: Similar to Composite components, it has its own child nodes, such as branches under the head office
- Leaf: represents a child node object in a combination, note that there are no child nodes, example: a branch in the smallest place
In fact, composite design patterns are used less, I will not spend the article about, to recommend an article to understand: JAVA design pattern – composite pattern
Later I will update other design mode blog posts, if the article is helpful to you, hope to click on the likes/favorites/follow! O ~ O (studying studying)