写点什么

Week 3 命题作业

用户头像
balsamspear
关注
发布于: 2020 年 10 月 18 日

第 1 题:请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。



第 2 题:请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。



组件类:Component

@Data
public class Component {
private String name;
private String content;
private List<Component> subComponentList;
public Component(String name, String content) {
this.name = name;
this.content = content;
}
public void addSubComponent(Component component) {
if (subComponentList == null) {
subComponentList = new ArrayList<>();
}
subComponentList.add(component);
}
public void print() {
System.out.println("print " + name + "(" + content + ")");
if (subComponentList != null && subComponentList.size() > 0) {
subComponentList.forEach(Component::print);
}
}
}

客户端(使用组件类)

public class Client {
public static void main(String[] args) {
Component winForm = new Component("WinForm", "WINDOW窗口");
Component picture = new Component("Picture", "LOGO图片");
Component button1 = new Component("Button", "登录");
Component button2 = new Component("Button", "注册");
Component frame = new Component("Frame", "FRAME1");
Component label1 = new Component("Label", "用户名");
Component textBox1 = new Component("TextBox", "文本框");
Component label2 = new Component("Label", "密码");
Component passwordBox = new Component("PasswordBox", "密码框");
Component checkBox = new Component("CheckBox", "复选框");
Component textBox2 = new Component("TextBox", "记住用户名");
Component linkLabel = new Component("LinkLabel", "忘记密码");
winForm.addSubComponent(picture);
winForm.addSubComponent(button1);
winForm.addSubComponent(button2);
winForm.addSubComponent(frame);
frame.addSubComponent(label1);
frame.addSubComponent(textBox1);
frame.addSubComponent(label2);
frame.addSubComponent(passwordBox);
frame.addSubComponent(checkBox);
frame.addSubComponent(textBox2);
frame.addSubComponent(linkLabel);
winForm.print();
}
}

输出结果



用户头像

balsamspear

关注

还未添加个人签名 2019.10.24 加入

还未添加个人简介

评论

发布
暂无评论
Week 3命题作业