写点什么

架构师训练营第 1 期 -week3

用户头像
习习
关注
发布于: 2020 年 10 月 04 日

题目



类图



winForm,frame属于容器类,其余控件属于基础控件类





代码实现



Printer接口



package com.geektime;
public interface Printer {
/**
* 打印
*/
void print();
/**
* 增加子元素
* @param printer printer
*/
void add(Printer printer);
}



Container类



package com.geektime;
import java.util.ArrayList;
/**
* 容器类
*/
public class Container implements Printer {
private ArrayList<Printer> printers = new ArrayList<>();
private String name;
public Container(String name) {
this.name = name;
}
@Override
public void add(Printer printer) {
printers.add(printer);
}
@Override
public void print() {
PrintUtil.printControlName(name);
printers.forEach(Printer::print);
}
}



BaseControl类



package com.geektime;
/**
* 基础控件类
*/
public class BaseControl implements Printer {
private String name;
public BaseControl(String name) {
this.name = name;
}
@Override
public void print() {
PrintUtil.printControlName(name);
}
@Override
public void add(Printer printer) {
throw new UnsupportedOperationException("不支持该方法");
}
}



PrintUtil 工具类



package com.geektime;
public class PrintUtil {
public static void printControlName(String name) {
System.out.println("print " + name);
}
}



启动类



package com.geektime;
public class Main {
public static void main(String[] args) {
Printer winForm = new Container("Window窗口");
Printer logo = new BaseControl("logo图片");
Printer frame = new Container("frame1");
Printer loginButton = new BaseControl("登陆");
Printer registerButton = new BaseControl("注册");
Printer userLabel = new BaseControl("用户名");
Printer passwordLabel = new BaseControl("密码");
Printer textBox = new BaseControl("文本框");
Printer passwordBox = new BaseControl("密码框");
Printer checkBox = new BaseControl("复选框");
Printer rememberBox = new BaseControl("记住用户名");
Printer linkLabel = new BaseControl("忘记密码");
winForm.add(logo);
winForm.add(frame);
winForm.add(loginButton);
winForm.add(registerButton);
frame.add(userLabel);
frame.add(passwordLabel);
frame.add(textBox);
frame.add(passwordBox);
frame.add(checkBox);
frame.add(rememberBox);
frame.add(linkLabel);
winForm.print();
}
}



运行结果



字数不能少于50字字数不能少于50字字数不能少于50字字数不能少于50字字数不能少于50字字数不能少于50字

用户头像

习习

关注

还未添加个人签名 2018.08.08 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第 1 期-week3