架构师训练营 - 第三周作业

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

手写单例模式



组合模式程序

程序运行结果



程序代码如下

package coffee.week3;
public class WinFormTest {
public static void main(String[] args) {
WinForm winForm = new WinForm("WINDOW窗口");
winForm.addComponent(new Button("登录"));
winForm.addComponent(new Button("注册"));
Frame frame = new Frame("Frame1");
frame.addComponent(new Lable("用户名"));
frame.addComponent(new TextBox("文本框"));
frame.addComponent(new Lable("密码"));
frame.addComponent(new PasswordBox("密码框"));
frame.addComponent(new CheckBox("复选框"));
frame.addComponent(new TextBox("记住用户名"));
frame.addComponent(new LinkLable("忘记密码"));
winForm.addComponent(frame);
winForm.draw();
}
}



package coffee.week3;
import java.util.ArrayList;
import java.util.List;
abstract class Component {
private List<Component> componentList = new ArrayList<>();
private String name;
public Component(String name) {
this.name = name;
}
public void addComponent(Component component) {
componentList.add(component);
}
public void draw() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + this.name + ")");
for (Component component : componentList) {
component.draw();
}
}
}



package coffee.week3;
public class WinForm extends Component {
public WinForm(String name) {
super(name);
}
}



package coffee.week3;
public class Button extends Component {
public Button(String name) {
super(name);
}
}



package coffee.week3;
public class Frame extends Component {
public Frame(String name) {
super(name);
}
}



package coffee.week3;
public class Lable extends Component {
public Lable(String name) {
super(name);
}
}



package coffee.week3;
public class TextBox extends Component {
public TextBox(String name) {
super(name);
}
}



package coffee.week3;
public class PasswordBox extends Component {
public PasswordBox(String name) {
super(name);
}
}



package coffee.week3;
public class CheckBox extends Component {
public CheckBox(String name) {
super(name);
}
}



package coffee.week3;
public class LinkLable extends Component {
public LinkLable(String name) {
super(name);
}
}



字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~字数补丁~

用户头像

咖啡

关注

还未添加个人签名 2018.01.30 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营 - 第三周作业