mirror of
https://github.com/tmrts/go-patterns.git
synced 2025-04-11 21:10:55 +00:00
1.9 KiB
1.9 KiB
Bridge Pattern
The bridge pattern allows you to "decouple an abstraction from its implementation so that the two can vary independently". It does so by creating two hierarchies: Abstraction and Implementation.
Abstraction | Implementation
Hierarchy | Hierarchy
|
------------- | ------------------
| Abstraction | | imp | <Implementor> |
|-------------| ----|------> |------------------|
| + imp | | | implementation() |
------------- | ------------------
| ^
| |
| ---------------------
| | ConcreteImplementor |
| |---------------------|
| | implementation() |
| ---------------------
Note: In the literature, the Abstraction
class is commonly represented as an "Abstract Class", meaning, children should be defined to instantiate it. Since Go does not explicitly support inheritance (and it has good reasons), that part was simplified by a concrete class modeled as a Struct.
Implementation
// Abstraction represents the concretion of the abstraction hierarchy of the bridge
type Abstraction struct {
imp Implementor
}
// Implementor represents the abstraction of the implementation hierarchy of the bridge
type Implementor interface {
implementation()
}
// ConcreteImplementor implements Implementor
type ConcreteImplementor struct{}
func (c *ConcreteImplementor) implementation() {
fmt.Println(`Some implementation here...`)
}
Usage
myObj := Abstraction{&ConcreteImplementor{}}
myObj.imp.implementation()