Skip to content

Commit 5b13794

Browse files
committed
bridge
1 parent cbe79a6 commit 5b13794

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

Bridge/cmd/bridge_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

Bridge/list/bullet.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

Bridge/list/list.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package list
2+
3+
type List interface {
4+
Print([]string)
5+
}

Bridge/list/plain.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

Bridge/todo/any.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

Bridge/todo/todo.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

Bridge/todo/unique.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)