写点什么

【架构师训练营】第三期作业

用户头像
云064
关注
发布于: 2020 年 06 月 24 日

1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。



2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。



abstract class Component {
private List<Component> list;
protected Sring name;
public Component() {
this.list = new ArrayList<Component>();
}
public void addComponent(Component c) {
this.list.add(c);
}
private void render() {
this.renderImpl();
for (Component c : this.list) {
c.render();
}
}
public void setName(String name) {
this.name = name;
}
protected void renderImpl();
}
class WindowForm extends Component {
protected void renderImpl() {
print("Print WinForm(" + this.name + ")");
}
}
class Picture extends Component {
protected void renderImpl() {
print("Print Picture(" + this.name + ")");
}
}
class Button extends Component {
protected void renderImpl() {
print("Print Picture(" + this.name + ")");
}
}
class Checkbox extends Button {
protected void renderImpl() {
print("Print Checkbox(" this.name + ")");
}
}
class Frame extends Component {
protected void renderImpl() {
print("Print Frame(" + this.name + ")");
}
}
class Label extends Component {
protected void renderImpl() {
print("Print Label(" + this.name + ")");
}
}
class LinkLabel extends Label {
protected void renderImpl() {
print("Print LinkLabel" + this.name + ")")
}
}
class TextBox extends Component {
protected void renderImpl() {
print("Print TextBox(" + this.name + ")");
}
}
class PasswordBox extends TextBox {
protected void renderImpl() {
print("Print PasswordBox(" + this.name + ")");
}
}
public class Main() {
public static void main() {
Component c = new WindowForm();
c.setName("WINDOW窗口");
Component pic = new Picture();
pic.setName("LOGO图片");
c.addComponent(pic);
Component btnLogin = new Button();
btnLogin.setName("登录");
c.addComponent(btnLogin);
Component btnRgst = new Button();
btnRgst.setName("注册");
c.addComponent(btnRgst);
c = addFrame(c);
c.render();
}
public static Component addFrame(Component container) {
Component frame = new Frame();
frame.setName("Frame1");
Component labelUser = new Label();
labelUser.setName("用户名");
frame.addComponent(labelUser);
Component text = new TextBox();
text.setName("文本框");
frame.addComponent(text);
Component labelPwd = new Label();
labelPwd.setName("密码");
frame.addComponent(labelPwd);
Component pwdBox = new PasswordBox();
pwdBox.setName("密码框");
frame.addComponent(pwdBox);
Component check = new Checkbox();
check.setName("复选框");
frame.addComponent(check);
Component remember = new TextBox();
remember.setName("记住用户名");
frame.addComponent(remember);
Component forget = new LinkLabel();
forget.setName("忘记密码");
frame.addComponent(forget);
container.addComponent(frame);
return container;
}
}



用户头像

云064

关注

还未添加个人签名 2018.05.24 加入

还未添加个人简介

评论

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