Master Software Architecture & Clean Code!
Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!
SUBSCRIBE NOWWhat is the Bridge Design Pattern?
The Bridge Design Pattern is a structural design pattern defined by the Gang of Four (GoF) that lets you decouple an abstraction from its implementation so that both can vary independently. In object-oriented programming, inheritance often binds classes too tightly at compile time, leading to rigid architectures and class explosion.
Instead of relying on deep class hierarchies, the Bridge pattern adopts the principle of composition over inheritance. It divides the problem into two distinct dimensions—the high-level control logic (abstraction) and the low-level platform-specific execution (implementation)—and connects them via an object reference.
The Core Problem: Class Explosion
Consider an application that manages geometric shapes (Circles, Squares) rendered across different platforms (Vector, Raster):
- Inheritance Approach: You would need classes like
VectorCircle,RasterCircle,VectorSquare, andRasterSquare. Adding one shape and one renderer requires multiplying class combinations exponentially. - Bridge Pattern Approach: Shapes become the abstraction hierarchy, while Renderers become the implementor interface. Adding a new shape or renderer only requires adding one standalone class.
Anatomy of the Bridge Pattern
- Abstraction: High-level interface containing business logic and a reference to the Implementor.
- Refined Abstraction: Extends or customizes the Abstraction for specific use cases.
- Implementor: Declares the common primitive operations that concrete platforms or engines must provide.
- Concrete Implementors: Platform-specific implementations of the Implementor interface.
Watch More Related Design Pattern Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=GPNVN4TnvMU
Click the below Image to Enlarge
| Bridge Design pattern - Introduction |
No comments:
Post a Comment