写点什么

架构师训练营 - 第三周 - 作业 1

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

1.

请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

2.

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

//Draw
public interface Draw {
void draw();
}
//WinForm
public class WinForm implements Draw {
private List<Draw> elements = new ArrayList<>();
private String content;
public WinForm(String content) {
this.content = content;
}
public void addElement(Draw element) {
elements.add(element);
}
@Override
public void draw() {
System.out.println("print WinForm(" + content + ")");
for (Draw element : elements) {
element.draw();
}
}
}
//Picture
public class Picture implements Draw {
private String content;
public Picture(String content) {
this.content = content;
}
@Override
public void draw() {
System.out.println("print Picture(" + content + ")");
}
}
//Button
public class Button implements Draw {
private String content;
public Button(String content) {
this.content = content;
}
@Override
public void draw() {
System.out.println("print Button(" + this.content + ")");
}
}
//Frame
public class Frame implements Draw {
private List<Draw> elements = new ArrayList<>();
private String content;
public Frame(String content) {
this.content = content;
}
public void addElement(Draw element) {
elements.add(element);
}
@Override
public void draw() {
System.out.println("print Frame(" + this.content + ")");
for (Draw draw : elements) {
draw.draw();
}
}
}
//Lable
public class Lable implements Draw {
private String content;
public Lable(String content) {
this.content = content;
}
@Override
public void draw() {
System.out.println("print Lable(" + this.content + ")");
}
}
//TextBox
public class TextBox implements Draw{
private String content;
public TextBox(String content) {
this.content = content;
}
@Override
public void draw() {
System.out.println("print TextBox("+ this.content +")");
}
}
//PasswordBox
public class PasswordBox implements Draw{
private String content;
public PasswordBox(String content) {
this.content = content;
}
@Override
public void draw() {
System.out.println("print PasswordBox("+ content +")");
}
}
//CheckBox
public class CheckBox implements Draw{
private String content;
public CheckBox(String content) {
this.content = content;
}
@Override
public void draw() {
System.out.println("print CheckBox(" + content + ")");
}
}
//LinkLable
public class LinkLable implements Draw {
private String content;
public LinkLable(String content) {
this.content = content;
}
@Override
public void draw() {
System.out.println("print LinkLable(" + this.content + ")");
}
}
//Client
public class Client {
public static void main(String[] args) {
WinForm winForm = new WinForm("WINDOW窗口");
Picture picture = new Picture("LOGO图片");
Button loginBtn = new Button("登录");
Button registBtn = new Button("注册");
Frame frame = new Frame("FRAME1");
winForm.addElement(picture);
winForm.addElement(loginBtn);
winForm.addElement(registBtn);
winForm.addElement(frame);
Lable usernameLab = new Lable("用户名");
TextBox textBox = new TextBox("文本框");
Lable passwordLab = new Lable("密码");
PasswordBox passwordBox = new PasswordBox("密码框");
CheckBox checkBox = new CheckBox("复选框");
TextBox remenberBox = new TextBox("记住用户名");
LinkLable linkLable = new LinkLable("忘记密码");
frame.addElement(usernameLab);
frame.addElement(textBox);
frame.addElement(passwordLab);
frame.addElement(passwordBox);
frame.addElement(checkBox);
frame.addElement(remenberBox);
frame.addElement(linkLable);
winForm.draw();
}
}



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

seng man

关注

还未添加个人签名 2018.12.04 加入

还未添加个人简介

评论

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