写点什么

组合设计模式 - 打印窗口组件的树状结构

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

GoF对组合模式的定义是,将对象组合成树形结构以表示“部分整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性

package main
import "fmt"
type Component interface {
Print()
}
type WinForm struct {
Type string
Name string
Children []Component
}
func NewWinForm(name string) *WinForm {
return &WinForm{
Type: "WinForm",
Name: name,
Children: []Component{},
}
}
func (w *WinForm) Print() {
fmt.Printf("print %s(%s) \n", w.Name, w.Type)
for _, c := range w.Children {
c.Print()
}
}
func (w *WinForm) AddChild(component Component) {
w.Children = append(w.Children, component)
}
type Picture struct {
Name string
Tpye string
}
func NewPicture(name string) *Picture {
return &Picture{
Name: name,
Tpye: "Picture",
}
}
func (p *Picture) Print() {
fmt.Printf("print %s(%s)\n", p.Tpye, p.Name)
}
type Button struct {
Name string
Type string
}
func NewButton(name string) *Button {
return &Button{
Name: name,
Type: "Button",
}
}
func (b *Button) Print() {
fmt.Printf("print %s(%s)\n", b.Type, b.Name)
}
type Frame struct {
Name string
Type string
Children []Component
}
func NewFrame(name string) *Frame {
return &Frame{
Name: name,
Type: "Frame",
Children: []Component{},
}
}
func (f *Frame) Print() {
fmt.Printf("print %s(%s)\n", f.Type, f.Name)
for _, c := range f.Children{
c.Print()
}
}
func (f *Frame) AddChild(component Component) {
f.Children = append(f.Children, component)
}
type Label struct {
Name string
Type string
}
func NewLabel(name string) *Label {
return &Label{
Name: name,
Type: "Label",
}
}
func (l *Label) Print() {
fmt.Printf("print %s(%s)\n", l.Type, l.Name)
}
type TextBox struct {
Name string
Type string
}
func NewTextBox(name string) *TextBox {
return &TextBox{
Name: name,
Type: "TextBox",
}
}
func (t *TextBox) Print() {
fmt.Printf("print %s(%s)\n", t.Type, t.Name)
}
type PasswordBox struct {
Name string
Type string
}
func NewPasswordBox(name string) *PasswordBox {
return &PasswordBox{
Name: name,
Type: "PasswordBox",
}
}
func (p *PasswordBox) Print() {
fmt.Printf("print %s(%s)\n", p.Type, p.Name)
}
type CheckBox struct {
Name string
Type string
}
func NewCheckBox(name string) *CheckBox {
return &CheckBox{
Name: name,
Type: "CheckBox",
}
}
func (c *CheckBox) Print() {
fmt.Printf("print %s(%s)\n", c.Type, c.Name)
}
type LinkLabel struct {
Name string
Type string
}
func NewLinkLabel(name string) *LinkLabel {
return &LinkLabel{
Name: name,
Type: "LinkLabel",
}
}
func (l *LinkLabel) Print() {
fmt.Printf("print %s(%s)\n", l.Type, l.Name)
}
func main() {
win := NewWinForm("WINDOWS窗口")
pic := NewPicture("LOGO图片")
win.AddChild(pic)
login := NewButton("登录")
win.AddChild(login)
register := NewButton("注册")
win.AddChild(register)
frame1 := NewFrame("FRAME1")
win.AddChild(frame1)
userLabel := NewLabel("用户名")
frame1.AddChild(userLabel)
userTextBox := NewTextBox("文本框")
frame1.AddChild(userTextBox)
password := NewLabel("密码")
frame1.AddChild(password)
passwordBox := NewPasswordBox("密码框")
frame1.AddChild(passwordBox)
checkBox := NewCheckBox("复选框")
frame1.AddChild(checkBox)
textBox := NewTextBox("记住用户名")
frame1.AddChild(textBox)
linkLabel := NewLinkLabel("忘记密码")
frame1.AddChild(linkLabel)
win.Print()
}





用户头像

张磊

关注

还未添加个人签名 2017.10.17 加入

还未添加个人简介

评论

发布
暂无评论
组合设计模式-打印窗口组件的树状结构