架构课第三周课后作业
发布于: 2020 年 06 月 24 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 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 FRAME1print LABEL 用户名print TEXTBOX 文本框print LABEL 密码print PASSWORDBOX 密码框print CHECKBOX 复选框print TEXTBOX 记住用户名print LINKLABEL 忘记密码
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 50
张瑞浩
关注
还未添加个人签名 2018.09.18 加入
还未添加个人简介
评论