week03 作业
发布于: 2020 年 06 月 24 日
1.手撸单例模式
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图
/*** 接口类*/public interface Component { void print();}/*** 抽象父类 实现公共模板方法*/public abstract class BaseComponent { private List<Component> components = new ArrayList(); public Component addComponent(Component component) { components.add(component); return this; } protected abstract void print0(); @Override public void print() { print0(); for(Component component: components) { component.print0(); } }}/*** 标题相关抽象父类 */public abstract class BaseNameComponent extend BaseComponent { private String name; @Override protected void print0() { System.out.println(name); }}/*** 具体组件* start* ============================*/public class WinForm extend BaseComponent { @Override protected void print0() { System.out.println("winForm"); }}public class Picture extend BaseComponent { @Override protected void print0() { System.out.println("winForm"); }}public class Button extend BaseNameComponent { public Button(String name) { super(name); }}public class Frame extend BaseComponent { @Override protected void print0() { System.out.println("winForm"); }}public class Label extend BaseComponent { public Label(String name) { super(name); }}public class TextBox extend BaseNameComponent { public TextBox(String name) { super(name); }}public class PasswordBox extend BaseComponent { @Override protected void print0() { System.out.println("PasswordBox"); }}public class CheckBox extend BaseComponent { @Override protected void print0() { System.out.println("CheckBox"); }}public class LinkLabel extend BaseComponent { @Override protected void print0() { System.out.println("LinkLabel"); }}/*** 具体组件* end* ============================*/public class main { public static void main(String[] args) { Component window = new WindowForm() .addCompont(new Picture()) .addCompont(new Button("登陆")) .addCompont(new Button("注册")) .addCompont( new Frame() .addCompont(new Label("用户名")) .addCompont(new TextBox("文本框")) .addCompont(new Label("密码")) .addCompont(new PasswordBox()) .addCompont(new Checkbox()) .addCompont(new LinkLabel()) ); window.print(); }}
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 71
Safufu
关注
还未添加个人签名 2018.11.16 加入
还未添加个人简介
评论