Master Software Architecture & Clean Code!
Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!
SUBSCRIBE NOWUnderstanding the Bridge Pattern Structure
The Bridge Design Pattern decouples an abstraction from its implementation so that both can vary independently. In object-oriented modeling, understanding how these two hierarchies connect via UML class diagrams is crucial for building maintainable, loosely coupled architectures.
Rather than compounding dimensions into a single inheritance tree, the Bridge pattern splits the architecture into two parallel hierarchies connected by an aggregation/composition reference—the "Bridge".
Key Class Diagram Components
- Abstraction: Defines the high-level control interface and maintains a reference to an object of type Implementor.
- Refined Abstraction: Extends the base Abstraction to provide variant business logic while delegating core primitive work to the Implementor.
- Implementor (Interface): Declares the low-level, primitive operations common to all concrete implementations.
- Concrete Implementor: Implements the Implementor interface with platform- or vendor-specific execution logic.
UML Representation & Code Relationship
Here is a clean Java representation reflecting the exact structural relationship shown in the UML class diagram:
// Implementor Interface
interface Implementor {
void implementationOperation();
}
// Concrete Implementors
class ConcreteImplementorA implements Implementor {
@Override
public void implementationOperation() {
System.out.println("Executing ConcreteImplementorA operation.");
}
}
class ConcreteImplementorB implements Implementor {
@Override
public void implementationOperation() {
System.out.println("Executing ConcreteImplementorB operation.");
}
}
// Abstraction (Bridge Connection)
abstract class Abstraction {
protected Implementor implementor; // Composition bridge
public Abstraction(Implementor implementor) {
this.implementor = implementor;
}
public abstract void operation();
}
// Refined Abstraction
class RefinedAbstraction extends Abstraction {
public RefinedAbstraction(Implementor implementor) {
super(implementor);
}
@Override
public void operation() {
System.out.print("RefinedAbstraction delegating: ");
implementor.implementationOperation();
}
}
// Client Code
public class BridgeClassDiagramDemo {
public static void main(String[] args) {
Abstraction itemA = new RefinedAbstraction(new ConcreteImplementorA());
itemA.operation();
Abstraction itemB = new RefinedAbstraction(new ConcreteImplementorB());
itemB.operation();
}
}
Watch More Related Design Pattern Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=akKwII8fjyI
Click the below Image to Enlarge
| Bridge Design pattern - Class Diagram |
No comments:
Post a Comment