Please enable JavaScript.
Coggle requires JavaScript to display documents.
Bridge (Key Objects (Refined Abstraction - Extends the interface defined…
Bridge
Key Objects
-
Implementor - defines interface for implementation classes. Usually primitive operations, abstraction contains higher level operations
-
-
Applicability
-
-
You want to avoid a permanent coupling between the abstract class and its implementation. For e.g. we may want to allow the implementation to selected or set a run time
Consequences
-
Shields Client from implementation - Hides implementation details form clients such as sharing of implementation objects
Decoupling interface and implementation - promotes loose coupling of the two. Allows for implementation to be changed at runtime without recompiling abstraction
Motivation
Take the example of a TV Remote. You can create an abstraction for it so you can implement different remotes for different tvs. However, if you need to vary the remote abstraction based on the TV functionality, you are stuck since you have a single abstraction. Instead have two separate abstractions, one for the TV and one for the remote. Make the TV abstraction an instance of the Remote abstraction. This allows you to change the TV instance at runtime, allowing for loose coupling between the two.
-
The abstraction contains a reference to the implementor. Children of the abstraction are referred to as refined abstractions, and children of the implementor are concrete implementors. Since we can change the reference to the implementor in the abstraction, we are able to change the abstraction’s implementor at run-time. Changes to the implementor do not affect client code.
The abstraction is an interface or abstract class and the implementor is also an interface or abstract class.
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.
-
Implementation
Only one implementor If there is only one implementor, then a concrete implementor can be used instead of using an implementor
Creating the right implementor If abstraction knows about concrete implementors, it can create them itself. If not a factor can be used
-
-
-
-