写点什么

架构师训练营第三周作业

用户头像
张锐
关注
发布于: 2020 年 06 月 21 日

1.手写单例模式,并上传图片

2.代码实现打印窗口

public interface Component {
void print();
String getName();
void addSub(Component c);
}

定义接口,具象的窗口部件扩展实现Component接口

public abstract class AbstractComponent implements Component {
private String name;
public AbstractComponent(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + getName() + ")");
}
@Override
public String getName() {
return this.name;
}
}

抽象类,print方法通用实现,放在父类中。

import java.util.ArrayList;
import java.util.List;
public class WinForm extends AbstractComponent {
List<Component> children = new ArrayList<>();
public WinForm(String name) {
super(name);
}
@Override
public void print() {
super.print();
for (Component c : children)
c.print();
}
@Override
public String getName() {
return "WINDOW窗口";
}
@Override
public void addSub(Component c) {
children.add(c);
}
}



public class Picture extends AbstractComponent {
public Picture(String name) {
super(name);
}
@Override
public void addSub(Component c) {}
}



public class Button extends AbstractComponent {
public Button(String name) {
super(name);
}
@Override
public void addSub(Component c) {
}
}



import java.util.ArrayList;
import java.util.List;
public class Frame extends AbstractComponent {
List<Component> children = new ArrayList<>();
public Frame(String name) {
super(name);
}
@Override
public void print() {
super.print();
for (Component c : children) {
c.print();
}
}
@Override
public void addSub(Component c) {
children.add(c);
}
}



public class Label extends AbstractComponent {
public Label(String name) {
super(name);
}
@Override
public void addSub(Component c) {}
}



public class TextBox extends AbstractComponent {
public TextBox(String name) {
super(name);
}
@Override
public void addSub(Component c) {}
}



public class PasswordBox extends AbstractComponent {
public PasswordBox(String name) {
super(name);
}
@Override
public void addSub(Component c) {}
}



public class CheckBox extends AbstractComponent {
public CheckBox(String name) {
super(name);
}
@Override
public void addSub(Component c) {}
}



public class LinkLabel extends AbstractComponent {
public LinkLabel(String name) {
super(name);
}
@Override
public void addSub(Component c) {}
}



public class DrawWindow {
public static void main(String args[]) {
Component winForm = new WinForm("WINDOW窗口");
winForm.addSub(new Picture("LOGO图片"));
winForm.addSub(new Button("登陆"));
winForm.addSub(new Button("注册"));
Component frame1 = new Frame("FRAME1");
frame1.addSub(new Label("用户名"));
frame1.addSub(new TextBox("文本框"));
frame1.addSub(new Label("密码"));
frame1.addSub(new PasswordBox("密码框"));
frame1.addSub(new CheckBox("复选框"));
frame1.addSub(new TextBox("记住用户名"));
frame1.addSub(new LinkLabel("忘记密码"));
winForm.addSub(frame1);
winForm.print();
}
}

编写测试类

测试结果

用户头像

张锐

关注

还未添加个人签名 2018.08.07 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第三周作业