写点什么

架构师训练营 - 第 3 周命题作业

用户头像
红了哟
关注
发布于: 2020 年 06 月 27 日
架构师训练营 - 第 3 周命题作业

1、单例模式手写

(1)、单例懒汉线程不安全



(2)、单例懒汉线程安全



(3)、单例饿汉线程安全



(4)、单例双检锁



2、组合模式应用



组件Component类

public class Component {
private String typeName;
private String chineseName;
private List<Component> subComponents;
public Component(String typeName,String chineseName){
this.typeName = typeName;
this.chineseName = chineseName;
this.subComponents = new ArrayList<>();
}
public boolean add(Component component){
return this.subComponents.add(component);
}
public void print(){
System.out.println(String.format("print %s(%s)",this.typeName,this.chineseName));
for (Component subComponent : subComponents) {
subComponent.print();
}
}
}



main方法执行使用组合

public static void main(String[] args) {
Component winForm = new Component("WinForm","WINDOW窗口");
Component logoPicture = new Component("Picture","LOGO图片");
winForm.add(logoPicture);
Component loginButton = new Component("Button","登录");
winForm.add(loginButton);
Component registerButton = new Component("Button","注册");
winForm.add(registerButton);
Component frame = new Component("Frame","FRAME1");
winForm.add(frame);
Component userNameLable = new Component("Lable","用户名");
frame.add(userNameLable);
Component textBox = new Component("TextBox","文本框");
frame.add(textBox);
Component passwordLable = new Component("Lable","密码");
frame.add(passwordLable);
Component passwordBox = new Component("PasswordBox","密码框");
frame.add(passwordBox);
Component checkBox = new Component("CheckBox","复选框");
frame.add(checkBox);
Component saveName = new Component("TextBox","记住用户名");
frame.add(saveName);
Component removeName = new Component("LinkLable","忘记密码");
frame.add(removeName);
winForm.print();
}



执行结果:



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

红了哟

关注

还未添加个人签名 2019.08.15 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营 - 第 3 周命题作业