写点什么

训练营第三周

用户头像
Just顾
关注
发布于: 2020 年 06 月 25 日

1.单例模式



2.请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。





//打印器接口
public interface Painter {
void paint(Component component);
}
//组件接口
public interface Component {
String getName();
default void print(Painter painter) {
painter.paint(this);
}
}
//窗口组件
public class WinForm implements Component {
private List<Component> childs = new ArrayList<>();
private String name;
public WinForm(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
public void addChild(Component component) {
childs.add(component);
}
@Override
public void print(Painter painter) {
painter.paint(this);
childs.forEach(c -> c.print(painter));
}
}
//frame 组件
public class Frame implements Component {
private String name;
private List<Component> childs = new ArrayList<>();
public Frame(String name) {
this.name = name;
}
public void addChild(Component component) {
childs.add(component);
}
@Override
public void print(Painter painter) {
painter.paint(this);
childs.forEach(painter::paint);
}
@Override
public String getName() {
return this.name;
}
}
public class Button implements Component {
private String name;
public Button(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class CheckBox implements Component {
private String name;
public CheckBox(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class Lable implements Component {
private String name;
public Lable(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class LinkLable implements Component {
private String name;
public LinkLable(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
//打印器
public class PainterImpl implements Painter {
@Override
public void paint(Component component) {
System.err
.println(String.format("print %s (%s)", component.getClass().getSimpleName(), component.getName()));
}
}
public class PasswordBox implements Component {
private String name;
public PasswordBox(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class Picture implements Component {
private String name;
public Picture(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class TextBox implements Component {
private String name;
public TextBox(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class Week3Work2 {
public static void main(String[] args) {
Painter painter = new PainterImpl();
WinForm winForm = new WinForm("WINDOW窗口");
winForm.addChild(new Picture("LOGO 图片"));
winForm.addChild(new Button("登录"));
winForm.addChild(new Button("注册LOGO 图片"));
Frame frame1 = new Frame("FRAME1");
winForm.addChild(frame1);
frame1.addChild(new Lable("用户名"));
frame1.addChild(new TextBox("文本框"));
frame1.addChild(new Lable("密码"));
frame1.addChild(new PasswordBox("密码框"));
frame1.addChild(new CheckBox("复选框"));
frame1.addChild(new TextBox("记住用户名"));
frame1.addChild(new TextBox("忘记密码"));
winForm.print(painter);
}
}



用户头像

Just顾

关注

还未添加个人签名 2018.05.06 加入

还未添加个人简介

评论

发布
暂无评论
训练营第三周