第三周作业
发布于: 2020 年 10 月 04 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
component.go
package homework
type component interface {
print()
}
复制代码
button.go
package homework
type button struct {
name string
}
func NewButton(name string) *button {
return &button{
name: name,
}
}
func (b *button) print() {
println("print Button(" + b.name + ")")
}
复制代码
checkBox.go
package homework
type checkBox struct {
name string
}
func NewCheckBox(name string) *checkBox {
return &checkBox{
name: name,
}
}
func (c *checkBox) print() {
println("print CheckBox(" + c.name + ")")
}
复制代码
frame.go
package homework
type frame struct {
name string
elements []component
}
func NewFrame(name string) *frame {
return &frame{
name: name,
}
}
func (f *frame) print() {
println("print Frame(" + f.name + ")")
for _, e := range f.elements {
e.print()
}
}
func (f *frame) AddElement(e component) {
f.elements = append(f.elements, e)
}
复制代码
lable.go
package homework
type lable struct {
name string
}
type linkLable struct {
*lable
href string
}
func NewLable(name string) *lable {
return &lable{
name: name,
}
}
func (l *lable) print() {
println("print Lable(" + l.name + ")")
}
func NewLinkLable(name, href string) *linkLable {
return &linkLable{
NewLable(name),
href,
}
}
func (l *linkLable) print() {
println("print LinkLable(" + l.name + ")")
}
复制代码
picture.go
package homework
type picture struct {
name string
}
func NewPicture(name string) *picture {
return &picture{
name: name,
}
}
func (b *picture) print() {
println("print Picture(" + b.name + ")")
}
复制代码
textBox.go
package homework
type textBox struct {
name string
}
type passwordBox struct {
*textBox
isPassword bool
}
func NewTextBox(name string) *textBox {
return &textBox{
name: name,
}
}
func (t *textBox) print() {
println("print TextBox(" + t.name + ")")
}
func NewPasswordBox(name string) *passwordBox {
return &passwordBox{
NewTextBox(name),
true,
}
}
func (t *passwordBox) print() {
println("print PasswordBox(" + t.name + ")")
}
复制代码
winForm.go
package homework
type winForm struct {
name string
elements []component
}
func NewWinForm(name string) *winForm {
return &winForm{
name: name,
}
}
func (w *winForm) Print() {
println("print WinForm(" + w.name + ")")
for _, e := range w.elements {
e.print()
}
}
func (w *winForm) AddElement(e component) {
w.elements = append(w.elements, e)
}
复制代码
homework_test.go
package homework
import "testing"
func TestHomework(t *testing.T) {
w := NewWinForm("WINDOW窗口")
w.AddElement(NewPicture("LOGO图片"))
w.AddElement(NewButton("登陆"))
w.AddElement(NewButton("注册"))
frame1 := NewFrame("FRAME1")
w.AddElement(frame1)
frame1.AddElement(NewLable("用户名"))
frame1.AddElement(NewTextBox("文本框"))
frame1.AddElement(NewLable("密码"))
frame1.AddElement(NewPasswordBox("密码框"))
frame1.AddElement(NewCheckBox("复选框"))
frame1.AddElement(NewLable("记住用户名"))
frame1.AddElement(NewLinkLable("忘记密码", "link"))
w.Print()
}
复制代码
划线
评论
复制
发布于: 2020 年 10 月 04 日阅读数: 39
fmouse
关注
还未添加个人签名 2018.08.07 加入
还未添加个人简介
评论