Week_03 作业

用户头像
golangboy
关注
发布于: 2020 年 10 月 04 日

作业要求:

作业分析:

通过使用组合设计模式,表达窗口组件布局结构,关键之处需要设计出leaf和composite抽象结构。

golang中通过使用接口,将composite结构所有组件进行输出

程序示例:

package main

import (
"fmt"
)

type WinComponent interface {
Print()
}

type WinLeaf struct {
Type string
Name string
}

func (w *WinLeaf) Print() {
fmt.Printf("print %s(%s)\n", w.Type, w.Name)
}

type WinComposite struct {
WinLeaf
list []WinComponent
}

func (w *WinComposite) Register(c WinComponent) {
w.list = append(w.list, c)
}

func (w *WinComposite) Print() {
w.WinLeaf.Print()
for _, c := range w.list {
c.Print()
}
}

type TextBox struct {
WinLeaf
}

func (l *TextBox) Print() {
l.Type = "TextBox"
l.WinLeaf.Print()
}

type PasswdBox struct {
WinLeaf
}

func (l *PasswdBox) Print() {
l.Type = "PasswordBox"
l.WinLeaf.Print()
}

type CheckBox struct {
WinLeaf
}

func (l *CheckBox) Print() {
l.Type = "CheckBox"
l.WinLeaf.Print()
}

type Label struct {
WinLeaf
}

func (l *Label) Print() {
l.Type = "Label"
l.WinLeaf.Print()
}

type LinkLabel struct {
WinLeaf
}

func (l *LinkLabel) Print() {
l.Type = "LinkLabel"
l.WinLeaf.Print()
}

type Picture struct {
WinLeaf
}

func (l *Picture) Print() {
l.Type = "Picture"
l.WinLeaf.Print()
}

type Frame struct {
WinComposite
}

func (l *Frame) Print() {
l.Type = "Frame"
l.WinComposite.Print()
}

type Button struct {
WinLeaf
}

func (l *Button) Print() {
l.Type = "Button"
l.WinLeaf.Print()
}

type WinForm struct {
WinComposite
}

func (l *WinForm) Print() {
l.Type = "WinForm"
l.WinComposite.Print()
}

func main() {
logo := &Picture{}
logo.Name = "LOGO图片"

frame := &Frame{}
frame.Name = "FRAME1"

username := &Label{}
username.Name = "用户名"
frame.Register(username)

usernameBox := &TextBox{}
usernameBox.Name = "文本框"
frame.Register(usernameBox)

passwd := &Label{}
passwd.Name = "密码"
frame.Register(passwd)

passwdBox := &PasswdBox{}
passwdBox.Name = "密码框"
frame.Register(passwdBox)

checkBox := &CheckBox{}
checkBox.Name = "复选框"
frame.Register(checkBox)

rememberUser := &Label{}
rememberUser.Name = "记住用户名"
frame.Register(rememberUser)

forgotPasswd := &LinkLabel{}
forgotPasswd.Name = "忘记密码"
frame.Register(forgotPasswd)

login := &Button{}
login.Name = "登录"

register := &Button{}
register.Name = "注册"

win := &WinForm{}
win.Name = "WINDOW窗口"
win.Register(logo)
win.Register(login)
win.Register(register)
win.Register(frame)
win.Print()
}


输出结果:



用户头像

golangboy

关注

还未添加个人签名 2018.09.18 加入

还未添加个人简介

评论

发布
暂无评论
Week_03 作业