第三周作业
发布于: 2020 年 06 月 24 日
一:单例
手写一个单例模式的实现代码
二:组合模式
用组合设计模式编写程序
打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图
package mainimport "fmt"//import "reflect"type Component interface { Print() AddChild(...Component)}type Base struct { name string children []Component}type WinForm Basefunc (b *WinForm) Print() { fmt.Printf("print WinForm(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *WinForm) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type Picture Basefunc (b *Picture) Print() { fmt.Printf("print Picture(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *Picture) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type Button Basefunc (b *Button) Print() { fmt.Printf("print Button(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *Button) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type Label Basefunc (b *Label) Print() { fmt.Printf("print Label(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *Label) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type TextBox Basefunc (b *TextBox) Print() { fmt.Printf("print TextBox(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *TextBox) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type PasswordBox Basefunc (b *PasswordBox) Print() { fmt.Printf("print PasswordBox(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *PasswordBox) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type Frame Basefunc (b *Frame) Print() { fmt.Printf("print Frame(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *Frame) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type CheckBox Basefunc (b *CheckBox) Print() { fmt.Printf("print CheckBox(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *CheckBox) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}type LinkLabel Basefunc (b *LinkLabel) Print() { fmt.Printf("print LinkLabel(%s)\n", b.name) for _, c := range b.children { c.Print() }}func (b *LinkLabel) AddChild(cmp...Component) { b.children = append(b.children, cmp...)}func main() { b := WinForm{name: "WINDOWS窗口"} b.AddChild(&Picture{name: "LOGO图片"}, &Button{name: "登录"}, &Button{name: "注册"}) frame1 := Frame{name: "Frame1"} frame1.AddChild(&Label{name:"用户名"}, &TextBox{name: "文本框"}, &Label{name: "密码"}, &PasswordBox{name: "密码框"}, &CheckBox{name: "复选框"}, &Label{name: "记住用户名"}, &LinkLabel{name: "忘记密码"}) b.AddChild(&frame1) b.Print() } 输出结果---------------------------------print WinForm(WINDOWS窗口)print Picture(LOGO图片)print Button(登录)print Button(注册)print Frame(Frame1)print Label(用户名)print TextBox(文本框)print Label(密码)print PasswordBox(密码框)print CheckBox(复选框)print Label(记住用户名)print LinkLabel(忘记密码)
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 47
版权声明: 本文为 InfoQ 作者【麻辣】的原创文章。
原文链接:【http://xie.infoq.cn/article/552fb9e824b288a46c8a39aa6】。未经作者许可,禁止转载。
麻辣
关注
还未添加个人签名 2018.10.13 加入
还未添加个人简介
评论