From 81c20c5cb7344e21ae5366b416f41cdb0e6e772b Mon Sep 17 00:00:00 2001 From: ismayilmalik Date: Mon, 8 Jan 2018 21:15:03 +0400 Subject: [PATCH] implemented bridge pattern --- README.md | 4 ++++ structural/bridge/bridge.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 structural/bridge/bridge.go diff --git a/README.md b/README.md index 942097e..4336e3f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/structural/bridge/bridge.go b/structural/bridge/bridge.go new file mode 100644 index 0000000..8804813 --- /dev/null +++ b/structural/bridge/bridge.go @@ -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 +} \ No newline at end of file