首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

组合设计模式编写程序

请用组合设计模式编写程序,打印输出图1的窗口, 窗口组件的树结构如图2所示。

1、新增接口IForm


public interface IForm {
    public void print();
}

2、实现接口的默认实现AbstractForm


public abstract class AbstractForm implements IForm {
    private String label;
    public AbstractForm(String _label){
        this.label = _label;
    }
    
    public void print() {
        System.out.println("print " + this.getClass().getSimpleName() + "(" + getLabel() + ")");
    }
    
    public String getLabel(){
        return this.label;
    }
}

3、实现Button、Label、TextBox、Picture等具体类。

4、实现Frame类、WinForm类


public class Frame extends AbstractForm {
    private Vector<IForm> vector = new Vector<IForm>();
    public Frame(String _label){
        super(_label);
    }
    @Override
    public void print() {
        super.print();
        for (IForm iform:
             vector) {
            iform.print();
        }
    }
    
    public void add (IForm form){
        vector.add(form);
    }
}

public class WinForm extends Frame {
    private static String STATIC_WINDOW_FORM_LABEL = "WINDOW窗口";
    public WinForm(){
        this(STATIC_WINDOW_FORM_LABEL);
    }
    public WinForm(String _label) {
        super(_label);
    }
}

码云地址:https://gitee.com/bigstonezg/window-form.git

  • 发表于:
  • 本文为 InfoQ 中文站特供稿件
  • 首发地址https://www.infoq.cn/article/b7228dc66b897db371473d60a
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券