写点什么

架构师训练营第三周作业

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

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



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





interface Component {
/**
* 打印
*/
void print();
/**
* 新增子节点
* @param component
*/
void addSubNote(Component component);
/**
* 获取子节点集合
* @return
*/
List<Component> getSubNotes();
}
public abstract class AbstractComponent implements Component {
private String name;
private List<Component> subNotes;
public AbstractComponent(String name){
this.name = name;
this.subNotes = new ArrayList<Component>();
}
/**
* 打印
*/
public abstract void print() ;
public void addSubNote(Component component) {
this.subNotes.add(component);
}
public List<Component> getSubNotes() {
return this.subNotes;
}
public String getName() {
return name;
}
}
public class WinFormComponent extends AbstractComponent {
public WinFormComponent(String name) {
super(name);
}
@Override
public void print() {
System.out.println(String.format("WinForm(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class PictureComponent extends AbstractComponent {
public PictureComponent(String name){
super(name);
}
@Override
public void print() {
System.out.println(String.format("Picture(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class FrameComponent extends AbstractComponent {
public FrameComponent(String name) {
super(name);
}
@Override
public void print() {
System.out.println(String.format("Frame(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class LabelComponent extends AbstractComponent {
public LabelComponent(String name) {
super(name);
}
@Override
public void print() {
System.out.println(String.format("Label(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class TextBoxComponent extends AbstractComponent {
public TextBoxComponent(String name) {
super(name);
}
@Override
public void print() {
System.out.println(String.format("TextBox(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class PasswordBoxComponent extends AbstractComponent {
public PasswordBoxComponent(String name) {
super(name);
}
@Override
public void print() {
System.out.println(String.format("PasswordBox(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class CheckBoxComponent extends AbstractComponent {
public CheckBoxComponent(String name) {
super(name);
}
@Override
public void print() {
System.out.println(String.format("CheckBox(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class LinkLabelComponent extends AbstractComponent {
public LinkLabelComponent(String name) {
super(name);
}
@Override
public void print() {
System.out.println(String.format("LinkLabel(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
public class ButtonComponent extends AbstractComponent {
public ButtonComponent(String name){
super(name);
}
@Override
public void print() {
System.out.println(String.format("Button(%s)",this.getName()));
for (Component component : getSubNotes()){
component.print();
}
}
}
//测试
public class Demo {
public static void main(String[] args) {
Component window = new WinFormComponent("WINDOW窗口");
Component logo = new PictureComponent("LOGO图片");
window.addSubNote(logo);
Component frame1 = new FrameComponent("FRAME1");
window.addSubNote(frame1);
Component username = new LabelComponent("用户名");
frame1.addSubNote(username);
Component usernameTextBox = new TextBoxComponent("文本框");
frame1.addSubNote(usernameTextBox);
Component password = new LabelComponent("密码");
frame1.addSubNote(password);
Component passwordBox = new PasswordBoxComponent("密码框");
frame1.addSubNote(passwordBox);
Component checkBox = new CheckBoxComponent("复选框");
frame1.addSubNote(checkBox);
Component rememberMe = new LabelComponent("记住用户名");
frame1.addSubNote(rememberMe);
Component forgetPassword = new LinkLabelComponent("忘记密码");
frame1.addSubNote(forgetPassword);
Component login = new ButtonComponent("登陆");
Component register = new ButtonComponent("注册");
window.addSubNote(login);
window.addSubNote(register);
window.print();
}
}

输入内容

WinForm(WINDOW窗口)
Picture(LOGO图片)
Frame(FRAME1)
Label(用户名)
TextBox(文本框)
Label(密码)
PasswordBox(密码框)
CheckBox(复选框)
Label(记住用户名)
LinkLabel(忘记密码)
Button(登陆)
Button(注册)



用户头像

好名字

关注

还未添加个人签名 2018.09.08 加入

还未添加个人简介

评论

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