第三周架构课程作业
发布于: 2020 年 06 月 24 日
1、请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
答:写了懒汉单例方式,如图:
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
答:在开发过程中,除了组合模式,还综合使用了模板模式,提取基类方法,提取泛型实例化方法,编译测试通过。
a、定义接口 InterfaceControl
package com.cd;import java.util.List;public interface InterfaceControl { /** * 输出控件信息 */ void print(); /** * 返回当前控件名 * * @return 控件名 */ String getName(); /** * 设置当前控件名 */ void setName(String name); /** * 返回子控件清单,不需要子控件时,要重写抛出异常 * * @return 控件清单 */ List<InterfaceControl> getSubs(); /** * 添加子结点,不需要子控件时,要重写抛出异常 * * @param InterfaceControl 结点 * @return 返回子结点个数 */ int addSub(InterfaceControl interfaceControl);}
b、定义基类 BaseControl,抽取出通用模板方法
package com.cd;import java.util.ArrayList;import java.util.List;public abstract class BaseControl implements InterfaceControl { private List<InterfaceControl> Subs = new ArrayList<>(); private String name; @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } @Override public List<InterfaceControl> getSubs() { return Subs; } @Override public void print() { System.out.println(String.format("print %s(%s)", this.getClass().getSimpleName(), getName())); List<InterfaceControl> Subs = getSubs(); if (Subs != null) { for (InterfaceControl ctl : Subs) { ctl.print(); } } } @Override public int addSub(InterfaceControl interfaceControl) { List<InterfaceControl> Subs = getSubs(); if (Subs == null) { return 0; } if (interfaceControl != null) { Subs.add(interfaceControl); } return Subs.size(); }}
c、定义9个具体的控件类,都只有类定义,不需要具体代码:
WinForm、Button、CheckBox、Frame、Label、LinkLabel、PassowrdBox、Picture、TextBox
package com.cd;public class WinForm extends BaseControl {}public class Button extends BaseControl {}public class CheckBox extends BaseControl {}public class Frame extends BaseControl {}public class Label extends BaseControl {}public class LinkLabel extends BaseControl {}public class PassowrdBox extends BaseControl {}public class Picture extends BaseControl {}public class TextBox extends BaseControl {}
d、定义完毕,下面是main主调方法:
package com.cd;public class Main { public static void main(String[] args) throws IllegalAccessException, InstantiationException { // 声明主窗体 InterfaceControl mainForm = new WinForm(); mainForm.setName("WINDOW窗口"); // 添加子控件 addSubs(mainForm); // 绘画整个窗体 mainForm.print(); } static <T> InterfaceControl createSub(String name, Class<T> clasz) throws IllegalAccessException, InstantiationException { InterfaceControl Sub = (InterfaceControl) clasz.newInstance(); Sub.setName(name); return Sub; } static void addSubs(InterfaceControl mainForm) throws InstantiationException, IllegalAccessException { mainForm.addSub(createSub("LOGO图片", Picture.class)); mainForm.addSub(createSub("登录", Button.class)); mainForm.addSub(createSub("注册", Button.class)); InterfaceControl frame = createSub("FRAME1", Frame.class); mainForm.addSub(frame); frame.addSub(createSub("用户名", Label.class)); frame.addSub(createSub("文本框", TextBox.class)); frame.addSub(createSub("密码", Label.class)); frame.addSub(createSub("密码框", PasswordBox.class)); frame.addSub(createSub("复选框", CheckBox.class)); frame.addSub(createSub("记住用户名", TextBox.class)); frame.addSub(createSub("忘记密码", LinkLabel.class)); }}
e、执行结果如下:
"C:\Program Files\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=52379:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;D:\work\JavaProject\WinDeom\out\production\WinDeom" cn.beinet.Mainprint 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(忘记密码)Process finished with exit code 0
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 53
dj_cd
关注
还未添加个人签名 2019.08.09 加入
还未添加个人简介
评论