写点什么

第三周作业

用户头像
fmouse
关注
发布于: 2020 年 10 月 04 日
  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。



  1. 请用组合设计模式编写程序,打印输出图 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()
}



用户头像

fmouse

关注

还未添加个人签名 2018.08.07 加入

还未添加个人简介

评论

发布
暂无评论
第三周作业