Week3:作业一

用户头像
车小勺的男神
关注
发布于: 2020 年 07 月 05 日

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



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

Window 抽象接口

package org.example;
public interface Window {
void print();
}

叶子节点组件

package org.example;
public class FinalComponent implements Window {
protected String name;
public FinalComponent(String name) {
this.name = name;
}
public void print() {
System.out.println("print: " + name);
}
}

中间节点组件

package org.example;
import java.util.ArrayList;
import java.util.List;
public class PanelComponent implements Window {
protected List<Window> windows;
protected String name;
public PanelComponent(String name) {
this.name = name;
windows = new ArrayList<Window>();
}
public void addSubComponent(Window component) {
windows.add(component);
}
public void print() {
System.out.println("print: " + name);
for (Window component: windows) {
component.print();
}
}
}

main 函数

package org.example;

public class RunPrint {

public static void main(String[] args)
{
PanelComponent frame1 = new PanelComponent("Frame(FRAME1)");
frame1.addSubComponent(new FinalComponent("Lable(用户名)"));
frame1.addSubComponent(new FinalComponent("TextBox(文本框)"));
frame1.addSubComponent(new FinalComponent("Lable(密码)"));
frame1.addSubComponent(new FinalComponent("PasswordBox(密码框)"));
frame1.addSubComponent(new FinalComponent("CheckBox(复选框)"));
frame1.addSubComponent(new FinalComponent("TextBox(记住用户名)"));
frame1.addSubComponent(new FinalComponent("LinkLable(忘记密码)"));

PanelComponent winForm = new PanelComponent("WinForm(WINDOW窗口)");
winForm.addSubComponent(new FinalComponent("Picture(LOGO图片)"));
winForm.addSubComponent(new FinalComponent("Button(登录)"));
winForm.addSubComponent(new FinalComponent("Button(注册)"));
winForm.addSubComponent(frame1);

winForm.print();
}
}

运行结果



用户头像

车小勺的男神

关注

还未添加个人签名 2018.11.07 加入

还未添加个人简介

评论

发布
暂无评论
Week3:作业一