写点什么

架构师训练营第 3 周作业

用户头像
aoeiuvzcs
关注
发布于: 2020 年 06 月 24 日
  1. 请在草稿纸上手写一个单例模式的实现代码



手写实现一个单例模式计数器,非线程安全



  1. 用组合设计模式编写程序,打印窗口



package main
import "fmt"
type Component interface {
Add(c Component)
Print()
}
type ComponentDesc struct {
desc string
}
func NewComponentDesc(desc string) ComponentDesc {
return ComponentDesc{
desc: desc,
}
}
func (cd *ComponentDesc)Desc() string {
return cd.desc
}
type ComponentBase struct {
}
func (cb *ComponentBase)Add(c Component) {
return
}
type Picture struct {
ComponentDesc
ComponentBase
}
func NewPicture(desc string) Component {
return &Picture{
ComponentDesc:NewComponentDesc(desc),
}
}
func (p *Picture)Print() {
fmt.Printf("print Picture(%s)\n", p.Desc())
}
type Button struct {
ComponentDesc
ComponentBase
}
func NewButton(desc string) Component {
return &Button{
ComponentDesc: NewComponentDesc(desc),
}
}
func (b *Button)Print() {
fmt.Printf("print Button(%s)\n", b.Desc())
}
type Label struct {
ComponentDesc
ComponentBase
}
func NewLabel(desc string) Component {
return &Label{
ComponentDesc:NewComponentDesc(desc),
}
}
func (l *Label)Print() {
fmt.Printf("print Lable(%s)\n", l.Desc())
}
type TextBox struct {
ComponentDesc
ComponentBase
}
func NewTextBox(desc string) Component {
return &TextBox{
ComponentDesc:NewComponentDesc(desc),
}
}
func (tb *TextBox)Print() {
fmt.Printf("print TextBox(%s)\n", tb.Desc())
}
type PasswordBox struct {
ComponentDesc
ComponentBase
}
func NewPasswordBox(desc string) Component {
return &PasswordBox{
ComponentDesc:NewComponentDesc(desc),
}
}
func (pb *PasswordBox)Print() {
fmt.Printf("print PasswordBox(%s)\n", pb.Desc())
}
type CheckBox struct {
ComponentDesc
ComponentBase
}
func NewCheckBox(desc string) Component {
return &CheckBox{
ComponentDesc:NewComponentDesc(desc),
}
}
func (cb *CheckBox)Print() {
fmt.Printf("print CheckBox(%s)\n", cb.Desc())
}
type LinkLabel struct {
ComponentDesc
ComponentBase
}
func NewLinkLabel(desc string) Component {
return &LinkLabel{
ComponentDesc:NewComponentDesc(desc),
}
}
func (ll *LinkLabel)Print() {
fmt.Printf("print LinkLabel(%s)\n", ll.Desc())
}
type WindowForm struct {
ComponentDesc
Child []Component
}
func NewWindowForm(desc string) Component {
return &WindowForm{
ComponentDesc: NewComponentDesc(desc),
}
}
func (wf* WindowForm)Add(c Component) {
wf.Child = append(wf.Child, c)
}
func (wf *WindowForm)Print() {
fmt.Printf("print WinForm(%s)\n", wf.Desc())
for _, c := range wf.Child {
c.Print()
}
}
type Frame struct {
ComponentDesc
Child []Component
}
func NewFrame(desc string) Component {
return &Frame{
ComponentDesc: NewComponentDesc(desc),
}
}
func (f* Frame)Add(c Component) {
f.Child = append(f.Child, c)
}
func (f* Frame)Print() {
fmt.Printf("print Frame(%s)\n", f.Desc())
for _, c := range f.Child {
c.Print()
}
}
func main() {
form := NewWindowForm("WINDOW窗口")
frame := NewFrame("FRAME1")
form.Add(NewPicture("LOGO图片"))
form.Add(NewButton("登录"))
form.Add(NewButton("注册"))
form.Add(frame)
frame.Add(NewLabel("用户名"))
frame.Add(NewTextBox("文本框"))
frame.Add(NewLabel("密码"))
frame.Add(NewPasswordBox("密码框"))
frame.Add(NewCheckBox("复选框"))
frame.Add(NewTextBox("记住用户名"))
frame.Add(NewLinkLabel("忘记密码"))
form.Print()
}



用户头像

aoeiuvzcs

关注

还未添加个人签名 2018.03.25 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第3周作业