架构师第三周作业
发布于: 2020 年 12 月 13 日
作业一:
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
From接口
package cn.ty.architect.tranining;public interface From { public void draw();}
WinForm
package cn.ty.architect.tranining;import java.util.ArrayList;import java.util.List;public class WinFrom implements From { private final String name; private final List<From> froms = new ArrayList<From>(); public WinFrom(String name){ this.name =name; } public void addFrom(From from){ froms.add(from); } @Override public void draw() { System.out.println("print "+this.getClass().getSimpleName() +"(" + name + ")"); froms.forEach(from -> from.draw()); }}
Frame类
package cn.ty.architect.tranining;public class Frame extends WinFrom { private String name; public Frame(String name){ super(name); }}
Button类(Lable,TxtBox等其他组件类跟Button基本一致,不在此赘述)
package cn.ty.architect.tranining;public class Button implements From{ private final String name; public Button(String name){ this.name = name; } @Override public void draw(){ System.out.println("print "+this.getClass().getSimpleName() +"(" + name + ")"); }}
DrawDesktop测试类:
package cn.ty.architect.tranining;public class DrawDesktop { public static void main(String[] args) { // 绘制WinForm WinFrom winFrom = new WinFrom("WINDOW窗口"); From picture = new Picture("LOGO图片"); From logoinBtn = new Button("登录"); From registerBtn = new Button("注册"); WinFrom frame = new WinFrom("FRAME1"); winFrom.addFrom(picture); winFrom.addFrom(logoinBtn); winFrom.addFrom(registerBtn); winFrom.addFrom(frame); // 绘制FRAME1 From userNameLable = new Lable("用户名"); From userNameTxt = new TextBox("文本框"); From passwordLable = new Lable("密码"); From passwordTxt = new PasswordBox("密码框"); From rememberCheckBox = new CheckBox("复选框"); From rememberTxt = new TextBox("记住用户名"); From forgetLink = new LinkLable("忘记密码"); frame.addFrom(userNameLable); frame.addFrom(userNameTxt); frame.addFrom(passwordLable); frame.addFrom(passwordTxt); frame.addFrom(rememberCheckBox); frame.addFrom(rememberTxt); frame.addFrom(forgetLink); //绘制桌面窗口 winFrom.draw(); }}
运行结果如下:
print WinFrom(WINDOW窗口)print Picture(LOGO图片)print Button(登录)print Button(注册)print WinFrom(FRAME1)print Lable(用户名)print TextBox(文本框)print Lable(密码)print PasswordBox(密码框)print CheckBox(复选框)print TextBox(记住用户名)print LinkLable(忘记密码)Process finished with exit code 0
划线
评论
复制
发布于: 2020 年 12 月 13 日阅读数: 17
ty
关注
还未添加个人签名 2020.11.06 加入
还未添加个人简介
评论