架构师训练营作业 --Week3

发布于: 2020 年 06 月 23 日
架构师训练营作业 --Week3

手写Singleton模式类

double-checked singleton

组合设计模式练习

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

代码如下:

import java.util.ArrayList;
import java.util.List;
/**
* 组件抽象类,所有的gui组件都应当继承自此类。addComponent 和 removeComponent 方法只适用于
* 包含子组件的组件。
*/
abstract class Component {
protected String name = "Component";
void addComponent(final Component component) {
throw new UnsupportedOperationException("Unsupport add component.");
}
void removeComponent(final Component component) {
throw new UnsupportedOperationException("Unsupport remove component.");
}
void print() {
throw new UnsupportedOperationException("Unsupported print operation.");
}
String getName() {
return this.name;
}
}
class Button extends Component {
public Button(final String name) {
this.name = name;
}
@Override
void print() {
System.out.println("print Button(" + this.getName() + ")");
}
}
class Picture extends Component {
public Picture(final String name) {
this.name = name;
}
@Override
void print() {
System.out.println("print Picture(" + this.getName() + ")");
}
}
class Label extends Component {
public Label(final String name) {
this.name = name;
}
@Override
void print() {
System.out.println("print Label(" + this.getName() + ")");
}
}
class LinkLabel extends Component {
public LinkLabel(final String name) {
this.name = name;
}
@Override
void print() {
System.out.println("print LinkLabel(" + this.getName() + ")");
}
}
class TextBox extends Component {
public TextBox(final String name) {
this.name = name;
}
@Override
void print() {
System.out.println("print TextBox(" + this.getName() + ")");
}
}
class PasswordBox extends Component {
public PasswordBox(final String name) {
this.name = name;
}
@Override
void print() {
System.out.println("print PasswordBox(" + this.getName() + ")");
}
}
class Checkbox extends Component {
public Checkbox(final String name) {
this.name = name;
}
@Override
void print() {
System.out.println("print Checkbox(" + this.getName() + ")");
}
}
class Frame extends Component {
// child components
private final List<Component> components;
public Frame(final String name) {
this.name = name;
this.components = new ArrayList<>();
}
@Override
void addComponent(final Component comp) {
this.components.add(comp);
}
@Override
void removeComponent(final Component comp) {
this.components.remove(comp);
}
@Override
void print() {
System.out.println("print Frame(" + this.getName() + ")");
for (final Component comp : components) {
comp.print();
}
}
}
class WinForm extends Component {
// child components
private final List<Component> components;
public WinForm(final String name) {
this.name = name;
this.components = new ArrayList<>();
}
@Override
void addComponent(final Component comp) {
this.components.add(comp);
}
@Override
void removeComponent(final Component comp) {
this.components.remove(comp);
}
@Override
void print() {
System.out.println("print WinForm(" + this.getName() + ")");
for (final Component comp : components) {
comp.print();
}
}
}
public class CompositePatternDemo {
public static void main(final String[] args) {
final WinForm winForm = new WinForm("Window 窗口");
final Button btnLogin = new Button("登录");
winForm.addComponent(btnLogin);
final Button btnReg = new Button("注册");
winForm.addComponent(btnReg);
final Picture pic = new Picture("LOGO图片");
winForm.addComponent(pic);
Frame frame = new Frame("Frame1");
Label lblUser = new Label("用户名");
frame.addComponent(lblUser);
TextBox txtUser = new TextBox("文本框");
frame.addComponent(txtUser);
Label lblPwd = new Label("密码");
frame.addComponent(lblPwd);
PasswordBox pwdbox = new PasswordBox("密码框");
frame.addComponent(pwdbox);
Checkbox chk = new Checkbox("复选框");
frame.addComponent(chk);
Label lblRememberMe = new Label("记住用户名");
frame.addComponent(lblRememberMe);
LinkLabel lnkLbl = new LinkLabel("忘记密码");
frame.addComponent(lnkLbl);
winForm.addComponent(frame);
winForm.print();
}
}

输出结果:

print WinForm(Window 窗口)
print Button(登录)
print Button(注册)
print Picture(LOGO图片)
print Frame(Frame1)
print Label(用户名)
print TextBox(文本框)
print Label(密码)
print PasswordBox(密码框)
print Checkbox(复选框)
print Label(记住用户名)
print LinkLabel(忘记密码)

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

吴炳华

关注

还未添加个人签名 2020.04.08 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营作业 --Week3