/**
* 控件顶层接口
* **/
public interface Control {
String getName();
void addControl(Control control);
void draw();
}
/**
* 简单控件基类
* */
public abstract class AbstractSimpleControl implements Control {
private String name;
@Override
public String getName(){
return this.name;
}
protected AbstractSimpleControl(String name){
this.name=name;
}
@Override
public void addControl(Control control){
throw new UnsupportedOperationException();
}
@Override
public void draw(){
System.out.println("print "+this.getClass().getSimpleName()+"("+this.getName()+")");
}
}
/**
* 容器控件基类
* **/
public abstract class AbstractContainerControl implements Control {
private String name;
private List<Control> controls;
protected AbstractContainerControl(String name){
this.name=name;
this.controls=new ArrayList<Control>();
}
public void addControl(Control control){
this.controls.add(control);
}
public String getName(){
return this.name;
}
@Override
public void draw() {
System.out.println("print "+this.getClass().getSimpleName()+"("+this.getName()+")");
for(Control control:this.controls){
control.draw();
}
}
}
/**
* Button控件
* */
public class Button extends AbstractSimpleControl {
public Button(String name){super(name);}
}
/***
* 复选框
* */
public class CheckBox extends AbstractSimpleControl {
public CheckBox(String name){super(name);}
}
/**
* 标签控件
* **/
public class Label extends AbstractSimpleControl {
public Label(String name){super(name);}
}
public class LinkLabel extends AbstractSimpleControl {
public LinkLable(String name){super(name);}
}
/**
* passwordBox
* **/
public class PasswordBox extends AbstractSimpleControl {
public PasswordBox(String name){super(name);}
}
/**
* 图片控件
* */
public class Picture extends AbstractSimpleControl {
public Picture(String name){super(name);}
}
/**
* 文本输入框控件
* */
public class TextBox extends AbstractSimpleControl {
public TextBox(String name){super(name);}
}
/**
* WinForm容器控件
* ***/
public class WinForm extends AbstractContainerControl {
public WinForm(String name){
super(name);
}
}
/**
* Frame 容器控件
* **/
public class Frame extends AbstractContainerControl {
public Frame(String name){
super(name);
}
}
public class CompositeControlDemo {
public static void main(String[] args) {
Control winForm = new WinForm("WINDOW窗口");
Control picture = new Picture("LOGO图片");
winForm.addControl(picture);
Control loginBtn=new Button("登录");
winForm.addControl(loginBtn);
Control registerBtn=new Button("注册");
winForm.addControl(registerBtn);
Control frame=new Frame("FRAME1");
winForm.addControl(frame);
Control userNameLabel=new Label("用户名");
frame.addControl(userNameLabel);
Control textBox=new TextBox("文本框");
frame.addControl(textBox);
Control passwordLabel=new Label("密码");
frame.addControl(passwordLabel);
Control passwordBox=new PasswordBox("密码框");
frame.addControl(passwordBox);
Control checkbox=new CheckBox("复选框");
frame.addControl(checkbox);
Control rememberMe=new TextBox("记住用户名");
frame.addControl(rememberMe);
Control noPassword=new LinkLable("忘记密码");
frame.addControl(noPassword);
winForm.draw();
}
}
评论