YouTip LogoYouTip

Bridge Pattern

# Bridge Pattern Bridge is used to decouple abstraction from implementation so that the two can vary independently. This type of design pattern is a structural pattern, which provides a bridge structure between abstraction and implementation to achieve decoupling. This pattern involves an interface that acts as a bridge, making the functionality of concrete classes independent from interface implementation classes. Both types of classes can be structurally changed without affecting each other. The purpose of the Bridge Pattern is to separate abstraction from implementation so that they can vary independently. This pattern separates the abstract part of an object from its implementation part, allowing them to change independently. It connects the abstract and implementation parts through composition, rather than inheritance. We will demonstrate the usage of the Bridge Pattern through the following example. Here, we can use the same abstract class method but different bridge implementation classes to draw circles of different colors. ## Introduction ### Intent To separate abstraction from implementation so that they can vary independently. ### Main Problem Solved To avoid the class explosion problem caused by inheritance and provide a more flexible way to extend. ### Use Cases The Bridge Pattern is suitable when a system may be classified from multiple perspectives, and each perspective may vary independently. ### Implementation Approach * **Separate Multi-Perspective Classification**: Separate the classification logic of different perspectives, allowing them to vary independently. * **Reduce Coupling**: Reduce the degree of coupling between abstraction and implementation. ### Key Code * **Abstraction Class**: Define an abstraction class as part of the system. * **Implementation Class**: Define one or more implementation classes, associated with the abstraction class through aggregation (not inheritance). ### Application Examples * **Reincarnation**: Separation of soul (abstraction) and body (implementation), allowing the soul to choose different bodies. * **Wall Switch**: Separation of switch (abstraction) and internal implementation (implementation), where the user does not need to care about the internal working mechanism of the switch. ### Advantages * **Separation of Abstraction and Implementation**: Improves system flexibility and maintainability. * **Strong Extensibility**: Abstraction and implementation can be extended independently. * **Transparent Implementation Details**: Users do not need to understand the implementation details. ### Disadvantages * **Difficulty in Understanding and Design**: The Bridge Pattern increases the difficulty of understanding and designing the system. * **Aggregation Association**: Requires developers to design and program at the abstraction level. ### Usage Suggestions * When the system needs to increase flexibility between abstraction roles and concrete roles, consider using the Bridge Pattern. * For systems that do not wish to use inheritance or where the number of classes increases sharply due to multi-level inheritance, the Bridge Pattern is particularly applicable. * When a class has two independent dimensions of variation, and both dimensions need to be extended, use the Bridge Pattern. ### Notes * The Bridge Pattern is suitable for two independent dimensions of variation, ensuring they can be extended and varied independently. ### Structure Here are the key roles of the Bridge Pattern: * **Abstraction**: Defines the abstraction interface, usually containing a reference to the implementation interface. * **Refined Abstraction**: An extension of the abstraction, which can be a subclass of the abstraction class or a concrete implementation class. * **Implementor**: Defines the implementation interface, providing an interface for basic operations. * **Concrete Implementor**: A concrete class that implements the implementation interface. ## Implementation We have a _DrawAPI_ interface as the bridge implementation and concrete classes _RedCircle_, _GreenCircle_ that implement the _DrawAPI_ interface. _Shape_ is an abstraction class that will use objects of _DrawAPI_. The _BridgePatternDemo_ class uses the _Shape_ class to draw circles of different colors. ![Image 2: UML Diagram of Bridge Pattern](#) ### Step 1 Create the bridge implementation interface. ## DrawAPI.java public interface DrawAPI{public void drawCircle(int radius, int x, int y); } ### Step 2 Create concrete bridge implementation classes that implement the _DrawAPI_ interface. ## RedCircle.java public class RedCircle implements DrawAPI{ @Override public void drawCircle(int radius, int x, int y){System.out.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.println("Drawing Circle[ color: green, radius: " + radius +", x: " +x+", "+ y +"]"); }} ### Step 3 Create the abstraction class _Shape_ using the _DrawAPI_ interface. ## Shape.java public abstract class Shape{protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI){this.drawAPI = drawAPI; }public abstract void draw(); } ### Step 4 Create concrete classes that implement the _Shape_ abstraction class. ## Circle.java public class Circle extends Shape{private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI){super(drawAPI); this.x = x; this.y = y; this.radius = radius; }public void draw(){drawAPI.drawCircle(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 RedCircle()); Shape greenCircle = new Circle(100,100, 10, new GreenCircle()); redCircle.draw(); greenCircle.draw(); }} ### Step 6 Execute the program, and the output will be: Drawing Circle[ color: red, radius: 10, x: 100, 100]Drawing Circle[ color: green, radius: 10, x: 100, 100] * * * ## Related Articles Recommended > (#)
← Filter PatternAdapter Pattern β†’