Bootstrap

组合设计模式编写程序

请用组合设计模式编写程序,打印输出图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 vector = new Vector();
    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