作业 3
手写单例模式实现
组合模式实现
public class Component {
private String name;
private List<Component> componentList = new LinkedList<Component>();
public Component(String name){
this.name = name;
}
public final String getName(){
return name;
}
public void display(){
System.out.println("print " + name);
for (Component component : componentList)
component.display();
}
public void addComponent(Component comp){
componentList.add(comp);
}
public void removeComponent(Component comp){
componentList.remove(comp);
}
@Override
public boolean equals(Object obj){
if (obj instanceof Component){
Component component = (Component) obj;
return component.getName() != null && component.getName().equals(this.getName());
}
return false;
}
public static void main(String[] args){
Component window = new Component("WinForm(WINDOW窗口)");
window.addComponent(new Component("Picture(LOGO图片)"));
window.addComponent(new Component("Button(登录)"));
window.addComponent(new Component("Button(注册)"));
Component frame = new Component("Frame(FRAME1)");
window.addComponent(frame);
frame.addComponent(new Component("Label(用户名)"));
frame.addComponent(new Component("TextBox(文本框)"));
frame.addComponent(new Component("Label(密码)"));
frame.addComponent(new Component("PasswordBox(密码框)"));
frame.addComponent(new Component("CheckBox(复选框)"));
frame.addComponent(new Component("TextBox(记住用户名)"));
frame.addComponent(new Component("LinkLabel(忘记密码)"));
window.display();
}
}
评论 (1 条评论)