The bridge pattern

By | March 21, 2018

The bridge pattern is a design pattern used in software engineering that is meant to “decouple an abstraction from its implementation so that the two can vary independently”,
– Wikipedia

The bridge pattern is one of my favorite patterns to use as it is so elegant. As the description above states it allows the abstraction and implementation to be separated so they con evolve independently… now what does that mean?

If you look bridge pattern up online you will find something like this… which I always find to be completely meaningless.

A more practical example of a bridge pattern will be the example below. And now things might make more sense…

The example above shows two abstract concepts “content” and “documentwriter” that each implement a template method pattern in the form of an abstract class. The clear diamond indicates that there is aggregation between them.

By separating the data type (content) from the export format (Documentwriter) we have allowed the two concepts to evolve separately. If a new file types comes out it will never ever affect the content and the same is valid the other way around.

The content (implementer) will make use of the template defined in documentwriter (abstraction) to Export itself. The export method takes the class type to be used to achieve the export in the format that the user requires.

 

Leave a Reply

Your email address will not be published.