第三周作业
1.手写单列模式:
2.利用组合模式编写程序:
1)抽象出共有的特征:
public interface UIElement {
public void show();
public void addElement(UIElement elem);
}
2)实现组合节点的元素:
public class WinForm implements UIElement{
private List<UIElement> list = new ArrayList<UIElement>();
private String eleName;
public WinForm(String ele) {
this.eleName = ele;
}
@Override
public void show() {
System.out.println("This is " + this.eleName);
for(UIElement ele : list) {
ele.show();
}
}
@Override
public void addElement(UIElement elem) {
this.list.add(elem);
}
}
3)实现叶子节点:
public class PasswordBox implements UIElement{
private String eleName;
public PasswordBox(String ele) {
this.eleName = ele;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("This is " + this.eleName);
}
@Override
public void addElement(UIElement elem) {
}
}
4)测试:
public class Test {
public static void main(String[] args) {
// new Test().test1();
new Test().test2();
}
public void test1() {
ComplexElement win = new ComplexElement("WINDOWS窗口");
win.addElement(new SimpleElement("Picture"));
win.addElement(new SimpleElement("Button(登录)"));
ComplexElement frame = new ComplexElement("Frame");
frame.addElement(new SimpleElement("Lable"));
frame.addElement(new SimpleElement("TextBox"));
win.addElement(frame);
win.show();
}
public void test2() {
WinForm win = new WinForm("WINDOW窗口");
win.addElement(new PictureLogo("Picture(LOGO图片)"));
win.addElement(new Button("Button(登录)"));
win.addElement(new Button("Button(注册"));
Frame frame = new Frame("Frame(FRAME1)");
frame.addElement(new Lable("Lable(用户名)"));
frame.addElement(new TextBox("TextBox(文本框)"));
frame.addElement(new Lable("Lable(密码)"));
frame.addElement(new PasswordBox("PasswordBox(密码框)"));
frame.addElement(new CheckBox("CheckBox(复选框)"));
frame.addElement(new TextBox("TextBox(记住用户名)"));
frame.addElement(new LinkLable("LinkLable(忘记密码)"));
win.addElement(frame);
win.show();
}
}
评论