implemented bridge pattern
This commit is contained in:
parent
0d876e949c
commit
81c20c5cb7
@ -16,6 +16,7 @@
|
||||
- [Structural patterns](#structural-patterns)
|
||||
- [Composition](#composition)
|
||||
- [Adapter](#adapter)
|
||||
- [Bridge](#bridge)
|
||||
- [Behavioral patterns](#behavioral-patterns)
|
||||
- [Chain of responsibility](#chain-of-responsibility)
|
||||
- [Strategy](#strategy)
|
||||
@ -58,6 +59,9 @@ lets clients treat individual objects and compositions of objects uniformly.
|
||||
Convert the interfac e of a class into another interface clients expect. Adapter lets
|
||||
classes work together that couldn' t otherwise because of incompatible interfaces.
|
||||
|
||||
### Bridge
|
||||
Decouple an abstraction from its implementation so that the two can vary independently.
|
||||
|
||||
## Behavioral patterns
|
||||
Behavioral patterns are concerned with algorithms and the assignment of responsibilities
|
||||
between objects. Behavio ral patterns describe not just patterns of objects or classes
|
||||
|
37
structural/bridge/bridge.go
Normal file
37
structural/bridge/bridge.go
Normal file
@ -0,0 +1,37 @@
|
||||
package bridge
|
||||
|
||||
import "fmt"
|
||||
import "errors"
|
||||
|
||||
// Bridge interface
|
||||
type Drawer interface {
|
||||
DrawRectangle(w, h int)
|
||||
}
|
||||
|
||||
type GreenRectangleDrawer struct {}
|
||||
func (d GreenRectangleDrawer) DrawRectangle(w, h int) {
|
||||
fmt.Printf("Green rectangle W: %d, H: %d", w, h)
|
||||
}
|
||||
|
||||
type RedRectangleDrawer struct {}
|
||||
func (d RedRectangleDrawer) DrawRectangle(w, h int) {
|
||||
fmt.Printf("Red rectangle W: %d, H: %d", w, h)
|
||||
}
|
||||
|
||||
type Shape interface {
|
||||
Draw() error
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
Width, Height int
|
||||
drawer Drawer
|
||||
}
|
||||
|
||||
func (d Rectangle) Draw () error {
|
||||
if d.drawer == nil {
|
||||
return errors.New("Drawer not initialized.")
|
||||
}
|
||||
|
||||
d.drawer.DrawRectangle(d.Width, d.Height)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user