写点什么

架构师训练营第三周作业

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

单例模式

单例模式保证产生单一实例,一个类只产生一个实例。为什么需要单例模式,从性能上来说,可以减少实例频繁的创建和销毁带来的资源消耗;从功能上来说,当多个用户使用这个实例的时候,便于统一控制。下面是手写的饿汉式与懒汉式的单例模式。

单例模式-饿汉式

单例模式-懒汉式

组合模式

组合模式可以解决树形结构这种场景的问题,如窗口结构如图2所示,需要打印出图1的窗口

WindowComponent

public interface WindowComponent {
public void draw();
}

ContainerComponent

public class ContainerComponent implements WindowComponent {
private String name;
public ContainerComponent(String name) {
this.name = name;
}
private List<WindowComponent> windowComponentList = new ArrayList<WindowComponent>();
public void draw() {
System.out.println(this.name);
for (WindowComponent windowComponent: windowComponentList) {
windowComponent.draw();
}
}
public void addComponent(WindowComponent windowComponent) {
windowComponentList.add(windowComponent);
}
}

LeafComponent

public class LeafComponent implements WindowComponent {
private String name;
public LeafComponent(String name) {
this.name = name;
}
public void draw() {
System.out.println(this.name);
}
}

Client

public class Client {
public static void main(String[] args) {
ContainerComponent windowsComponent = new ContainerComponent("print WinForm(WINDOWS窗口)");
LeafComponent pictureComponent = new LeafComponent("print Picture(LOGO图片)");
LeafComponent loginButtonComponent = new LeafComponent("print Button(登录)");
LeafComponent registerButtonComponent = new LeafComponent("print Button(注册)");
ContainerComponent frameComponent = new ContainerComponent("print Frame(FRAME1)");
windowsComponent.addComponent(pictureComponent);
windowsComponent.addComponent(loginButtonComponent);
windowsComponent.addComponent(registerButtonComponent);
windowsComponent.addComponent(frameComponent);
LeafComponent usernameLabelComponent = new LeafComponent("print Label(用户名)");
LeafComponent usernameTextBoxComponent = new LeafComponent("print TextBox(文本框)");
LeafComponent passwordLabelComponent = new LeafComponent("print Label(密码)");
LeafComponent passwordTextBoxComponent = new LeafComponent("print PasswordBox(密码框)");
LeafComponent checkBoxComponent = new LeafComponent("print CheckBox(复选框)");
LeafComponent textBoxComponent = new LeafComponent("print TextBox(记住用户名)");
LeafComponent linkLabelComponent = new LeafComponent("print LinkLabel(忘记密码)");
frameComponent.addComponent(usernameLabelComponent);
frameComponent.addComponent(usernameTextBoxComponent);
frameComponent.addComponent(passwordLabelComponent);
frameComponent.addComponent(passwordTextBoxComponent);
frameComponent.addComponent(checkBoxComponent);
frameComponent.addComponent(textBoxComponent);
frameComponent.addComponent(linkLabelComponent);
windowsComponent.draw();
}
}

结果输出

print WinForm(WINDOWS窗口)
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 月 24 日阅读数: 48
用户头像

olderwei

关注

还未添加个人签名 2018.04.26 加入

还未添加个人简介

评论

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