-
Notifications
You must be signed in to change notification settings - Fork 21
/
decorate.go
61 lines (48 loc) · 1.1 KB
/
decorate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import "fmt"
// Component 步骤一 :实现Component抽象类以及ConcreteComponent具体构建
type Component interface {
Display()
}
type Window struct{}
func (w Window) Display() {
fmt.Println("显示窗体")
}
type TextBox struct{}
func (t TextBox) Display() {
fmt.Println("显示文本框")
}
type ListBox struct{}
func (l ListBox) Display() {
fmt.Println("显示列表框")
}
// ScrollBarDecorator 步骤二:实现ConcreteDecorator具体装饰类
type ScrollBarDecorator struct {
Component
}
func (sbd ScrollBarDecorator) Display() {
fmt.Println("为构建增加滚动条")
sbd.Component.Display()
}
type BlackBorderDecorator struct {
Component
}
func (bbd BlackBorderDecorator) Display() {
fmt.Println("为构建增加黑色边框")
bbd.Component.Display()
}
// NewDecorator 步骤三: 定义工厂函数生产出具体装饰类
func NewDecorator(t string, decorator Component) Component {
switch t {
case "sbd":
return ScrollBarDecorator{
Component: decorator,
}
case "bbd":
return BlackBorderDecorator{
Component: decorator,
}
default:
return nil
}
}