week002 作业
发布于: 2020 年 06 月 24 日
1. 请在草稿纸上手写一个单例模式的实现代码
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
代码如下:
测试代码
public class WinFormTest { public static void main(String[] args) { WinForm winForm = new WinForm(); winForm.element.add(new FormPicture("LOGO图片")); winForm.element.add(new FormButton("登录")); winForm.element.add(new FormButton("注册")); FormFrame frame1 = new FormFrame("Frame1"); frame1.frameElement.add(new FrameLable("用户名")); frame1.frameElement.add(new FrameTextBox("文本框")); frame1.frameElement.add(new FrameLable("密码")); frame1.frameElement.add(new FramePasswordBox("密码框")); frame1.frameElement.add(new FrameCheckBox("复选框")); frame1.frameElement.add(new FrameTextBox("记住用户名")); frame1.frameElement.add(new LinkLable("忘记密码")); winForm.element.add(frame1); winForm.display(); }}
组件代码:
WinForm组件:
public class WinForm { List<WinForm> element = new LinkedList<WinForm>(); public void display() { System.out.printf("print WinForm(%s)","WINDOW窗口"); System.out.println(); element.stream().forEach(a->a.display()); }}
按钮组件:
public class FormButton extends WinForm { private String label; public FormButton(String label) { this.label = label; } @Override public void display() { System.out.printf("print Button %s", label); System.out.println(); }}
图片组件:
public class FormPicture extends WinForm{ private String formPicture; public FormPicture(String formPicture) { this.formPicture = formPicture; } @Override public void display() { System.out.println("Print Picture(" + formPicture + ")"); }}
Frame组件:
public class FormFrame extends WinForm { List<FormFrame> frameElement = new LinkedList<>(); private String frameName; public FormFrame() {} public FormFrame(String frameName) { this.frameName = frameName; } @Override public void display() { System.out.printf("print Frame(%s)",frameName); frameElement.stream().forEach(a->a.display()); }}
Frame的Box组件:(想用策略+工厂做,未完成)
public class FrameBox extends FormFrame { protected String boxName; public FrameBox(String boxName) { this.boxName = boxName; } }
checkBox组件:
public class FrameCheckBox extends FormFrame { private String checkBox; public FrameCheckBox(String checkBox) { this.checkBox = checkBox; } @Override public void display() { System.out.println(); System.out.printf("print CheckBox(%s)",checkBox); } }
passwordBox组件:
public class FramePasswordBox extends FrameBox { public FramePasswordBox(String boxName) { super(boxName); } @Override public void display() { System.out.println(); System.out.printf("print PasswordBox(%s)",boxName); } }
TextBox组件:
public class FrameTextBox extends FrameBox { public FrameTextBox(String boxName) { super(boxName); } @Override public void display() { System.out.println(); System.out.printf("print TextBox(%s)",boxName); } }
Label组件:
public class FrameLable extends FormFrame{ private String lable; public FrameLable(String lable) { this.lable = lable; } @Override public void display() { System.out.println(); System.out.printf("print Lable(%s)",lable); } }
Linkable组件:
public class LinkLable extends FormFrame { private String linkAble; public LinkLable(String linkAble) { this.linkAble = linkAble; } @Override public void display() { System.out.println(); System.out.printf("print CheckBox(%s)",linkAble); } }
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 56
徐培
关注
还未添加个人签名 2018.10.31 加入
还未添加个人简介
评论