File tree 7 files changed +136
-0
lines changed
7 files changed +136
-0
lines changed Original file line number Diff line number Diff line change
1
+ package cmd
2
+
3
+ import (
4
+ "github.com/i-coder-robot/design-patterns-in-golang/Bridge/list"
5
+ "github.com/i-coder-robot/design-patterns-in-golang/Bridge/todo"
6
+ "testing"
7
+ )
8
+
9
+ func Test (t * testing.T ) {
10
+ myToDos := factoryToDo ("any" )
11
+ rendering := factoryList ("bullet" )
12
+ myToDos .SetList (rendering )
13
+
14
+ myToDos .Add ("study" )
15
+ myToDos .Add ("sports" )
16
+ myToDos .Add ("work" )
17
+ myToDos .Add ("happy" )
18
+
19
+ myToDos .Print ()
20
+ }
21
+
22
+
23
+ func factoryToDo (s string ) todo.ToDo {
24
+ if s == "unique" {
25
+ return todo .NewUnique ()
26
+ }
27
+ return todo .NewAny ()
28
+ }
29
+
30
+ func factoryList (s string ) list.List {
31
+ if s == "plain" {
32
+ return list .NewPlain ()
33
+ }
34
+ return list .NewBullet ('*' )
35
+ }
Original file line number Diff line number Diff line change
1
+ package list
2
+
3
+ import "fmt"
4
+
5
+ type Bullet struct {
6
+ Bullet rune
7
+ }
8
+
9
+ func NewBullet (b rune ) * Bullet {
10
+ return & Bullet {Bullet : b }
11
+ }
12
+
13
+ func (b * Bullet ) Print (todos []string ) {
14
+ for _ , v := range todos {
15
+ fmt .Println ("\t " , string (b .Bullet ), v )
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package list
2
+
3
+ type List interface {
4
+ Print ([]string )
5
+ }
Original file line number Diff line number Diff line change
1
+ package list
2
+
3
+ import "fmt"
4
+
5
+ type Plain struct {
6
+ }
7
+
8
+ func NewPlain () * Plain {
9
+ return & Plain {}
10
+ }
11
+
12
+ func (p * Plain ) Print (todos []string ){
13
+ for _ ,v := range todos {
14
+ fmt .Println ("\t " ,v )
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ package todo
2
+
3
+ import "github.com/i-coder-robot/design-patterns-in-golang/Bridge/list"
4
+
5
+ type Any struct {
6
+ rendering list.List
7
+ todos []string
8
+ }
9
+
10
+ func NewAny () * Any {
11
+ return & Any {}
12
+ }
13
+
14
+ func (a * Any ) SetList (l list.List ){
15
+ a .rendering = l
16
+ }
17
+
18
+ func (a * Any ) Add (name string ){
19
+ a .todos = append (a .todos , name )
20
+ }
21
+
22
+ func (a * Any ) Print (){
23
+ a .rendering .Print (a .todos )
24
+ }
Original file line number Diff line number Diff line change
1
+ package todo
2
+
3
+ import "github.com/i-coder-robot/design-patterns-in-golang/Bridge/list"
4
+
5
+ type ToDo interface {
6
+ SetList (l list.List )
7
+ Add (name string )
8
+ Print ()
9
+ }
Original file line number Diff line number Diff line change
1
+ package todo
2
+
3
+ import "github.com/i-coder-robot/design-patterns-in-golang/Bridge/list"
4
+
5
+ type Unique struct {
6
+ rendering list.List
7
+ todos []string
8
+ }
9
+
10
+ func NewUnique () * Unique {
11
+ return & Unique {}
12
+ }
13
+
14
+ func (u * Unique ) Add (name string ){
15
+ for _ ,v := range u .todos {
16
+ if name == v {
17
+ return
18
+ }
19
+ }
20
+
21
+ u .todos = append (u .todos ,name )
22
+ }
23
+
24
+ func (u * Unique ) Print () {
25
+ u .rendering .Print (u .todos )
26
+ }
27
+
28
+ func (u * Unique ) SetList (l list.List ) {
29
+ u .rendering = l
30
+ }
You can’t perform that action at this time.
0 commit comments