第 3 周作业提交
作业一:
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
采用饿汉式创建单例
请用组合设计模式编写程序
/**
* 定义的组件抽象类
*/
public abstract class SubComponent {
private String SubComponentName;
public SubComponent(String name){
SubComponentName = name;
}
private List<SubComponent> list;
public void add(SubComponent c){
if( ObjectUtils.isEmpty(list) ) {
list = new ArrayList<>();
}
list.add(c);
}
public abstract void print();
}
/**
* 用于实例化的组件实现类
*/
public class WinForm extends SubComponent {
public WinForm(String name) {
super (name);
}
@Override
public abstract void print() {
System.out.println(subComponentName);
}
}
public class CheckBox extends SubComponent {
public WinForm(String name) {
super (name);
}
@Override
public abstract void print() {
System.out.println(subComponentName);
}
}
/**
* 测试类
*/
public class TestCombination {
public static void main(String[] args) {
WinForm winForm = new WinForm("WINDOW窗口");
Picture picture = new Picture("LOGO图片");
Button login = new Button("登录");
Button register = new Button("注册");
Frame frame = new Frame("FRAME1");
Lable label1 = new Lable("用户名");
TextBox textBox1 = new TextBox("文本框");
Lable label2 = new Lable("密码");
PasswordBox passwordBox = new PasswordBox("密码框");
CheckBox checkbox = new CheckBox("复选框");
TextBox textBox2 = new TextBox("记住用户名");
LinkLable linkLabel = new LinkLable("忘记密码");
frame.add(label1);
frame.add(textBox1);
frame.add(label2);
frame.add(passwordBox);
frame.add(checkbox);
frame.add(textBox2);
frame.add(linkLabel);
winForm.add(picture);
winForm.add(login);
winForm.add(register);
winForm.add(frame);
winForm.print();
}
}
评论