架构师训练营作业 (第三周)
发布于: 2020 年 06 月 24 日
作业一:
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
1)、定义接口
package com.demo;/** * @create 2020-06-24 20:42 */public interface CustomView { /** * 输出View 信息 */ void print(); /** * 获取ViewName * @return */ String getViewName(); /** * 设置 viewName * @param viewName */ void setViewName(String viewName); /** * 添加customView * @param customView */ void addCustomView(CustomView customView);}
2)、定义基类
package com.demo;import java.util.ArrayList;import java.util.List;/** * @create 2020-06-24 20:50 */public class CustomBaseView implements CustomView{ private String viewName; private List<CustomView> childs=new ArrayList<>(); @Override public void print() { System.out.println(String.format("print %s(%s)", this.getClass().getSimpleName(), getViewName())); for(CustomView customView: childs){ customView.print(); } } @Override public String getViewName() { return this.viewName; } @Override public void setViewName(String viewName) { this.viewName=viewName; } @Override public void addCustomView(CustomView customView) { if(customView !=null){ childs.add(customView); } }}
3)、定义9个具体的 View类,都只有类定义,不需要具体代码:
WinFormCustomView、ButtonCustomView、CheckBoxCustomView、FrameCustomView、LabelCustomView、LinkLabelCustomView、PassowrdBoxCustomView、PictureCustomView、TextBoxCustomView
package com.demo;/** * @create 2020-06-24 21:11 */public class WinFormCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class ButtonCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class CheckBoxCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class FrameCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class LabelCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class LinkLabelCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class PassowrdBoxCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class PictureCustomView extends CustomBaseView {}/** * @create 2020-06-24 21:11 */public class TextBoxCustomView extends CustomBaseView {}
4)、写测试方法
package com.demo;/** * @create 2020-06-24 21:16 */public class Test { public static void main(String[] args) throws IllegalAccessException, InstantiationException { Test test=new Test(); // 声明主窗体 CustomView mainForm = new WinFormCustomView(); mainForm.setViewName("WINDOW窗口"); // 添加子控件 test.addChilds(mainForm); // 绘画整个窗体 test.mainForm.print(); } private <T> CustomView createChild(String name, Class<T> clasz) throws IllegalAccessException, InstantiationException { CustomView child = (CustomView) clasz.newInstance(); child.setViewName(name); return child; } private void addChilds(CustomView mainForm) throws InstantiationException, IllegalAccessException { mainForm.addCustomView(createChild("LOGO图片", PictureCustomView.class)); mainForm.addCustomView(createChild("登录", ButtonCustomView.class)); mainForm.addCustomView(createChild("注册", ButtonCustomView.class)); CustomView frame = createChild("FRAME1", FrameCustomView.class); mainForm.addCustomView(frame); frame.addCustomView(createChild("用户名", LabelCustomView.class)); frame.addCustomView(createChild("文本框", TextBoxCustomView.class)); frame.addCustomView(createChild("密码", LabelCustomView.class)); frame.addCustomView(createChild("密码框", PasswordBoxCustomView.class)); frame.addCustomView(createChild("复选框", CheckBoxCustomView.class)); frame.addCustomView(createChild("记住用户名", TextBoxCustomView.class)); frame.addCustomView(createChild("忘记密码", LinkLabelCustomView.class)); }}
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 50
小遵
关注
还未添加个人签名 2018.06.16 加入
还未添加个人简介
评论