implemented bridge pattern

This commit is contained in:
ismayilmalik 2018-01-08 21:15:03 +04:00
parent 0d876e949c
commit 81c20c5cb7
2 changed files with 41 additions and 0 deletions

View File

@ -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

View 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
}