Bridge Design Pattern
- Dhruv Kudalkar, Bhavya Gor & Ishaan Shah
Bridge Design Pattern
Ishaan
shah
What is the Bridge Design Pattern?
● The Bridge Design Pattern is a structural design pattern that decouples an
abstraction from its implementation so that the two can vary independently.
● It is used when there is a need to avoid a permanent binding between an
abstraction and its implementation and when changes in the implementation of
an abstraction should not affect clients that use it.
● The bridge pattern allows the Abstraction and the Implementation to be
developed independently and the client code can access only the Abstraction
part without being concerned about the Implementation part.
● It applies the Single Responsibility and the Open-Closed SOLID Principles.
Components of the Bridge Design Pattern
● Abstraction: This defines the abstraction's interface and maintains a
reference to an object of type Implementor. It represents the "what"
of the abstraction, defining the high-level logic.
● Refined Abstraction: This extends the abstraction, providing
variations on operation implementation.
● Implementor: This is an interface that defines the methods that
concrete implementations must follow. It represents the "how" of
the abstraction.
● Concrete Implementor: This implements the Implementor interface
and defines concrete implementations for the interface methods.
UML Diagram
Example
● Abstraction: Restaurant
● Refined Abstractions: ItalianRestaurant, AmericanRestaurant
● Implementor: PizzaAPI
● Concrete Implementors: MargheritaPizza, PepperoniPizza
● Abstraction: Restaurant defines methods like orderPizza() and maintains a reference
to the PizzaAPI interface.
● Refined Abstractions: ItalianRestaurant and AmericanRestaurant extend the
Restaurant abstraction and provide specific implementations of the orderPizza()
method tailored to their cuisine styles.
● Implementor: PizzaAPI interface declares methods like prepare() and bake() without
specifying how they are implemented.
● Concrete Implementors: MargheritaPizza and PepperoniPizza provide concrete
implementations of the PizzaAPI interface for preparing and baking Margherita and
Pepperoni pizzas, respectively.
Benefits and Drawbacks
Benefits:
● Decoupling: It separates the abstraction and its implementation, allowing
them to vary independently.
● Flexibility: It provides flexibility in changing both abstraction and
implementation separately.
● Simplifies Abstraction: It allows the abstraction and implementation to evolve
independently without requiring changes to each other.
Drawbacks:
● Complexity: Introducing the Bridge pattern can add complexity, especially for
small projects or when there is a single implementation of an abstraction.
● Increased Number of Classes: It may lead to an increase in the number of
classes in the codebase, which can make it harder to manage and understand.
Any Questions?
THANK YOU!