架构师训练营第三周作业
1.单例代码:
2.组合模式打印程序:
import java.util.ArrayList;
import java.util.List;
abstract class Component {
protected String name;
protected List<Component> children;
public Component(String name) {
this.name = name;
children = new ArrayList<Component>();
}
public abstract void add(Component component);
public abstract void remove(Component component);
public abstract void display();
}
class Widget extends Component {
public Widget(String name) {
super(name);
}
@Override
public void add(Component component) {
children.add(component);
}
@Override
public void remove(Component component) {
children.remove(component);
}
@Override
public void display() {
System.out.println("print " + this.getClass().getName() + "(" + this.name + ")");
for (Component compont : children) {
compont.display();
}
}
}
class WinForm extends Widget {
public WinForm(String name) {
super(name);
}
}
class Picture extends WinForm {
public Picture(String name) {
super(name);
}
}
class Button extends WinForm {
public Button(String name) {
super(name);
}
}
class Frame extends WinForm {
public Frame(String name) {
super(name);
}
}
class Lable extends Frame {
public Lable(String name) {
super(name);
}
}
class TextBox extends Frame {
public TextBox(String name) {
super(name);
}
}
class PasswordBox extends Frame {
public PasswordBox(String name) {
super(name);
}
}
class CheckBox extends Frame {
public CheckBox(String name) {
super(name);
}
}
class LinkLable extends Frame {
public LinkLable(String name) {
super(name);
}
}
public class WidgetContainer {
public static void main(String[] args) {
WinForm form = new WinForm("WINDOWS窗口");
Picture pic = new Picture("LOGO图片");
Button logonButton = new Button("登录");
Button registButton = new Button("注册");
Frame frame = new Frame("FRAME1");
Lable userNameLable = new Lable("用户名");
TextBox textBox = new TextBox("文本框");
Lable passwordLable = new Lable("密码");
PasswordBox passwordBox = new PasswordBox("密码框");
CheckBox checkBox = new CheckBox("复选框");
Lable remeberUserNameLable = new Lable("记住用户名");
LinkLable forgetPassword = new LinkLable("忘记密码");
form.add(pic);
form.add(logonButton);
form.add(registButton);
form.add(frame);
frame.add(userNameLable);
frame.add(textBox);
frame.add(passwordLable);
frame.add(passwordBox);
frame.add(checkBox);
frame.add(remeberUserNameLable);
frame.add(forgetPassword);
form.display();
}
}
评论