Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bridge Pattern vs Dependency Injection

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.

enter image description here

like image 803
Warlock Avatar asked Aug 30 '13 20:08

Warlock


People also ask

What is the difference between dependency injection and factory pattern?

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.

What is the difference between Bridge pattern and adapter pattern?

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.

What is the difference between strategy and Bridge pattern?

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.


3 Answers

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

like image 189
Narendra Pathai Avatar answered Nov 15 '22 13:11

Narendra Pathai


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.

like image 24
Mario Rossi Avatar answered Nov 15 '22 12:11

Mario Rossi


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.

like image 33
dkatzel Avatar answered Nov 15 '22 12:11

dkatzel