design-patterns/structural/bridge/README.md
Ruidy 11b03fee22
Bridge (#8)
* bridge example

* edit gitignore
2020-09-17 17:45:18 +02:00

2.7 KiB
Raw Permalink Blame History

Bridge

Bridge is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently.

Summary

Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.

Problem

Say you have a geometric Shape class with a pair of subclasses: Circle and Square. You want to extend this class hierarchy to incorporate colors, so you plan to create Red and Blue shape subclasses. However, since you already have two subclasses, youll need to create four class combinations such as BlueCircle and RedSquare.

Adding new shape types and colors to the hierarchy will grow it exponentially. For example, to add a triangle shape youd need to introduce two subclasses, one for each color. And after that, adding a new color would require creating three subclasses, one for each shape type. The further we go, the worse it becomes.

Solution

This problem occurs because were trying to extend the shape classes in two independent dimensions: by form and by color. Thats a very common issue with class inheritance.

The Bridge pattern attempts to solve this problem by switching from inheritance to the object composition. What this means is that you extract one of the dimensions into a separate class hierarchy, so that the original classes will reference an object of the new hierarchy, instead of having all of its state and behaviors within one class.

How to Implement

  1. Identify the orthogonal dimensions in your classes. These independent concepts could be: abstraction/platform, domain/infrastructure, front-end/back-end, or interface/implementation.

  2. See what operations the client needs and define them in the base abstraction class.

  3. Determine the operations available on all platforms. Declare the ones that the abstraction needs in the general implementation interface.

  4. For all platforms in your domain create concrete implementation classes, but make sure they all follow the implementation interface.

  5. Inside the abstraction class, add a reference field for the implementation type. The abstraction delegates most of the work to the implementation object thats referenced in that field.

  6. If you have several variants of high-level logic, create refined abstractions for each variant by extending the base abstraction class.

  7. The client code should pass an implementation object to the abstractions constructor to associate one with the other. After that, the client can forget about the implementation and work only with the abstraction object.