Sitemap

Member-only story

Design Patterns in Go: Bridge

4 min readOct 26, 2024

Structural patterns focus on the organization of code, components, or interfaces. They address issues like managing object relationships, providing abstraction from underlying implementations, or integrating different libraries and frameworks into a cohesive system.

OK, Let’s go through the description of the 2nd structural pattern — Bridge.

Context:
You have an abstraction with multiple implementations. The abstraction and implementations are tightly coupled together through inheritance hierarchies. This leads to issues with rigidity, difficulty extending the abstraction or implementations independently, and being unable to swap implementations at runtime.

One typical example is to add Color into existing Shape hierarchy, it couldbe depicted as following:

There's the Shape abstraction, which is extended or implemented by Circle and Square. Well, now we want to render them in different colors, like red and blue. One solution is adjusting the Shape hierarchy to add more subclasses, Circle{Red,Blue}, Square{Red,Blue}.

If the problem scales, like adding kinds of materials, the number of subclasses will…

--

--

ZhangJie (Kn)
ZhangJie (Kn)

Responses (1)