写点什么

第三周作业

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

一:单例

手写一个单例模式的实现代码



二:组合模式



用组合设计模式编写程序

打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图





package main
import "fmt"
//import "reflect"
type Component interface {
Print()
AddChild(...Component)
}
type Base struct {
name string
children []Component
}
type WinForm Base
func (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 Base
func (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 Base
func (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 Base
func (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 Base
func (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 Base
func (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 Base
func (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 Base
func (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 Base
func (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
用户头像

麻辣

关注

还未添加个人签名 2018.10.13 加入

还未添加个人简介

评论

发布
暂无评论
第三周作业