0% found this document useful (0 votes)
28 views4 pages

Bridge Pattern in Java Example

The bridge pattern decouples an abstraction from its implementation so that they can vary independently. It involves a bridge interface that makes concrete classes independent from interface implementer classes. Both types of classes can be altered without affecting each other. The example demonstrates drawing circles in different colors using the same abstract class method but different bridge implementer classes.

Uploaded by

ikramusersa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Bridge Pattern in Java Example

The bridge pattern decouples an abstraction from its implementation so that they can vary independently. It involves a bridge interface that makes concrete classes independent from interface implementer classes. Both types of classes can be altered without affecting each other. The example demonstrates drawing circles in different colors using the same abstract class method but different bridge implementer classes.

Uploaded by

ikramusersa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

07/12/2023 13:55 Design Patterns - Bridge Pattern

Design Patterns - Bridge Pattern

Bridge is used when we need to decouple an abstraction from its implementation so


that the two can vary independently. This type of design pattern comes under
structural pattern as this pattern decouples implementation class and abstract class
by providing a bridge structure between them.

This pattern involves an interface which acts as a bridge which makes the
functionality of concrete classes independent from interface implementer classes.
Both types of classes can be altered structurally without affecting each other.

We are demonstrating use of Bridge pattern via following example in which a circle
can be drawn in different colors using same abstract class method but different
bridge implementer classes.

Implementation
We have a DrawAPI interface which is acting as a bridge implementer and concrete
classes RedCircle, GreenCircle implementing the DrawAPI interface. Shape is an
abstract class and will use object of DrawAPI. BridgePatternDemo, our demo class
will use Shape class to draw different colored circle.

Step 1
Create bridge implementer interface.

[Link]

[Link] 1/4
07/12/2023 13:55 Design Patterns - Bridge Pattern

public interface DrawAPI {


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

Step 2
Create concrete bridge implementer classes implementing the DrawAPI interface.

[Link]

public class RedCircle implements DrawAPI {


@Override
public void drawCircle(int radius, int x, int y) {
[Link]("Drawing Circle[ color: red, radius: " + radius + ",
}
}

[Link]

public class GreenCircle implements DrawAPI {


@Override
public void drawCircle(int radius, int x, int y) {
[Link]("Drawing Circle[ color: green, radius: " + radius +
}
}

Step 3
Create an abstract class Shape using the DrawAPI interface.

[Link]

public abstract class Shape {


protected DrawAPI drawAPI;

protected Shape(DrawAPI drawAPI){


[Link] = drawAPI;
}

[Link] 2/4
07/12/2023 13:55 Design Patterns - Bridge Pattern

public abstract void draw();


}

Step 4
Create concrete class implementing the Shape interface.

[Link]

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;
[Link] = radius;
}

public void draw() {


[Link](radius,x,y);
}
}

Step 5
Use the Shape and DrawAPI classes to draw different colored circles.

[Link]

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());

[Link]();
[Link]();
}
}

[Link] 3/4
07/12/2023 13:55 Design Patterns - Bridge Pattern

Step 6
Verify the output.

Drawing Circle[ color: red, radius: 10, x: 100, 100]


Drawing Circle[ color: green, radius: 10, x: 100, 100]

[Link] 4/4

Common questions

Powered by AI

The Bridge pattern facilitates structural changes by using an abstraction that holds a reference to the interface implementer. This bridge structure allows the abstraction to operate independently of the implementer's details. When the abstraction or any variant of it acts on the interface, it can interact with multiple concrete implementer classes without being hard-coded to any particular one. Thus, changes in the abstraction do not affect the implementers and vice versa, allowing each to evolve separately .

Changing the `DrawAPI` implementations affects the system design by enhancing its flexibility and adaptability without altering the abstraction layer. New features or optimizations can be introduced in how shapes are drawn by simply adding or modifying implementer classes such as `RedCircle` or `GreenCircle`, or by creating other colors or types. This change does not affect the `Shape` abstraction or its subclasses, because the interaction between them relies on the interface contract, not the specific implementer classes, allowing for seamless integration and evolution of drawing features .

The Bridge pattern aligns with the Open/Closed Principle by enabling the system to be open for extension but closed for modification. This is achieved by allowing new implementations of the `DrawAPI` or new abstractions of `Shape` to be added without altering existing code. Developers can introduce new concrete classes for drawing or additional shape types as extensions, thus enriching the system's capabilities without modifying the abstraction structure or already existing implementation details, preserving stability and reducing the risk of introducing errors .

In the Bridge pattern example, the `DrawAPI` interface plays the structural role of defining the contract for the concrete implementer classes (`RedCircle`, `GreenCircle`). It serves as the bridge between the abstract class (`Shape`) and its concrete implementations, outlining the method signature for `drawCircle`. This enables the `Shape` class to delegate the drawing responsibilities to any class that implements `DrawAPI`, thereby decoupling the shape abstraction from the specifics of the drawing implementation .

The `Shape` abstract class embodies the principles of the Bridge pattern by holding a reference to a `DrawAPI` interface, which serves as the bridge implementer. This design allows the `Shape` class and its subclasses to focus solely on the abstraction of shapes, delegating the implementation-specific details, such as drawing a circle in a particular color, to different `DrawAPI` implementers (`RedCircle`, `GreenCircle`). As a result, both the shape abstraction and drawing implementation can be extended or modified independently, reflecting the core idea of the Bridge pattern to separate abstraction from implementation .

The Bridge and Adapter patterns differ mainly in their design intent. The Bridge pattern is focused on decoupling an abstraction from its implementation so that both can vary independently, typically used from the start in a design. In contrast, the Adapter pattern makes existing interfaces compatible with another by acting as a wrapper, usually employed to integrate a class into a system it wasn't designed to fit into initially. While the Bridge provides a solution for future flexibility in system evolution, the Adapter often addresses compatibility issues with legacy code or external libraries .

The potential benefits of using the Bridge design pattern in complex software systems include increased flexibility and scalability, as it allows for independent extension of abstractions and implementations. It simplifies the code structure by decoupling interface and implementation, making the system easier to understand and modify. Furthermore, it promotes reusability and adherence to the Open/Closed Principle, as new functionality can be added with minimal impact on existing code .

The Bridge design pattern is considered a structural pattern because it focuses on decoupling the abstraction and implementation so that the two can vary independently, achieving a design structure that is flexible and easy to manage. This structural separation allows the developer to change and extend the implementations and abstractions without affecting each other, promoting a better organized and scalable codebase .

The main objective of the Bridge design pattern is to decouple an abstraction from its implementation so that the two can vary independently. This is achieved by creating an interface that acts as a bridge, allowing the functionality of concrete classes to become independent from the interface implementer classes. As a result, both types of classes can be changed structurally without affecting each other .

In the provided example, the Bridge pattern is implemented using the `DrawAPI` interface as the bridge implementer. Concrete classes `RedCircle` and `GreenCircle` implement this interface to draw circles with different colors. The abstract class `Shape` contains a reference to an object of type `DrawAPI` and is used by its subclass `Circle`. When a `Circle` is created, it is provided with a concrete implementer (`RedCircle` or `GreenCircle`). The `draw()` method in `Circle` calls `drawCircle()` on the `DrawAPI`, resulting in a circle being drawn with the specified color, without the `Circle` class depending directly on how the drawing occurs .

You might also like