-
Bridge Design PatternModeling/DesignPattern 2019. 8. 19. 23:05
1. Overview
Bridge belongs to Structural patterns. This pattern allows us to vary independently between abstraction and implementations. but if Bridge has to be designed in advance, then only we can have varying abstractions & implementations. it has two parts.
- Abstraction
- Implementation
- Abstraction – the core of the bridge design pattern and defines the crux. Contains a reference to the implementer using composition.
- Refined Abstraction – Extends the abstraction takes the finer detail one level below. Hides the finer elements from implementors.
- Implementor – It defines the interface for implementation classes. This interface does not need to correspond directly to the abstraction interface and can be very different. Abstraction imp provides an implementation in terms of operations provided by the Implementer interface.
- Concrete Implementor – Implements the above implementor by providing a concrete implementation.
2. Features
- Allows the Abstraction and Implementation to be developed independently and the client code can access only the Abstraction part without being concerned about the Implementation part.
- The abstraction is an interface or abstract class and the implementor is also an interface or abstract class.
- The abstraction contains a reference to the implementor. Children of the abstraction are referred to as refined abstractions, and children of the implementors are concrete implementors. Since we can change the reference to the implementor in abstraction, we are able to change the abstraction's implementor at run-time. Changes to the implementor do not affect client code.
- It increases the loose coupling between class abstraction and it's implementation.
3. Pitfalls
- Complex to understand & implement this pattern
- Needs to have a well thought out & fairly comprehensive design in front of you before you can decide on bridge pattern
- Needs to be designed in advanced. Adding Bridge to legacy code is difficult. Even for the ongoing project adding bridge at a later time in development may require a fair amount of rework.
4. Usages
// Java code to demonstrate // bridge design pattern // abstraction in bridge pattern abstract class Vehicle { protected Workshop workShop1; protected Workshop workShop2; protected Vehicle(Workshop workShop1, Workshop workShop2) { this.workShop1 = workShop1; this.workShop2 = workShop2; } abstract public void manufacture(); } // Refine abstraction 1 in bridge pattern class Car extends Vehicle { public Car(Workshop workShop1, Workshop workShop2) { super(workShop1, workShop2); } @Override public void manufacture() { System.out.print("Car "); workShop1.work(); workShop2.work(); } } // Refine abstraction 2 in bridge pattern class Bike extends Vehicle { public Bike(Workshop workShop1, Workshop workShop2) { super(workShop1, workShop2); } @Override public void manufacture() { System.out.print("Bike "); workShop1.work(); workShop2.work(); } } // Implementor for bridge pattern interface Workshop { abstract public void work(); } // Concrete implementation 1 for bridge pattern class Produce implements Workshop { @Override public void work() { System.out.print("Produced"); } } // Concrete implementation 2 for bridge pattern class Assemble implements Workshop { @Override public void work() { System.out.print(" And"); System.out.println(" Assembled."); } } // Demonstration of bridge design pattern class BridgePattern { public static void main(String[] args) { Vehicle vehicle1 = new Car(new Produce(), new Assemble()); vehicle1.manufacture(); Vehicle vehicle2 = new Bike(new Produce(), new Assemble()); vehicle2.manufacture(); } }
5. References
'Modeling > DesignPattern' 카테고리의 다른 글
Model–view–controller (MVC) (0) 2020.02.23 Categorizing Design Pattern (0) 2019.09.29 Abstract factory (0) 2019.08.23 Factory Pattern (0) 2019.08.22 Simple Factory Pattern (0) 2019.08.22