架构师训练营 第三周 作业

用户头像
极客
关注
发布于: 2020 年 06 月 24 日

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

完成情况:DCL双检查锁机制的单例模式(Java)



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





完成情况如下:

1、概念

组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户可以使用一致的方法操作单个对象和组合对象。

组合关系与聚合关系的区别:

(1)组合关系:在古代皇帝三宫六院,贵妃很多,但是每一个贵妃只属于皇帝。

(2)聚合关系:一个老师有很多学生,但是每一个学生又属于多个老师。



Container.java

public abstract class Container {
private String name;
private String type;
public Container(String name,String type) {
super();
this.name = name;
this.type=type;
}
public abstract void add(Container form);
public abstract void remove(Container form);
public abstract void display(int depth);
public String getName() {
return name;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setName(String name) {
this.name = name;
}
}



Control.java

public class Control extends Container {
public Control(String name,String type) {
super(name,type);
}
@Override
public void add(Container form) {
}
@Override
public void remove(Container form) {
}
@Override
public void display(int depth) {
for (int i = 0; i < depth; i++) {
System.out.print(" ");
}
System.out.println("print "+getType()+ "("+getName()+")");
}
}



Frame.java

public class Frame extends Container {
private List<Container> children = new ArrayList<>();
public Frame(String name,String type) {
super(name,type);
}
@Override
public void add(Container form) {
children.add(form);
}
@Override
public void remove(Container form) {
children.remove(form);
}
@Override
public void display(int depth) {
for (int i = 0; i < depth; i++) {
System.out.print(" ");
}
System.out.println("print "+getType()+ "("+getName()+")");
for (Container form : children) {
form.display(depth + 2);
}
}
}



Winform.java

public class Winform extends Container {
private List<Container> children = new ArrayList<>();
public Winform(String name,String type) {
super(name,type);
}
@Override
public void add(Container form) {
children.add(form);
}
@Override
public void remove(Container form) {
children.remove(form);
}
@Override
public void display(int depth) {
for (int i = 0; i < depth; i++) {
System.out.print(" ");
}
System.out.println("print "+getType()+ "("+getName()+")");
for (Container container : children) {
container.display(depth + 2);
}
}
}



Main.java

public class Main {
public static void main(String[] args) {
// write your code here
Winform winForm = new Winform("Windows窗口","Windows");
Frame frame = new Frame("FRAME1","Frame");
Control lflogo = new Control("LOGO","Picture");
Control lflogin = new Control("登录","Button");
Control lfregister = new Control("注册","Button");
Control cUname = new Control("用户名","Label");
Control cUnameVal = new Control("用户名","TextBox");
Control cPass = new Control("密码","Label");
Control cPassVal = new Control("密码","TextBox");
Control remLabel = new Control("记住用户名","Label");
Control remChkBox = new Control("记住用户名","CheckBox");
Control linkLabel = new Control("忘记密码","LinkLabel");
winForm.add(lflogo);
winForm.add(lflogin);
winForm.add(lfregister);
winForm.add(frame);
frame.add(cUname);
frame.add(cUnameVal);
frame.add(cPass);
frame.add(cPassVal);
remLabel.add(remChkBox);
frame.add(remLabel);
frame.add(linkLabel);
winForm.display(0);
}
}

程序输出如下:

"D:\Program Files\Java\jdk1.8.0_111\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:26376,suspend=y,server=n -javaagent:C:\Users\L\AppData\Local\JetBrains\IntelliJIdea2020.1\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\bcprov-jdk15on-1.54.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\javassist-3.12.0.GA.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\local_policy.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\xom-1.2.10.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\WorkSpace\Private\Java\架构练习\dev\out\production\dev;D:\Program Files\ideaIU-2020.1.2.win\lib\idea_rt.jar" com.company.Main
Connected to the target VM, address: '127.0.0.1:26376', transport: 'socket'
print Windows(Windows窗口)
print Picture(LOGO)
print Button(登录)
print Button(注册)
print Frame(FRAME1)
print Label(用户名)
print TextBox(用户名)
print Label(密码)
print TextBox(密码)
print Label(记住用户名)
print LinkLabel(忘记密码)
Disconnected from the target VM, address: '127.0.0.1:26376', transport: 'socket'

Process finished with exit code 0




用户头像

极客

关注

还未添加个人签名 2018.03.29 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营 第三周 作业