架构师 1 期 - 代码重构作业

用户头像
ltl3884
关注
发布于: 2020 年 10 月 08 日





先定义一个component接口

public interface Component {
public void draw();
}

再定义两个实现类 Node 和 Leaf 继承 Component 接口,分别表示中间节点和叶子节点

public class Node implements Component {
private String name;
public Node(String name) {
this.name = name;
}
List<Component> components = new ArrayList<Component>();
@Override
public void draw() {
System.out.println("print " + name);
for (Component c : components) {
c.draw();
}
}
public void addComponent(Component IComponent) {
components.add(IComponent);
}
}



public class Leaf implements Component {
private String name;
public Leaf() {
}
public Leaf(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println("print " + name);
}
}



frame和window继承node

picture,button等继承leaf

public class Frame extends Node {
private String name;
public Frame(String name) {
super("Frame(" + name + ")");
}
}



public class Picture extends Leaf {
private String name;
public Picture(String name) {
super("Picture(" + name + ")");
}
}



最后写一个测试类

public class Test {
public static void main(String[] args) {
Window window = new Window("WINDOW窗口");
Picture picture = new Picture("LOGO图片");
Frame frame = new Frame("FRAME1");
Button login = new Button("登录");
Button signUp = new Button("注册");
Lable username = new Lable("用户名");
TextBox textBox = new TextBox("文本框");
Lable password = new Lable("密码");
PasswordBox passwordBox = new PasswordBox("密码框");
CheckBox checkBox = new CheckBox("复选框");
TextBox rememberMe = new TextBox("记住用户名");
LinkLable linkLable = new LinkLable("忘记密码");
frame.addComponent(username);
frame.addComponent(textBox);
frame.addComponent(password);
frame.addComponent(passwordBox);
frame.addComponent(checkBox);
frame.addComponent(rememberMe);
frame.addComponent(linkLable);
window.addComponent(picture);
window.addComponent(login);
window.addComponent(signUp);
window.addComponent(frame);
window.draw();
}
}

输出如下

print Window(WINDOW窗口)
print Picture(LOGO图片)
print Button(登录)
print Button(注册)
print Frame(FRAME1)
print Lable(用户名)
print TextBox(文本框)
print Lable(密码)
print PasswordBox(密码框)
print CheckBox(复选框)
print TextBox(记住用户名)
print LinkLable(忘记密码)



用户头像

ltl3884

关注

还未添加个人签名 2017.12.08 加入

还未添加个人简介

评论

发布
暂无评论
架构师1期-代码重构作业