第三周课后作业
发布于: 2020 年 06 月 21 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
使用double check lock写法
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
1、定义两个接口类型:View和ViewContainer
public interface View { void print();}public interface ViewContainer extends View { void add(View... views);}
2、为了实现组合,定义一个抽象类
public abstract class AbstractViewContainer implements ViewContainer { private final List<View> viewList = new ArrayList<>(); @Override public void add(View... views) { if (Objects.nonNull(views)) { viewList.addAll(Arrays.asList(views)); } } @Override public void print() { viewList.forEach(View::print); }}
3、实现具体类
3.1、类继承关系
3.2、具体代码
public class WinForm extends AbstractViewContainer { private final String title; public WinForm(String title) { this.title = title; } @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(" + title + ")"); super.print(); }}public class Frame extends AbstractViewContainer { private final String title; public Frame(String title) { this.title = title; } @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(" + title + ")"); super.print(); }}
public class Button implements View { private final String title; public Button(String title) { this.title = title; } @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(" + title + ")"); }}public class CheckBox implements View { @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(复选框)"); }}public class Label implements View { private final String content; public Label(String content) { this.content = content; } @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(" + content + ")"); }}public class LinkLabel extends Label { public LinkLabel(String content) { super(content); }}public class PasswordBox implements View { @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(密码框)"); }}public class Picture implements View { private final String pictureName; public Picture(String pictureName) { this.pictureName = pictureName; } @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(" + pictureName + ")"); }}public class TextBox implements View { private final String title; public TextBox(String title) { this.title = title; } @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(" + title + ")"); }}
4、测试
public static void main(String[] args) { View forgetLabel = new LinkLabel("忘记密码"); View rememberBox = new TextBox("记住密码"); View checkBox = new CheckBox(); View passwordBox = new PasswordBox(); View passwordLabel = new Label("密码"); View textBox = new TextBox("文本框"); View usernameLabel = new Label("用户名"); ViewContainer frame = new Frame("FRAME1"); frame.add(usernameLabel, textBox, passwordLabel, passwordBox, checkBox, rememberBox, forgetLabel); View picture = new Picture("LOGO图片"); View signInButton = new Button("登录"); View signUpButton = new Button("注册"); ViewContainer winForm = new WinForm("WINDOW窗口"); winForm.add(picture, signInButton, signUpButton, frame); winForm.print(); }
划线
评论
复制
发布于: 2020 年 06 月 21 日 阅读数: 49
版权声明: 本文为 InfoQ 作者【iHai】的原创文章。
原文链接:【http://xie.infoq.cn/article/7123b62396f81f96050b030ea】。文章转载请联系作者。
iHai
关注
还未添加个人签名 2018.07.26 加入
还未添加个人简介
评论