package main
import "fmt"
type Component interface {
Name() string
Form() string
Add(c Component)
Print()
}
type Desc struct {
name string
form string
}
func (d *Desc) Name() string {
return d.name
}
func (d *Desc) Form() string {
return d.form
}
type Frame struct {
Desc
children []Component
}
func NewFrame(name string, form string) Component {
return &Frame{
Desc: Desc{
name: name,
form: form,
},
}
}
func (f *Frame) Add(c Component) {
f.children = append(f.children, c)
}
func (f *Frame) Print() {
fmt.Printf("print %s(%s)\n", f.Form(), f.Name())
for _, v := range f.children {
v.Print()
}
}
type FrameItem struct {
Desc
}
func NewFrameItem(name string, form string) Component {
return &FrameItem{
Desc: Desc{
name: name,
form: form,
},
}
}
func (fi *FrameItem) Print() {
fmt.Printf("print %s(%s)\n", fi.Form(), fi.Name())
}
func (fi *FrameItem) Add(Component) {
panic("no implement")
}
func main() {
f := NewFrame("WINDOW窗口", "WinForm")
f.Add(NewFrameItem("LOGO图片", "Picture"))
f.Add(NewFrameItem("登录", "Button"))
f.Add(NewFrameItem("注册", "Button"))
child := NewFrame("Frame1", "Frame")
child.Add(NewFrameItem("用户名", "Label"))
child.Add(NewFrameItem("文本框", "TextBox"))
child.Add(NewFrameItem("密码", "Label"))
child.Add(NewFrameItem("密码框", "PasswordBox"))
child.Add(NewFrameItem("复选框", "CheckBox"))
child.Add(NewFrameItem("记住用户名", "TextBox"))
child.Add(NewFrameItem("忘记密码", "LinkLabel"))
f.Add(child)
f.Print()
}
评论