架构师训练营 W3 作业
作者:Kun
- 2020-06-24
本文字数:1054 字
阅读完需:约 3 分钟
手写单例
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
// abstract classpublic abstract class Component { protected String name; public void print() { System.out.println("print " + getClass().getName() .substring(getClass().getName().lastIndexOf(".") + 1) + "(" + name + ")"); }
protected void add(Component component) { throw new UnsupportedOperationException(); }}
public class WindowForm extends Component { private List<Component> childrenComponent = new ArrayList<>();
public WindowForm(String name) { this.name = name; }
@Override public void print() { super.print(); childrenComponent.forEach(c->c.print()); }
@Override public void add(Component component) { childrenComponent.add(component); }}
public class Button extends Component { public Button(String name) { this.name = name; } @Override public void print() { super.print(); }}
//clientpublic class Client { public static void main(String[] args) { WindowForm window = new WindowForm("Window窗口"); Picture pic = new Picture("LOGO图片"); Button logon = new Button("登录"); Button register = new Button("注册"); Frame frame = new Frame("FRAME1"); Label userNameLabel = new Label("用户名"); Label pwdLabel = new Label("密码"); TextBox userNameTextBox = new TextBox("用户名文本框"); PasswordBox pwdBox = new PasswordBox("密码框"); CheckBox checkBox = new CheckBox("复选框"); Label rememberMe = new Label("记住用户名"); LinkLabel forgetPwd = new LinkLabel("忘记密码"); window.add(pic); window.add(logon); window.add(register); window.add(frame); frame.add(userNameLabel); frame.add(userNameTextBox); frame.add(pwdLabel); frame.add(pwdBox); frame.add(checkBox); frame.add(rememberMe); frame.add(forgetPwd); window.print(); }}
复制代码
划线
评论
复制
发布于: 2020-06-24阅读数: 58
版权声明: 本文为 InfoQ 作者【Kun】的原创文章。
原文链接:【http://xie.infoq.cn/article/d79b6e210d255c74ccc3d4f81】。未经作者许可,禁止转载。
Kun
关注
Life is short. 2018-01-13 加入
Software Developer










评论