Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

An overview of the

Bridge pattern, a structural design pattern, is used to split a class or series of closely related classes into two separate hierarchies, abstraction and implementation, which can be used separately during development.

role

Abstraction: Abstracts the definition and holds a reference to the heap implementation object.

Implementation: provides the interface of implementation, but does not give the concrete implementation.

Precise abstraction: Extends abstraction, references to the implementation object and complements the abstraction object.

Concrete implementation: Implement the methods in the implementation interface.

The template

Abstraction: A graphical abstraction class that refers to an instantiation object and defines a method for drawing.

public abstract class AbstractShape {
    Color color;

    public AbstractShape(Color color) {
        this.color = color;
    }

    public abstract void draw(a);
}
Copy the code

Implementationalization: Defines an interface to get colors.

public interface Color {
    String getColor(a);
}
Copy the code

Precise abstraction: Defines two classes that inherit abstraction, circle and square, precise implementation logic of draw() method.

public class Circle extends AbstractShape{
    public Circle(Color color) {
        super(color);
    }

    @Override
    public void draw(a) {
        System.out.println("Use" + color.getColor() + "Draw circles"); }}Copy the code
public class Square extends AbstractShape{
    public Square(Color color) {
        super(color);
    }

    @Override
    public void draw(a) {
        System.out.println("Use" + color.getColor() + "Draw a square."); }}Copy the code

Concrete implementation: the definition of red and green two realize the color interface class, through getColor() method to give the specific color.

public class Green implements Color{
    @Override
    public String getColor(a) {
        return "Green"; }}Copy the code
public class Red implements Color{
    @Override
    public String getColor(a) {
        return "Red"; }}Copy the code

Client: free combination of different shapes and different colors.

public class BridgePattern {
    public static void main(String[] args) {
        // Create a green object
        Color color = new Green();
        // Create a square with the initial color green
        AbstractShape shape = new Square(color);
        // Use green to draw squares
        shape.draw();

        // Change the graph to a circle
        shape = new Circle(color);
        // Use green to draw circles
        shape.draw();

        // Change the color to red
        color = new Red();
        // Change the graph to square
        shape = new Square(color);
        // Use red to draw squares
        shape.draw();
        
        // Change the shape to a circle
        shape = new Circle(color);
        // Draw circles in redshape.draw(); }}Copy the code

summary

advantages

The open closed principle: abstraction and implementation are separated. They are not constrained by abstraction, and are not bound to a fixed level of abstraction. They do not directly affect each other.

High transparency: The client does not care about implementation details, only interacts with the abstract part, and is not exposed to additional details.

Single responsibility: The abstract part focuses on high-level logic, and the implementation part deals with details.

disadvantages

Increased complexity: Using this pattern for highly cohesive loads can lead to more complex code.

Applicable scenario

  • Bridge mode can be used when large and miscellaneous classes with multiple functions need to be split or reassembled.
  • It is necessary to perform dynamic coupling directly in multiple phase extraction and realization, and bridge mode can be used when switching different implementations.
  • The bridge pattern can be used when you want to extend a class across multiple independent dimensions.

The last

The article has written the bad place, asks the big guy to be generous to give advice, the mistake is can let the person grow up most, wishes me and the big guy the distance to shorten gradually!!

If you feel the article is helpful to you, please click like, favorites, follow, comment four consecutive support, your support is the biggest power of my creation!!