第三周 代码重构 作业一
发布于: 2020 年 10 月 04 日
1、请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
如图所示以静态内部类方式实现的单例模式
2、请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
示例代码实现如下
/** * 抽象组件 */public abstract class Component { protected String name; public Component(String name) { this.name = name; } /** * 窗口输出 */ public abstract void print();}
import java.util.ArrayList;import java.util.List;/** * 抽象容器 */public abstract class Container extends Component { private List<Component> children; public Container(String name) { super(name); } public void add(Component child) { if (children == null) { children = new ArrayList<>(); } children.add(child); } /** * 窗口输出容器 */ protected abstract void printSelf(); @Override public void print() { printSelf(); if (children != null) { children.forEach(Component::print); } }}
/** * window窗口 */public class WinForm extends Container { public WinForm(String name) { super(name); } @Override protected void printSelf() { System.out.println("print WinForm(" + name + ")"); }}
/** * 带边框容器 */public class Frame extends Container { public Frame(String name) { super(name); } @Override protected void printSelf() { System.out.println("print Frame(" + name + ")"); }}
/** * 图片 */public class Picture extends Component{ public Picture(String name) { super(name); } @Override public void print() { System.out.println("print Picture(" + name + ")"); }}
/** * 按钮 */public class Button extends Component{ public Button(String name) { super(name); } @Override public void print() { System.out.println("print Button(" + name + ")"); }}
public class Lable extends Component { public Lable(String name) { super(name); } @Override public void print() { System.out.println("print Label(" + name + ")"); }}
/** * 文本框 */public class TextBox extends Component { public TextBox(String name) { super(name); } @Override public void print() { System.out.println("print TextBox(" + name + ")"); }}
/** * 密码框 */public class PasswordBox extends Component { public PasswordBox(String name) { super(name); } @Override public void print() { System.out.println("print PasswordBox(" + name + ")"); }}
/** * 复选框 */public class CheckBox extends Component{ public CheckBox(String name) { super(name); } @Override public void print() { System.out.println("print CheckBox(" + name + ")"); }}
/** * 链接 */public class LinkLable extends Component{ public LinkLable(String name) { super(name); } @Override public void print() { System.out.println("print LinkLable(" + name + ")"); }}
示例输出代码如下
public class Main { public static void main(String[] args) { WinForm winForm = createLoginForm(); winForm.print(); } private static WinForm createLoginForm() { WinForm winForm = new WinForm("WINDOW窗口"); Picture logo = new Picture("LOGO图片"); winForm.add(logo); Button loginBtn = new Button("登录"); winForm.add(loginBtn); Button regBtn = new Button("注册"); winForm.add(regBtn); Frame frame = new Frame("FRAME1"); winForm.add(frame); Lable nameLable = new Lable("用户名"); frame.add(nameLable); TextBox nameBox = new TextBox("文本框"); frame.add(nameBox); Lable pwdLable = new Lable("密码"); frame.add(pwdLable); PasswordBox pwdBox = new PasswordBox("密码框"); frame.add(pwdBox); CheckBox rememberCb = new CheckBox("复选框"); frame.add(rememberCb); TextBox rememberBox = new TextBox("记住用户名"); frame.add(rememberBox); LinkLable linkLable = new LinkLable("忘记密码"); frame.add(linkLable); return winForm; }}
输出结果如下
print 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 LinkLable(忘记密码)
划线
评论
复制
发布于: 2020 年 10 月 04 日 阅读数: 21
版权声明: 本文为 InfoQ 作者【应鹏】的原创文章。
原文链接:【http://xie.infoq.cn/article/55c1b2353239e12f90b9f9d6f】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
应鹏
关注
还未添加个人签名 2020.08.25 加入
还未添加个人简介
评论