Mountain wind 2022-06-24 08:03:03 阅读数:411
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 :
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 】.
The bridge mode contains the following main roles :
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 :
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());
}
}
copyright:author[Mountain wind],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240224254197.html