Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Bridge Design Pattern

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

STRUCTURAL DESIGN PATTERN

The Structural Design Patterns deal with different ways to create a class structure. Moreover, they use
Inheritance and Composition techniques to create larger objects from small objects. 
When we have interface hierarchies in both interfaces as well as implementations, then the BRIDGE design pattern is used to
decouple the interfaces from implementation and hiding the implementation details from the client programs.
According to the GoF bridge design pattern is: Decouple an abstraction from its implementation so that the two can vary
independently.
In the Bridge Pattern, we separate an abstraction and its implementation and develop separate inheritance structures for both
the abstraction and the implementer. The abstraction is an interface or an abstract class, and likewise the implementer is an
interface or an abstract class. The abstraction contains a reference to the implementer. Children of the abstraction are referred to
as refined abstractions, and children of the implementer are concrete implementers. Since we can change the reference to the
implementer in the abstraction, we are able to change the abstraction’s implementer at run-time. However, changes to the
implementer do not affect client code.
The Bridge Pattern’s intent is to put the abstraction and implementation into two different class hierarchies so that both can be
extend independently.
Structural design patterns are concerned with how classes and objects can be composed, to form larger structures.
The structural design patterns simplifies the structure by identifying the relationships.
These patterns focus on, how the classes inherit from each other and how they are composed from other classes.
A Bridge Pattern says that just "decouple the functional abstraction from the implementation so that the two can vary
independently".
The Bridge Pattern is also known as Handle or Body.

Advantage of Bridge Pattern


It enables the separation of implementation from the interface.
It improves the extensibility.
It allows the hiding of implementation details from the client.
Usage of Bridge Pattern
•When you don't want a permanent binding between the functional abstraction and its implementation.
•When both the functional abstraction and its implementation need to extended using sub-classes.
•It is mostly used in those places where changes are made in the implementation does not affect the clients.
•It enables the separation of implementation from the interface.
•It improves the extensibility.
•It allows the hiding of implementation details from the client.
Bridge pattern decouples an definition from its implementation. It is a structural pattern.
This pattern involves an interface which acts as a bridge. The bridge makes the concrete classes independent from interface implementer
classes. 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.
Both types of classes can be altered without affecting each other.
Example
interface Printer {
public void print(int radius, int x, int y);
class ColorPrinter implements Printer {
@Override
public void print(int radius, int x, int y) {
System.out.println("Color: " + radius +", x: " +x+", "+ y +"]");
}
}
class BlackPrinter implements Printer {
@Override
public void print(int radius, int x, int y) {
System.out.println("Black: " + radius +", x: " +x+", "+ y +"]");
}
}
abstract class Shape {
protected Printer print;
protected Shape(Printer p){
this.print = p;
}
public abstract void draw();
}
class Circle extends Shape {
private int x, y, radius;

public Circle(int x, int y, int radius, Printer draw) {


super(draw);
this.x = x;
this.y = y;
this.radius = radius;
}

public void draw() {


print.print(radius,x,y);
}
}
public class Main {
public static void main(String[] args) {
Shape redCircle = new Circle(100,100, 10, new ColorPrinter());
Shape blackCircle = new Circle(100,100, 10, new BlackPrinter());

redCircle.draw();
blackCircle.draw();
}
}
The code above generates the following result.
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.
DrawAPI.java
public interface DrawAPI
{
public void drawCircle(int radius, int x, int y);
}
Step 2
Create concrete bridge implementer classes implementing 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 an abstract 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 class implementing 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)
{
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 different colored circles.
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
Verify the output.
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]

You might also like