Java design pattern -- bridging pattern [structural pattern]

Mountain wind 2022-06-24 08:03:03 阅读数:411

javadesignpatternbridgingpattern

JAVA Design patterns -- Bridging mode 【 Structural mode 】

1. Definition

background : In a system with multiple dimensions that may change , Using inheritance will cause class explosion , Inflexible to expand . Every time a specific implementation is added to a dimension, many classes will be added . For example, the following class diagram is designed :
 Insert picture description here
When using this design , We need different shapes , We need to inherit Shape Class to implement different shapes , When we need to implement different shapes with different colors , We need classes that also inherit different shapes , In this way, the problem of quasi explosion arises . In order to design the system more flexibly , We can consider using the bridge mode at this time . In the case that follows , We will rewrite this problem all over again using the bridge pattern , For reference .

Definition : Separate abstraction from implementation , So that they can change independently . It uses composition relationship instead of inheritance relationship , This reduces the coupling between the two dimensions of abstraction and reality , It also reflects the design principles of 【 Composite reuse 】 And 【 Dependence reversal 】 principle , It also includes 【 Opening and closing principle 】.

Portal : Object oriented software design principles 【JAVA】( Opening and closing principle 、 Richter substitution 、 Dependence reversal 、 Interface isolation 、 Dimitar's law 、 Synthetic multiplexing principle )

2. structure

The bridge mode contains the following main roles :

  • Abstract character : Defining abstract classes , And include a reference to the implementation object .
  • Extend Abstract roles : It's a subclass of abstract characters , Implement the business methods in the parent class , And through the combination of relationship calls to realize the role of business methods .
  • Realize the role : Define the interfaces for implementing roles , For extension Abstract role call .
  • Concrete realization role : Give the implementation of the role interface .

3. Background case rewriting

By looking at the background case , We found that Shape And color These two dimensions may change , Separate out , Use bridge mode , It is realized by replacing inheritance relationship with composition relationship . The class diagram is as follows :
 Insert picture description here
You can see that we will Shape And Color Separated ,Shape Abstract classes are used to realize ,Color The interface mode is adopted to realize ,Shape And Color Is a composite relationship ,Shape And Color Can develop independently , There will be no case , When we need other colors, we just need to implement Color Interface can , When we implement other shapes, we can inherit Shape To achieve . So called bridging , The feeling is , The design method that originally piled the classes together , Separate according to certain dimensions , And connect through combination , This can improve the scalability of the system , Expand any one of the separated change dimensions , No need to modify the original system .

The implementation code of the case is as follows :

abstract class Shape {

protected Color color;
public Shape(Color color) {

this.color = color;
}
public abstract String PrintShape();
}
class Circle extends Shape{

public Circle(Color color) {

super(color);
}
@Override
public String PrintShape() {

return color.getColor() + " Circle";
}
}
class Rectangle extends Shape{

public Rectangle(Color color) {

super(color);
}
@Override
public String PrintShape() {

return color.getColor() + " Rectangle";
}
}
interface Color {

abstract String getColor();
}
class Green implements Color{

@Override
public String getColor() {

return "Green";
}
}
class Red implements Color{

@Override
public String getColor() {

return "Red";
}
}
public class Client {

public static void main(String[] args) {

Shape circle = new Circle(new Green());
System.out.println(circle.PrintShape());
Shape rectangle = new Rectangle(new Red());
System.out.println(rectangle.PrintShape());
}
}

 Insert picture description here

4. advantage

  • The bridge mode improves the scalability of the system , Expand any one of the separated change dimensions , Do not modify the original system .
  • Make details transparent to customers .

5. Use scenarios

  • When a class has two independent changing dimensions , When both dimensions need to be extended .
  • When a system does not want to use inheritance or the number of system classes increases dramatically because of multi-level inheritance .
  • When a system needs to add more flexibility between abstract and concrete roles of components . Avoid creating static inheritance relationships between two levels , Bridging patterns enable them to establish an association at the abstraction level .
copyright:author[Mountain wind],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240224254197.html