java design pattern bridge

Bridge mode

Bridge is used to decouple abstraction and implementation so that the two can change independently. This type of design pattern is a structured pattern that provides decoupling between the two by providing a bridge between the abstraction and the implementation.

This pattern involves an interface that acts as a bridge, making the functionality of the entity class independent of the interface implementation class. These two types of classes can be structurally changed without affecting each other.

We use the following example to demonstrate the use of the Bridge Pattern. Among them, you can use the same abstract class method but different bridge implementation classes to draw circles of different colors.

Introduction

Intent: Separate the abstract part from the implementation part so that they can all change independently.

Main solution: In cases where there are many possible changes, inheritance can cause class explosion problems and is not flexible enough to expand.

When to use: Implementations may have multiple angles of classification, and each angle may change.

How to solve: Separate this multi-angle classification and let them change independently, reducing the coupling between them.

Key Code: Abstract class dependent implementation classes.

Application example: 1. The Pig Eight Rings reincarnate from the captain of the canopy to the pig, and the mechanism of reincarnation will divide the world into two levels, namely: the soul and the flesh. The former is equivalent to abstraction, and the latter is equivalent to realization. Through the delegation of functions, the creature calls the function of the physical object so that the creature can dynamically select. 2, the switch on the wall, you can see the switch is abstract, do not care how to achieve inside.

Advantages: 1. Separation of abstraction and implementation. 2. Excellent expansion ability. 3, the implementation details are transparent to the customer.

Disadvantages: The introduction of bridging mode will increase the understanding and design difficulty of the system. Since the aggregation relationship is established in the abstraction layer, the developer is required to design and program the abstraction.

Usage scenarios: 1. If a system needs to add more flexibility between the abstracted role and the concrete role of the component, avoiding establishing a static inheritance relationship between the two levels, the bridge mode allows them to establish an association at the abstraction level. relationship. 2. Bridging mode is especially useful for systems that do not want to use inheritance or because the number of system classes increases dramatically because of multi-level inheritance. 3. A class has two independently changing dimensions, and both dimensions need to be extended.

Note: For two independently changing dimensions, using bridge mode is a good fit.

Implementation

We have a DrawAPI interface implemented as a bridge and an entity class RedCircle, GreenCircle that implements the DrawAPI interface. >. Shape is an abstract class that will use the DrawAPI object. BridgePatternDemo, our demo class uses the Shape class to draw circles of different colors.

bridge_pattern_uml_diagram

Step 1

Create a bridge implementation interface.

DrawAPI.java

public interface DrawAPI { public void drawCircle(int radius, int x, int y ); }

Step 2

Create an entity bridge implementation class that implements the DrawAPI interface.

RedCircle.java

public class RedCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y ) { System.out< Span class="hl-code">.println("Drawing Circle[ color: red, radius: " + radius +" , x: " +x+", "+ y +"]"); } }

GreenCircle.java

public class GreenCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y ) { System.out< Span class="hl-code">.println("Drawing Circle[ color: green, radius: " + radius +" , x: " +x+", "+ y +"]"); } }

Step 3

Create an abstract class Shape using the DrawAPI interface.

Shape.java

public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI){ this.drawAPI< Span class="hl-code"> = drawAPI; } public abstract void draw(); }

Step 4

Create an entity class that implements the Shape interface.

Circle.java

public class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI)< Span class="hl-code"> { super(drawAPI< Span class="hl-brackets">); this.x< Span class="hl-code"> = x; this.y< Span class="hl-code"> = y; this.radius< Span class="hl-code"> = radius; } public void draw() { drawAPI.drawCircle< Span class="hl-brackets">(radius,x,y) ; } }

Step 5

Use the Shape and DrawAPI classes to draw circles of different colors.

BridgePatternDemo.java

public class BridgePatternDemo { public static void main(String[] args) { Shape redCircle = new Circle(100,100, 10, new





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition