架构师训练营 - 设计模式

用户头像
Pontus
关注
发布于: 2020 年 06 月 24 日

HomeWork

Singleton Pattern



Composite Pattern

  • Coding in Composite Pattern, print window as pic1, tree structure of the window as pic2, and example of printing output as pic3.





package main
import (
"fmt"
)
type IComponent interface {
Print()
}
// root class
type Composite struct {
Name string
List []IComponent
}
func NewComposite(name string) *Composite {
var list []IComponent
return &Composite{
name,
list,
}
}
func (c *Composite) Add(ic IComponent) {
c.List = append(c.List, ic)
}
func (c *Composite) Print() {
// print leader
fmt.Printf("print %s\n", c.Name)
// print follower
for _, cc := range c.List {
cc.Print()
}
}
// leaf class
type Picture struct {
Composite
}
func (m *Picture) Print() {
fmt.Printf("print Picture(%s)\n", m.Name)
}
type Button struct {
Composite
}
func (m *Button) Print() {
fmt.Printf("print Button(%s)\n", m.Name)
}
type Label struct {
Composite
}
func (m *Label) Print() {
fmt.Printf("print Label(%s)\n", m.Name)
}
type TextBox struct {
Composite
}
func (m *TextBox) Print() {
fmt.Printf("print TextBox(%s)\n", m.Name)
}
type PasswordBox struct {
Composite
}
func (m *PasswordBox) Print() {
fmt.Printf("print PasswordBox(%s)\n", m.Name)
}
type CheckBox struct {
Composite
}
func (m *CheckBox) Print() {
fmt.Printf("print CheckBox(%s)\n", m.Name)
}
type LinkLabel struct {
Composite
}
func (m *LinkLabel) Print() {
fmt.Printf("print LinkLabel(%s)\n", m.Name)
}
func main() {
winform := NewComposite("WinForm(WINDOW窗口)")
picture := Picture{Composite{"LOGO图片", nil}}
button1 := Button{Composite{"登陆", nil}}
button2 := Button{Composite{"注册", nil}}
frame1 := NewComposite("Frame(FRAME1)")
label1 := Label{Composite{"用户名", nil}}
textbox1 := TextBox{Composite{"文本框", nil}}
label2 := Label{Composite{"密码", nil}}
passwordbox := PasswordBox{Composite{"密码框", nil}}
checkbox := CheckBox{Composite{"复选框", nil}}
textbox2 := TextBox{Composite{"记住用户名", nil}}
linklabel := LinkLabel{Composite{"忘记密码", nil}}
frame1.Add(&label1)
frame1.Add(&textbox1)
frame1.Add(&label2)
frame1.Add(&passwordbox)
frame1.Add(&checkbox)
frame1.Add(&textbox2)
frame1.Add(&linklabel)
winform.Add(&picture)
winform.Add(&button1)
winform.Add(&button2)
winform.Add(frame1)
winform.Print()
}



用户头像

Pontus

关注

还未添加个人签名 2018.04.21 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营 - 设计模式