写点什么

Week3

发布于: 2020 年 06 月 24 日

1. DCL写法单例模式



  1. 定义一个Component类,包含两个属性type、subComponents,分别表示当前Component的类型(名称)和当前Component的子Component。

package com.example.week3;

import java.util.ArrayList;
import java.util.List;

public class Component {

private String type;

private List<Component> subComponents;

public Component(String type) {
this.type = type;
}

public void draw() {
System.out.println(type);
if (subComponents != null && subComponents.size() > 0) {
for (Component subComponent : subComponents) {
subComponent.draw();
}
}
}

public void addComponent(Component component) {
if (subComponents == null) {
subComponents = new ArrayList<>();
}
subComponents.add(component);
}

public void addComponents(List<Component> components) {
if (subComponents == null) {
subComponents = new ArrayList<>();
}
subComponents.addAll(components);
}
}




package com.example.week3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

public static void main(String[] args) {
Component label = new Component("label[username]");
Component textBox = new Component("text box");
Component labelPwd = new Component("label[password]");
Component pwdBox = new Component("PasswordBox");
Component checkBox = new Component("CheckBox");
Component textBoxRememberUsername = new Component("TextBox");
Component linkTable = new Component("LinkTable");

List<Component> componentList = new ArrayList<>();
componentList.add(label);
componentList.add(textBox);
componentList.add(labelPwd);
componentList.add(pwdBox);
componentList.add(checkBox);
componentList.add(textBoxRememberUsername);
componentList.add(linkTable);

Component frame = new Component("frame");
frame.addComponents(componentList);

Component picture = new Component("picture");
Component button = new Component("button");
Component button2 = new Component("button register");

List<Component> formComponents = Arrays.asList(picture, button, button2, frame);

Component form = new Component("form");
form.addComponents(formComponents);

form.draw();
}
}




用户头像

还未添加个人签名 2018.04.26 加入

还未添加个人简介

评论

发布
暂无评论
Week3