作业 3

发布于: 2020 年 06 月 24 日
  1. 手写单例模式实现

  1. 组合模式实现

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();

}

}

用户头像

annie

关注

还未添加个人签名 2018.04.27 加入

还未添加个人简介

评论

发布
暂无评论
作业3