架构师训练营第三周课后作业

用户头像
Cloud.
关注
发布于: 2020 年 06 月 23 日

手写单例模式

程序员的字真是没眼看,老师要求必须手写,手写了之后还是写了个代码版本

public class Pool {
private static volatile Pool pool ;
public static Pool get(){
if(Objects.isNull(pool)){
synchronized (Pool.class){
if(Objects.isNull(pool)){
pool = new Pool();
}
}
}
return pool;
}
}



用组合模式打印图中窗口

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





public class Composite {
public static void main(String[] args) {
Component winForm = new WinForm("WINDOW窗口");
winForm.add(new Picture("LOGO图片"));
winForm.add(new Button("登陆"));
winForm.add(new Button("注册"));
Component frame = new Frame("FRAME1");
winForm.add(frame);
frame.add(new Label("用户名"));
frame.add(new TextBox("文本框"));
frame.add(new Label("密码"));
frame.add(new PasswordBox("密码框"));
frame.add(new CheckBox("复选框"));
frame.add(new TextBox("记住用户名"));
frame.add(new LinkLabel("忘记密码"));
winForm.print();
}
private static abstract class Component {
private List<Component> components = new ArrayList<>();
public void add(Component component){
components.add(component);
}
public abstract String getName();
public void print() {
System.out.println("print " + getName());
components.forEach(Component::print);
}
}
private static class WinForm extends Component {
private String name;
public WinForm(String name) {
this.name = name;
}
@Override
public String getName() {
return "WinForm(" + name + ")";
}
}
private static class Picture extends Component {
private String name;
public Picture(String name) {
this.name = name;
}
@Override
public String getName() {
return "Picture(" + name + ")";
}
}
private static class Button extends Component {
private String name;
public Button(String name) {
this.name = name;
}
@Override
public String getName() {
return "Button(" + name + ")";
}
}
private static class Frame extends Component {
private String name;
public Frame(String name) {
this.name = name;
}
@Override
public String getName() {
return "Frame(" + name + ")";
}
}
private static class Label extends Component {
private String name;
public Label(String name) {
this.name = name;
}
@Override
public String getName() {
return "Label(" + name + ")";
}
}
private static class TextBox extends Component {
private String name;
public TextBox(String name) {
this.name = name;
}
@Override
public String getName() {
return "TextBox(" + name + ")";
}
}
private static class PasswordBox extends Component {
private String name;
public PasswordBox(String name) {
this.name = name;
}
@Override
public String getName() {
return "PasswordBox(" + name + ")";
}
}
private static class CheckBox extends Component {
private String name;
public CheckBox(String name) {
this.name = name;
}
@Override
public String getName() {
return "CheckBox(" + name + ")";
}
}
private static class LinkLabel extends Component {
private String name;
public LinkLabel(String name) {
this.name = name;
}
@Override
public String getName() {
return "LinkLabel(" + name + ")";
}
}
}

程序输出结果:

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(忘记密码)



发布于: 2020 年 06 月 23 日 阅读数: 50
用户头像

Cloud.

关注

还未添加个人签名 2020.05.14 加入

还未添加个人简介

评论

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