【架构师训练营第 1 期】第三周作业
发布于: 2020 年 10 月 05 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树形结构如图 2 所示,打印输出示例参考图 3
各个组件的定义:
public class Component {
private String text;
public Component(String text){
this.text=text;
}
public void draw(){
System.out.println("print "+this.getClass().getSimpleName()+"("+this.text+")");
}
}
public class Container extends Component {
List<Component> components = null;
public Container(String text) {
super(text);
components = new ArrayList<>();
}
@Override
public void draw() {
super.draw();
int size = components.size();
for (int i = 0; i < size; i++) {
components.get(i).draw();
}
}
public void addComponent(Component component) {
this.components.add(component);
}
}
public class Button extends Component {
public Button(String text) {
super(text);
}
}
public class Label extends Component {
public Label(String text) {
super(text);
}
}
public class Picture extends Component {
public Picture(String text) {
super(text);
}
}
public class CheckBox extends Component {
public CheckBox(String text) {
super(text);
}
}
public class TextBox extends Component {
public TextBox(String text) {
super(text);
}
}
public class PasswordBox extends Component {
public PasswordBox(String text) {
super(text);
}
}
public class Linkable extends Component {
public Linkable(String text) {
super(text);
}
}
public class WinForm extends Container{
public WinForm(String text) {
super(text);
}
}
public class Frame extends Container{
public Frame(String text) {
super(text);
}
}
复制代码
主程序:
public class HomeWork {
public static void main(String[] args) {
Container winForm = new WinForm("WINDOW窗口");
winForm.addComponent(new Picture("LOGO图片"));
winForm.addComponent(new Button("登录"));
winForm.addComponent(new Button("注册"));
Frame frame = new Frame("FRAME1");
frame.addComponent(new Label("用户名"));
frame.addComponent(new TextBox("文本框"));
frame.addComponent(new Label("密码"));
frame.addComponent(new PasswordBox("密码框"));
frame.addComponent(new CheckBox("复选框"));
frame.addComponent(new Label("记住用户名"));
frame.addComponent(new Linkable("忘记密码"));
winForm.addComponent(frame);
winForm.draw();
}
}
复制代码
划线
评论
复制
发布于: 2020 年 10 月 05 日阅读数: 45
知鱼君
关注
还未添加个人签名 2018.03.26 加入
还未添加个人简介
评论