写点什么

Week3 作业

用户头像
TiK
关注
发布于: 2020 年 06 月 23 日

1 手写单例模式



注意事项:

私有化构造方法

对外暴露获取对象方法。

如果采用懒汉式,注意多线程问题,采用双检索机制。

2 组合模式编写控件程序

用组合模式编写程序,打印树状结构。



Component实现
public abstract class Component {
private String name;
public Component(String name) {
this.name = name;
}
protected abstract void print();
public String getName() {
return name;
}
public void add(Component component) {
throw new UnsupportedOperationException();
}
public void remove(Component component) {
throw new UnsupportedOperationException();
}
}
Container实现
package camp.week3;
import java.util.ArrayList;
import java.util.List;
public abstract class Container extends Component {
private List<Component> components = new ArrayList<>();
public Container(String name) {
super(name);
}
public void add(Component component) {
components.add(component);
}
public void remove(Component component) {
components.remove(component);
}
@Override
protected void print() {
System.out.println("print ".concat(this.getClass().getSimpleName()).concat("(").concat(getName()).concat(")"));
for (Component component : components) {
component.print();
}
}
}



Widget实现
public abstract class Widget extends Component {
public Widget(String name) {
super(name);
}
@Override
protected void print() {
System.out.println("print ".concat(this.getClass().getSimpleName()).concat("(").concat(getName()).concat(")"));
}
}
WinForm 与 Frame
package camp.week3;
public class WinForm extends Container {
public WinForm(String name) {
super(name);
}
}
Label, Picture....
package camp.week3;
public class Picture extends Widget{
public Picture(String name) {
super(name);
}
}
Client
package camp.week3;
public class Client {
public static void main(String[] args) {
Component winForm = new WinForm("Window窗口");
Picture pic = new Picture("LOGO图片");
winForm.add(pic);
winForm.add(new Button("登录"));
winForm.add(new Button("注册"));
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();
}
}



用户头像

TiK

关注

还未添加个人签名 2018.04.26 加入

还未添加个人简介

评论

发布
暂无评论
Week3 作业