Introduction :
A structural design pattern called the Bridge pattern separates an abstraction from its implementation so that each can change on its own. It is employed when it is necessary to prevent an abstraction and its implementation from being permanently bound, allowing for the independent development of the two.
The Bridge design pattern allows you to maintain separation between the abstraction and implementation. The pattern is one of structural design.
Bridge design pattern is divided into two parts:
- Abstraction
- Implementation
An implementation class is contained inside an interface class thanks to this design technique.
The client code can access only the Abstraction component without worrying about the Implementation part thanks to the bridge pattern, which permits the Abstraction and Implementation to be written independently.
An interface or abstract class is the abstraction, and an interface or abstract class is also the implementer.
There is a reference to the implementer in the abstraction. Concrete implementers are the offspring of the implementer, and refined abstractions are the offspring of the implementer. We are able to alter the implementer of the abstraction at run-time because we can modify the reference to the implementer within the abstraction. Client code is unaffected by changes made to the implementer.
It strengthens the loose coupling that exists between the implementation and the class abstraction.
The bridge design pattern is composed of the participants listed below.
Abstraction (abstract class): It specified the behavior portion of the abstract interface. It keeps the Implementer reference as well.
The interface established by Abstraction is extended by RefinedAbstraction (normal class).
Implementer (interface): It establishes the implementation classes’ interface. This interface can differ greatly from the abstraction interface and does not necessarily need to match it exactly. Implementer offers an implementation based on the functions that the Implementer interface exposes.
The Implementer interface is implemented by the ConcreteImplementor (normal class).
Application of the Bridge Pattern
When the functional concept and its implementation don’t need to be permanently bound.
when employing sub-classes to extend both the functional concept and its implementation is necessary.
It is primarily utilized in settings where clientele is unaffected by implementation modifications.
Advantages
To allow for independent variation, the bridge pattern decouples an abstraction from its implementation.
Mainly, platform independence features are implemented with its help.
In order to acomplish this, it adds one more method level redirection.
Provide the implementation in a subhierarchical inheritance structure and publish the abstraction interface in a different one.
Implementation run-time binding can be achieved by using the bridge pattern.
Orthogonal class hierarchies can be mapped using the bridge pattern
The design of Bridge is such that the implementation and the abstraction can change separately from one another.
When to Apply the Bridge Pattern
When you wish to keep an abstraction and its implementation from being permanently bound.
When it makes sense to subclass in order to extend both the abstractions and their implementations.
When client code shouldn’t be impacted by implementation changes.
when integrating several characteristics of variability results in an abundance of classes.
Example in Java
Consider the following example: we have several shapes (abstraction) that may be rendered in various colors (implementation).
Step 1: Define the Implementor interface
// Implementor interface
interface MyColor {
void applyColor();
}
Step 2: Implement the Implementor interface with ConcreteImplementor
// Concrete Implementor 1
class RedColor implements MyColor {
@Override
public void applyColor() {
System.out.println(“Applying red color.”);
}
}
// Concrete Implementor 2
class GreenColor implements MyColor {
@Override
public void applyColor() {
System.out.println(“Applying green color.”);
}
}
Step 3: Define the Abstraction class
// Abstraction
abstract class Shape {
protected MyColor myColor;
// Constructor
public Shape(MyColor myColor) {
this.myColor = myColor;
}
abstract void draw();
}
Step 4: Implement the Refined Abstraction classes
// Refined Abstraction 1
class Circle extends Shape {
public Circle(MyColor myColor) {
super(myColor);
}
@Override
void draw() {
System.out.print(“Drawing Circle in “);
myColor.applyColor();
}
}
// Refined Abstraction 2
class Rectangle extends Shape {
public Rectangle(MyColor myColor) {
super(myColor);
}
@Override
void draw() {
System.out.print(“Drawing Rectangle in “);
myColor.applyColor();
}
}
Step 5: Use the Bridge Pattern
public class BridgePatternDemo {
public static void main(String[] args) {
Shape redCircle = new Circle(new RedColor());
Shape greenRectangle = new Rectangle(new GreenColor());
redCircle.draw(); // Output: Drawing Circle in Applying red color.
greenRectangle.draw(); // Output: Drawing Rectangle in Applying green color.
}
}
How It Operates
The Shape class is the abstraction that has a reference to the MyColor implementor in it.
Rectangle and Circle are sophisticated abstractions that can be expanded without regard to myColor.
The concrete implementors RedColor and GreenColor specify the particular behavior of applying myColor.
The ability to alter the myColor and form separately in this example shows how the Bridge pattern encourages scalability and flexibility in software architecture.
In brief
An abstraction and its implementation are separated by a bridge pattern, allowing the two to change separately.
Its primary purpose is to implement features related to platform independence.
To accomplish the goal, it adds one more redirection at the method level.
Publish the abstraction interface in a distinct inheritance hierarchy and place the implementation in its own inheritance hierarchy.
Utilize the bridge pattern to bind the implementation at run-time.
To map orthogonal class hierarchies, use the bridge pattern.
The bridge is intentionally constructed such that the implementation and the concept can change separately.
For more information & classes Call: 2048553004
Registration Link: Click Here!
Author: Ambarish Durani
JAVA Trainer
IT Education Centre Placement & Training Institute
© Copyright 2024 | IT Education Centre.