写点什么

架构师训练营第三周作业

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

1.单例模式代码

1.1 方法一 (饿汉式单例)





public class PersonSingleton {
private static PersonSingleton personSingleton = new PersonSingleton();
private PersonSingleton() {
}
public static PersonSingleton getInstance() {
return personSingleton;
}
}

1.2 饿汉式单例优缺点:



  • 优点:



不存在线程安全问题



  • 缺点



单例的初始化可能很耗费时间,有可能该单例一直不会被引用,初始化完全浪费资源



1.3 方法二(懒汉式单例)



public class PersonSingleton {
private static PersonSingleton personSingleton = null;
private PersonSingleton() {
}
public static synchronized PersonSingleton getInstance() {
if (personSingleton == null) {
personSingleton = new PersonSingleton();
}
return personSingleton;
}
}

1.4 懒汉式单例优缺点:

  • 优点



解决了使用时初始化的问题。



  • 缺点



由于使用了同步方法,多线程可能存在性能问题。



1.5方法三(懒汉式单例改进)





public class PersonSingleton {
private static PersonSingleton personSingleton = null;
private PersonSingleton() {
}
public static PersonSingleton getInstance() {
if (personSingleton == null) {
synchronized(PersonSingleton.class){
if (personSingleton == null) {
personSingleton = new PersonSingleton();
}
}
}
return personSingleton;
}
}

1.6 懒汉式单例改进优点

解决了使用时初始化的问题,同时解决了多线程访问的性能问题。



2. 组合模式



2.1 Container



//定义一个容器接口
public interface Container {
void draw();
}

2.2 Component

//定义一个抽象类实现容器接口,该类是所有控件的父类
public abstract class Component implements Container {
public abstract void add(Container container);
public abstract void remove(Container container);
}

2.3 WinForm

// 定义WinForm控件
public class WinForm extends Component {
private List<Container> containers = null;
private String name;
public WinForm(String name){
containers = new ArrayList<>();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println(getName());
if (containers.size()>0){
containers.forEach(c ->c.draw());
}
}
@Override
public void add(Container container) {
containers.add(container);
}
@Override
public void remove(Container container) {
containers.remove(container);
}
}

2.4 Button

//定义Button控件
public class Button extends Component {
private String name;
public Button(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(Container container) {
}
@Override
public void remove(Container container) {
}
@Override
public void draw() {
System.out.println(getName());
}
}

2.5 Frame

//定义Frame控件
public class Frame extends Component {
private String name;
private List<Container> containers = null;
public Frame(String name) {
this.name = name;
this.containers = new ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(Container container) {
containers.add(container);
}
@Override
public void remove(Container container) {
containers.remove(container);
}
@Override
public void draw() {
System.out.println(getName());
if(containers.size()>0){
containers.forEach(c->c.draw());
}
}
}

2.6 Label

//定义Label控件
public class Label extends Component {
private String name;
public Label(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(Container container) {
}
@Override
public void remove(Container container) {
}
@Override
public void draw() {
System.out.println(getName());
}
}

2.7 TextBox

//定义TextBox控件
public class TextBox extends Component {
private String name;
public TextBox(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(Container container) {
}
@Override
public void remove(Container container) {
}
@Override
public void draw() {
System.out.println(getName());
}
}

2.8 PasswordBox



//定义PasswordBox
public class PasswordBox extends Component {
private String name;
public PasswordBox(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(Container container) {
}
@Override
public void remove(Container container) {
}
@Override
public void draw() {
System.out.println(getName());
}
}

2.10 CheckBox

//定义CheckBox控件
public class CheckBox extends Component {
private String name;
public CheckBox(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(Container container) {
}
@Override
public void remove(Container container) {
}
@Override
public void draw() {
System.out.println(getName());
}
}

2.11 LinkLabel

//定义LinkLabel控件
public class LinkLabel extends Component {
private String name;
public LinkLabel(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(Container container) {
}
@Override
public void remove(Container container) {
}
@Override
public void draw() {
System.out.println(getName());
}
}

2.12 客户端调用

Component winForm = new WinForm("Windows窗口");
winForm.add(new Picture("LOGO图片"));
winForm.add(new Button("登录"));
winForm.add(new Button("注册"));
Component frame = new Frame("FRAME1");
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.add(frame);
winForm.draw();



2.13 输出结果



Windows窗口
LOGO图片
登录
注册
FRAME1
用户名
文本框
密码
密码框
复选框
记住用户名
忘记密码



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

关注

还未添加个人签名 2018.04.25 加入

还未添加个人简介

评论

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