week3- 作业
发布于: 2020 年 06 月 24 日
1. 请在草稿纸上手写一个单例模式的实现代码。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出
IComponent
public interface IComponent { void print();}
public class Component implements IComponent { protected String name; public Component(String name) { this.name = name; } @Override public void print() { System.out.println("组件 " + name); }}
public abstract class Container extends Component { public Container(String name) { super(name); } List<Component> components = new ArrayList<>(); public void add(Component component) { components.add(component); } @Override public void print() { System.out.println("容器 " + name); for (Component component : components) { component.print(); } }}
各容器实现
public class WinForm extends Container{ public WinForm(String name) { super(name); }}public class Frame extends Container{ public Frame(String name) { super(name); }}
各组件实现
public class Button extends Component { public Button(String name) { super(name); }}public class CheckBox extends Component { public CheckBox(String name) { super(name); }}public class Label extends Component { public Label(String name) { super(name); }}public class LinkLabel extends Component { public LinkLabel(String name) { super(name); }}public class PasswordBox extends Component { public PasswordBox(String name) { super(name); }}public class Picture extends Component { public Picture(String name) { super(name); }}public class TextBox extends Component { public TextBox(String name) { super(name); }}
测试实现
public class Test { public static void main(String[] args) { WinForm winForm = new WinForm("WINDOW窗体"); Component picture = new Picture("LOGO图片"); Component login = new Picture("登录"); Component reg = new Button("注册"); winForm.add(picture); winForm.add(login); winForm.add(reg); Frame frame = new Frame("FRAME1"); frame.add(new Label("用户名")); frame.add(new TextBox("文本框")); frame.add(new Label("密码")); frame.add(new PasswordBox("密码框")); frame.add(new CheckBox("复选框")); frame.add(new TextBox("记住用户名")); frame.add(new LinkLabel("忘记密码")); winForm.add(frame); winForm.print(); }}
运行结果
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 44
暖丶冬
关注
还未添加个人签名 2018.11.09 加入
还未添加个人简介
评论