Bridge diagram against dependency injection

What are the differences between bridge pattern and dependency injection?

For both patterns, we have an abstract class with a different abstraction. The following is a diagram of the Bridge UML template.

enter image description here

+6
source share
4 answers

AFAIK Dependency Injection is not a design pattern, but a design guide defined in SOLID principles .

Thus, the bridge pattern uses dependency injection in it to achieve the desired polymorphic behavior when the DrawingAPI is injected into the constructor to separate the Shape from the concrete API implementation.

Fragment from the example Wikipedia Bridge template

 protected Shape(DrawingAPI drawingAPI){ this.drawingAPI = drawingAPI; } 

Bridge Template - Design Template

Dependency Injection - Design Guide or Principle

+7
source

You can inject dependencies through several mechanisms. The mechanism of the bridge is just one of them. A simple interface implementation is another. Weaving class and other dynamic tricks are another one.

Injection Dependency is a design / development technology, but not a template, because it can be implemented in several ways.

After thinking a little bit more about it, you might think that dependency injection is a software architecture template (but still not constructive), in the sense that it is the usual way to solve a number of architectural problems (testability, configurability, modularity, etc.).

In other words, Injection Dependency could be effectively seen as a Pattern, but on a different level: Architecture, not Design.

+4
source

Many design patterns have similar UML diagrams.

The bridge pattern is completely different from Injection Dependency.

Injection Dependency is a way to easily insert (and change) dependencies in code, either at run time or at compile time.

Bridge Pattern is a way to have an additional interface between different systems. A bridge is the level of communication between your code and another system. For example, the two most used Bridge Pattern implementations in Java are JDBC (which communicates with the database through Bridge Bridge) and Swing (which uses the bridge to interact with the operating system user interface). This allows another system to receive or replace another system without affecting or changing the level of communication in your system.

EDIT: Recall that the bridge also allows both sides of the bridge to evolve and change independently without affecting the other. This is due to the fact that the bridge isolates both sides of each other.

+3
source

The Bridge template uses "Dependency Inversion" to make the bridge work, i.e. The base class / abstraction interface depends on the developer interface.

Dependency injection is the most commonly used implementation of the Dependency Inversion Principle.

+1
source

Source: https://habr.com/ru/post/952852/


All Articles