What are differences between Bridge Pattern and Dependency Injection?
For the both patterns we have an abstract class with implementation of another abstraction. There is the Bridge Pattern UML diagramm below.
Difference between Dependency Injection and Factory PatternIt requires both a dependent object and a factory object to work properly. DI makes the unit tests easier. It does not require boilerplate code. The factory pattern requires the object you want to test, the factory object, and the dependent object.
A Bridge pattern can only be implemented before the application is designed. Allows an abstraction and implementation to change independently whereas an Adapter pattern makes it possible for incompatible classes to work together.
Strategy Pattern is used for Behavioural decisions, while Bridge Pattern is used for Structural decisions. Brigde Pattern separats the abstract elements from the implementation details, while Strategy Pattern is concerned making algorithms more interchangeable.
AFAIK Dependency Injection is not a design pattern but a design guideline defined in the SOLID principles.
So Bridge pattern uses dependency injection in it to achieve the required polymorphic behavior where the DrawingAPI
is being injected in the constructor to decouple the Shape
from the concrete implementation of API.
Snippet from the example of Bridge Pattern of Wikipedia
protected Shape(DrawingAPI drawingAPI){
this.drawingAPI = drawingAPI;
}
Bridge Pattern - A design pattern
Dependency Injection - Design guideline or principle
You can do Dependency Injection through several mechanisms. The Bridge mechanism is only one of them. Simple interface implementation is another. Class-weaving and other dynamic tricks yet another.
Dependency Injection is a development/design technique, but not a pattern since it can be implemented in several ways.
Thinking a bit more about this, you could consider Dependency Injection a Software Architecture Pattern (but still not a design one), in the sense that it's a common way of addressing a series of Architectural concerns (testability, configurability, modularity, etc).
In other words, Dependency Injection could effectively be considered a Pattern, but in a different level: Architecture, not Design.
Many Design Patterns have similar UML diagrams.
The Bridge Pattern is completely different than Dependency Injection.
Dependency Injection - A way to easily insert (and swap) dependencies in code either at runtime or compile time.
Bridge Pattern - A way to have an extra interface between different systems. The Bridge is the communication layer between your code and the other system. For example, the two most used Bridge Pattern implementations in Java are JDBC (which communicates with a database via a Driver Bridge) and Swing (which uses a Bridge to communicate with the Operating System's UI). This lets the other system get swapped out or changed without affecting or changes the communication layer to your system.
EDIT: Forgot to mention that a Bridge also allows both sides on the bridge to evolve and change independently without affecting the other. This is because the Bridge isolates both sides from each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With