架构课第三周课后作业

发布于: 21 小时前
  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

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

/*组件类型枚举*/
public enum ComponentType {
FRAME,LABEL, TEXTBOX, PASSWORDBOX, CHECKBOX, WINFORM, LINKLABEL,PICTURE,BUTTON
}
public interface Printer {
public void print();
}
public class Component implements Printer{
private ComponentType componentType;
private String name;
private List<Component> children = new LinkedList<>();
public static Component of(ComponentType componentType,String name){
Component component = new Component();
component.componentType = componentType;
component.name = name;
return component;
}
public void addChildren(Component... component){
children.addAll(Arrays.asList(component));
}
public void addChild(Component component){
children.add(component);
}
public void print(){
System.out.println("print "+ this.componentType + " " + this.name);
children.forEach(
Component::print
);
}
}
public static void main(String[] args) {
Component frame = Component.of(ComponentType.FRAME,"FRAME1");
frame.addChildren(
Component.of(ComponentType.LABEL,"用户名"),
Component.of(ComponentType.TEXTBOX,"文本框"),
Component.of(ComponentType.LABEL,"密码"),
Component.of(ComponentType.PASSWORDBOX,"密码框"),
Component.of(ComponentType.CHECKBOX,"复选框"),
Component.of(ComponentType.TEXTBOX,"记住用户名"),
Component.of(ComponentType.LINKLABEL,"忘记密码"));
Component winForm = Component.of(ComponentType.WINFORM,"WINDOW窗口");
winForm.addChildren(
Component.of(ComponentType.PICTURE,"LOGO图片"),
Component.of(ComponentType.BUTTON,"登录"),
Component.of(ComponentType.BUTTON,"注册"),
frame);
winForm.print();
}
result:
print WINFORM WINDOW窗口
print PICTURE LOGO图片
print BUTTON 登录
print BUTTON 注册
print FRAME FRAME1
print LABEL 用户名
print TEXTBOX 文本框
print LABEL 密码
print PASSWORDBOX 密码框
print CHECKBOX 复选框
print TEXTBOX 记住用户名
print LINKLABEL 忘记密码

用户头像

张瑞浩

关注

还未添加个人签名 2018.09.18 加入

还未添加个人简介

评论

发布
暂无评论
架构课第三周课后作业