Singleton Pattern & Composite Pattern
发布于: 2020 年 10 月 04 日
1. Singleton (JavaScript)
2. Composite Pattern
Element 是抽象类,UIElement是Element的实现,所有其他的类都是继承实现了UIElement。
class Element { constructor (label) {} add (child) {} draw () {} print () {}}class UIElement extends Element { constructor (label) { super(label) this.label = label this.parent = null this.children = [] } add (child) { child.parent = this this.children.push(child) } draw () { for (let i = 0; i < this.children.length; i++) { const child = this.children[i] child.print() child.draw() } } print () { console.log(`print ${this.constructor.name}(${this.label})`) }}class Window extends UIElement {}class WindowForm extends UIElement {}class Picture extends UIElement {}class Button extends UIElement {}class Frame extends UIElement {}class Label extends UIElement {}class TextBox extends UIElement {}class PasswordBox extends UIElement {}class Checkbox extends UIElement {}class LinkLabel extends UIElement {}const windowElement = new Window()const winForm = new WindowForm('Window')const winFormPicture = new Picture('Logo')const winFormButtonLogin = new Button('Login')const winFormButtonRegister = new Button('Register')const winFormFrame = new Frame('Frame')const frameLabelUsername = new Label('Username')const frameTextboxUsername = new TextBox('Username Input')const frameLabelPassword = new Label('Password')const frameTextboxPassword = new PasswordBox('Password Input')const frameCheckbox = new Checkbox('Checkbox')const frameLabelRemember = new Label('Remember Username')const frameLinkLabel = new LinkLabel('Forget Password')windowElement.add(winForm)winForm.add(winFormPicture)winForm.add(winFormButtonLogin)winForm.add(winFormButtonRegister)winForm.add(winFormFrame)winFormFrame.add(frameLabelUsername)winFormFrame.add(frameTextboxUsername)winFormFrame.add(frameLabelPassword)winFormFrame.add(frameTextboxPassword)winFormFrame.add(frameCheckbox)winFormFrame.add(frameLabelRemember)winFormFrame.add(frameLinkLabel)windowElement.draw()
划线
评论
复制
发布于: 2020 年 10 月 04 日 阅读数: 18
晨
关注
还未添加个人签名 2020.05.01 加入
还未添加个人简介
评论