写点什么

week3 作业

用户头像
雪涛公子
关注
发布于: 2020 年 06 月 21 日

1.单例模式



2.组合模式

Tree接口:顶层抽象,只定义show()方法。

public interface Tree {

void show();
}



TreeNode类:treeList变量存储子节点,显示的时候,如果是Node,则继续遍历子节点。

public class TreeNode implements Tree {

private List<Tree> treeList = new ArrayList<>();

private String name;

public TreeNode(String name) {
this.name = name;
}

public void addLeaf(Tree tree) {
treeList.add(tree);
}

@Override
public void show() {
System.out.println("print " + name);
for (Tree tree : treeList) {
tree.show();
}
}
}



TreeLeaf类:叶子节点,输出名称。

public class TreeLeaf implements Tree {

private String name;

public TreeLeaf(String name) {
this.name = name;
}

@Override
public void show() {
System.out.println("print " + name);
}
}



Tester类:

public class TreeTester {

public static void main(String[] args) {
TreeNode winForm = new TreeNode("WinForm(WINDOW窗口)");
TreeLeaf logo = new TreeLeaf("Picture(LOGO图片)");
winForm.addLeaf(logo);

TreeLeaf login = new TreeLeaf("BUTTON(登录)");
winForm.addLeaf(login);

TreeLeaf register = new TreeLeaf("BUTTON(注册)");
winForm.addLeaf(register);

TreeNode frame = new TreeNode("Frame(FRAME1)");
TreeLeaf LabelUsername = new TreeLeaf("LABEL(用户名)");
frame.addLeaf(LabelUsername);

TreeLeaf textBoxUsername = new TreeLeaf("TextBox(文本框)");
frame.addLeaf(textBoxUsername);

TreeLeaf LabelPwd = new TreeLeaf("Label(密码)");
frame.addLeaf(LabelPwd);

TreeLeaf pwdBox = new TreeLeaf("PasswordBox(密码框)");
frame.addLeaf(pwdBox);

TreeLeaf checkBox = new TreeLeaf("CheckBox(复选框)");
frame.addLeaf(checkBox);

TreeLeaf textBoxRememberUsername = new TreeLeaf("TextBox(记住用户名)");
frame.addLeaf(textBoxRememberUsername);

TreeLeaf linkLabel = new TreeLeaf("LinkLabel(忘记密码)");
frame.addLeaf(linkLabel);

winForm.addLeaf(frame);
winForm.show();
}
}



输出:

print WinForm(WINDOW窗口)
print Picture(LOGO图片)
print BUTTON(登录)
print BUTTON(注册)
print Frame(FRAME1)
print LABEL(用户名)
print TextBox(文本框)
print Label(密码)
print PasswordBox(密码框)
print CheckBox(复选框)
print TextBox(记住用户名)
print LinkLabel(忘记密码)



用户头像

雪涛公子

关注

还未添加个人签名 2017.11.20 加入

还未添加个人简介

评论

发布
暂无评论
week3 作业