组合模式设计窗口组件
发布于: 2020 年 06 月 22 日
组合模式设计窗口组件
概述
但凡遇到树形的设计,第一时间想到用组合模式。比如一个窗口,可以包含自窗口,子窗口又包含若干控件。窗口绘制得时候,递归调用子窗口的绘制方法.
设计
首先定义窗口接口:
package com.geektime.composite;public interface Window {    public void draw();}
定义一个控件的抽象基类,实现打印的默认操作:
package com.geektime.composite;public abstract class Control implements Window {    private String name = "";    public Control(String name) {        this.name = name;    }    protected void drawSelf() {        String info = "print " +                this.getClass().getSimpleName() +                "(" + this.name +                ")";        System.out.println(info);    }    @Override    public void draw() {        this.drawSelf();    }}
有些子窗口还可以包含子窗口,即树的非叶子节点。定义一个BaseFrame抽象类继承Control:
package com.geektime.composite;import java.util.ArrayList;public abstract class BaseFrame extends Control {    ArrayList<Window> subviewList = new ArrayList<Window>();    public BaseFrame(String name) {        super(name);    }    public void addSubview(Window subview) {        this.subviewList.add(subview);    }    @Override    public void draw() {        this.drawSelf();        //        for (Window window : this.subviewList) {            window.draw();        }    }}
现在定义具体的实现类,比如Button,Label,TextBox等
Button
package com.geektime.composite;public final class Button extends Control {    public Button(String name) {        super(name);    }}
CheckBox
package com.geektime.composite;public final class CheckBox extends Control{    public CheckBox(String name) {        super(name);    }}
Frame
package com.geektime.composite;public final class Frame extends BaseFrame{    public Frame(String name) {        super(name);    }}
Label
package com.geektime.composite;public final class Label extends Control {    public Label(String name) {        super(name);    }}
LinkLabel
package com.geektime.composite;public final class LinkLabel extends Control {    public LinkLabel(String name) {        super(name);    }}
PasswordBox
package com.geektime.composite;public final class PasswordBox extends Control {    public PasswordBox(String name) {        super(name);    }}
Picture
package com.geektime.composite;public final class Picture extends Control {    public Picture(String name) {        super(name);    }}
TextBox
package com.geektime.composite;/** * @author pinkong */public final class TextBox extends Control {    public TextBox(String name) {        super(name);    }}
组装测试
package com.geektime.composite;public class Client {    public static void main (String [] args) {        WinForm winForm = new WinForm("WINDOW窗口");        Picture picture = new Picture("LOGO图片");        Button btnLogin = new Button("登录");        Button btnRegister = new Button("注册");        Frame frame = new Frame("FRAME1");        Label lbUsername = new Label("用户名");        TextBox textBox = new TextBox("文本框");        Label lbPassword = new Label("密码");        TextBox tbPassword = new TextBox("密码框");        CheckBox checkBox = new CheckBox("复选框");        TextBox tbRemenber = new TextBox("记住用户名");        LinkLabel linkLabel = new LinkLabel("忘记密码");        //组装        winForm.addSubview(picture);        winForm.addSubview(btnLogin);        winForm.addSubview(btnRegister);        winForm.addSubview(frame);        frame.addSubview(lbUsername);        frame.addSubview(textBox);        frame.addSubview(lbPassword);        frame.addSubview(tbPassword);        frame.addSubview(checkBox);        frame.addSubview(tbRemenber);        frame.addSubview(linkLabel);        //打印        winForm.draw();    }}
输出结果
print WinForm(WINDOW窗口)print Picture(LOGO图片)print Button(登录)print Button(注册)print Frame(FRAME1)print Label(用户名)print TextBox(文本框)print Label(密码)print TextBox(密码框)print CheckBox(复选框)print TextBox(记住用户名)print LinkLabel(忘记密码)
 划线
   评论
  复制
发布于: 2020 年 06 月 22 日 阅读数: 35
 
 缺省模式
  关注 
还未添加个人签名 2018.03.16 加入
还未添加个人简介
 
 
  
  
 
 
 
  
  
  
  
  
  
  
  
    
评论