第 03 周 重构软件代码 命题作业

发布于: 2020 年 06 月 23 日
第03周 重构软件代码 命题作业

作业1. 请在草稿纸上手写一个单例模式的实现代码.

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

public abstract class Component {
private String name;
public Component(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void add(Component component) {
throw new UnsupportedOperationException("不支持添加操作.");
}
public void remove(Component component) {
throw new UnsupportedOperationException("不支持删除操作.");
}
public abstract void print();
}
public class WindowForm extends Component {
List<Component> list = Lists.newArrayList();
public WindowForm(String name) {
super(name);
}
@Override
public void add(Component component) {
list.add(component);
}
@Override
public void remove(Component component) {
list.remove(component);
}
@Override
public void print() {
System.out.println("print WindowForm(" + this.getName() + ")");
for (Component component : list) {
component.print();
}
}
}
public class Frame extends Component {
List<Component> list = Lists.newArrayList();
public Frame(String name) {
super(name);
}
@Override
public void add(Component component) {
list.add(component);
}
@Override
public void remove(Component component) {
list.remove(component);
}
@Override
public void print() {
System.out.println("print Frame(" + this.getName() + ")");
for (Component component : list) {
component.print();
}
}
}
public class Picture extends Component {
public Picture(String name) {
super(name);
}
@Override
public void print() {
System.out.println("print Picture(" + this.getName() + ")");
}
}
public abstract class Box extends Component {
public Box(String name) {
super(name);
}
public abstract void print();
}
public class CheckBox extends Box {
public CheckBox(String name) {
super(name);
}
@Override
public void print() {
System.out.println("print CheckBox(" + this.getName() + ")");
}
}
public class PasswordBox extends Box {
public PasswordBox(String name) {
super(name);
}
@Override
public void print() {
System.out.println("print PasswordBox(" + this.getName() + ")");
}
}
public class TextBox extends Box {
public TextBox(String name) {
super(name);
}
@Override
public void print() {
System.out.println("print TextBox(" + this.getName() + ")");
}
}
public class Button extends Component {
public Button(String name) {
super(name);
}
@Override
public void print() {
System.out.println("print Button(" + this.getName() + ")");
}
}
public class Lable extends Component {
public Lable(String name) {
super(name);
}
@Override
public void print() {
System.out.println("print Lable(" + this.getName() + ")");
}
}
public class LinkLable extends Lable {
public LinkLable(String name) {
super(name);
}
@Override
public void print() {
System.out.println("print LinkLable(" + this.getName() + ")");
}
}
public class Main {
public static void main(String[] args) {
Component windowFormComponent = new WindowForm("WINDOW窗口");
windowFormComponent.add(new Picture("LOGO图片"));
windowFormComponent.add(new Button("登录"));
windowFormComponent.add(new Button("注册"));
Component frameComponent = new Frame("FRAME1");
frameComponent.add(new Lable("用户名"));
frameComponent.add(new TextBox("文本框"));
frameComponent.add(new Lable("密码"));
frameComponent.add(new PasswordBox("密码框"));
frameComponent.add(new CheckBox("复选框"));
frameComponent.add(new TextBox("记住用户名"));
frameComponent.add(new LinkLable("忘记密码"));
windowFormComponent.add(frameComponent);
windowFormComponent.print();
}
}

用户头像

Jaye

关注

还未添加个人签名 2018.01.23 加入

还未添加个人简介

评论

发布
暂无评论
第03周 重构软件代码 命题作业