架构师训练营第三周作业

发布于: 2020 年 06 月 21 日

1、手写单例模式

1-1、提前初始化

减少同步调用开销;

1-2、延迟初始化

为了保证线程安全,用synchronized关键字修饰方法,带来的问题是在性能问题,每次只能一个线程访问;

1-3、双重加锁检查

双重加锁检查,将synchronized的范围缩小,提升性能,但是需要注意必须使用 volatile关键字 修饰变量,在保证“独占性”的前提下,也要保证“可见性”;

2、组合模式打印窗口组件

/**
* 控件
*/
public interface Control {
/**
* 打印
*/
void print();
}
/**
* 按钮
*/
public class Button implements Control {
private final String name;
public Button(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}
/**
* 复选框
*/
public class CheckBox implements Control {
private final String name;
public CheckBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}
/**
* Frame框架
*/
public class Frame implements Control {
private final String name;
private List<Control> subControls;
public Frame(String name) {
this.name = name;
this.subControls = new ArrayList<>();
}
public void addChild(Control control) {
subControls.add(control);
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
if (subControls != null && subControls.size() != 0) {
for (Control control : subControls) {
control.print();
}
}
}
}
/**
* 标签
*/
public class Label implements Control {
private final String name;
public Label(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}
/**
* 超链接
*/
public class Linkable implements Control {
private final String name;
public Linkable(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}
/**
* 密码框
*/
public class PasswordBox implements Control {
private final String name;
public PasswordBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}
/**
* 图片
*/
public class Picture implements Control {
private final String name;
public Picture(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}
/**
* 文本框
*/
public class TextBox implements Control {
private final String name;
public TextBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}
package org.geektime.design.pattern;
import java.util.ArrayList;
import java.util.List;
/**
* 窗口
*/
public class WinForm implements Control {
private final String name;
private List<Control> subControls;
public WinForm(String name) {
this.name = name;
this.subControls = new ArrayList<>();
}
public void addChild(Control control) {
subControls.add(control);
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
if (subControls != null && subControls.size() != 0) {
for (Control control : subControls) {
control.print();
}
}
}
public static void main(String[] args) {
WinForm winForm = new WinForm("WINDOW窗口");
Control logo = new Picture("LOGO图片");
winForm.addChild(logo);
Control login = new Button("登录");
winForm.addChild(login);
Control register = new Button("注册");
winForm.addChild(register);
Frame frame = new Frame("FRAME1");
Control username = new Label("用户名");
frame.addChild(username);
Control textBox = new TextBox("文本框");
frame.addChild(textBox);
Control password = new Label("密码");
frame.addChild(password);
Control passwordBox = new PasswordBox("密码框");
frame.addChild(passwordBox);
Control checkbox = new CheckBox("复选框");
frame.addChild(checkbox);
Control rememberPassword = new Label("记住密码");
frame.addChild(rememberPassword);
Control forgetPassword = new Linkable("忘记密码");
frame.addChild(forgetPassword);
winForm.addChild(frame);
winForm.print();
}
}

程序输出结果

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

Albert

关注

还未添加个人签名 2018.08.31 加入

还未添加个人简介

评论

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