架构师第 3 周作业

发布于: 2020 年 06 月 24 日

作业:1手写单例模式并拍照

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

解答第2题如下:

步骤1新建java project工程如图:

步骤2建立抽象类Component,代码如下:

package com.geekbang.design.CompositePattern;
public abstract class Component {
public void add(Component component){
throw new UnsupportedOperationException("不支持添加操作");
}
public void remove(Component component){
throw new UnsupportedOperationException("不支持删除操作");
}
public void print(){
throw new UnsupportedOperationException("不支持打印操作");
}
}

步骤3建立实现类Frame,Panel,WinForm分别继承于Cmponent

package com.geekbang.design.CompositePattern;
import com.geekbang.design.CompositePattern.Component;
import java.util.ArrayList;
import java.util.List;
public class Frame extends Component {
private List<Component> items = new ArrayList<>();
private String name;
public Frame(String name) {
this.name = name;
}
@Override
public void add(Component component) {
items.add(component);
}
@Override
public void remove(Component component) {
items.remove(component);
}
@Override
public void print() {
System.out.println(this.name);
for (Component component : items) {
component.print();
}
}
}

package com.geekbang.design.CompositePattern;
import com.geekbang.design.CompositePattern.Component;
public class Panel extends Component {
private String name;
public Panel(String name){
this.name = name;
}
@Override
public void print() {
System.out.println(name);
}
}

import com.geekbang.design.CompositePattern.Component;
import java.util.ArrayList;
import java.util.List;
public class WinForm extends Component {
private List<Component> items = new ArrayList<>();
private String name;
public WinForm(String name) {
this.name = name;
}
@Override
public void add(Component component) {
items.add(component);
}
@Override
public void remove(Component component) {
items.remove(component);
}
@Override
public void print() {
System.out.println(this.name);
for (Component component : items) {
component.print();
}
}
}

步骤4书写Test测试类

package com.geekbang.design.CompositePattern;
public class Test {
public static void main(String[] args) {
Component winForm = new WinForm("print WinForm(WINDOW窗口)");
Component picture = new Panel("print Picture(LOGO图片)");
Component signIn = new Panel("pint Button(登录)");
Component signUp = new Panel("print Button(注册)");
Component frame = new Frame("print Frame(FRAME1)");
Component lable1 = new Panel("print Lable(用户名)");
Component textBox = new Panel("print TextBox(文本框)");
Component lable2 = new Panel("print Lable(密码)");
Component passwordBox = new Panel("print PasswordBox(密码框)");
Component checkBox = new Panel("print CheckBox(复选框)");
Component textBox2 = new Panel("print TextBox(记住用户名)");
Component linkLable = new Panel("print LinkLable(忘记密码)");
frame.add(lable1);
frame.add(textBox);
frame.add(lable2);
frame.add(passwordBox);
frame.add(checkBox);
frame.add(textBox2);
frame.add(linkLable);
winForm.add(picture);
winForm.add(signIn);
winForm.add(signUp);
winForm.add(frame);
winForm.print();
}
}

输出结果如下图所示:

用户头像

zgscy100

关注

还未添加个人签名 2018.02.28 加入

还未添加个人简介

评论

发布
暂无评论
架构师第3周作业