写点什么

架构师训练营第三周课后作业

用户头像
万有引力
关注
发布于: 2020 年 12 月 11 日

作业1.在草稿纸上首页单例模式实现代码



作业2. 使用组合模式输出窗体

import java.util.List;
import java.util.ArrayList;
interface Widget {
void print();
}
interface WidgetContainer extends Widget {
void addWidget(Widget widget);
}
abstract class Component {
private String label;
protected void setLabel(String label) {
this.label = label;
}
protected String getLabel() {
return label;
}
}
class WinForm extends Component implements WidgetContainer {
private List<Widget> widgets = new ArrayList();
public WinForm(String label) {
setLabel(label);
}
public void print() {
System.out.println("print WinForm(" + getLabel() +")");
for(Widget w : widgets) {
w.print();
}
}
public void addWidget(Widget widget) {
widgets.add(widget);
}
}
class Frame extends Component implements WidgetContainer {
private List<Widget> widgets = new ArrayList();
public Frame(String label) {
setLabel(label);
}
public void print() {
System.out.println("print Frame(" + getLabel() +")");
for(Widget w : widgets) {
w.print();
}
}
public void addWidget(Widget widget) {
widgets.add(widget);
}
}
class Picture extends Component implements Widget {
public Picture(String label) {
setLabel(label);
}
public void print() {
System.out.println("print Picture(" + getLabel() +")");
}
}
class Button extends Component implements Widget {
public Button(String label) {
setLabel(label);
}
public void print() {
System.out.println("print Button(" + getLabel() +")");
}
}
class Label extends Component implements Widget {
public Label(String label) {
setLabel(label);
}
public void print() {
System.out.println("print Label(" + getLabel() +")");
}
}
class TextBox extends Component implements Widget {
public TextBox(String label) {
setLabel(label);
}
public void print() {
System.out.println("print TextBox(" + getLabel() +")");
}
}
class PasswordBox extends Component implements Widget {
public PasswordBox(String label) {
setLabel(label);
}
public void print() {
System.out.println("print PasswordBox(" + getLabel() +")");
}
}
class CheckBox extends Component implements Widget {
public CheckBox(String label) {
setLabel(label);
}
public void print() {
System.out.println("print CheckBox(" + getLabel() +")");
}
}
class LinkLabel extends Component implements Widget {
public LinkLabel(String label) {
setLabel(label);
}
public void print() {
System.out.println("print LinkLabel(" + getLabel() +")");
}
}
class Main {
public static void main(String[] args) {
WidgetContainer winForm = new WinForm("WINDOW窗口");
Widget picLogo = new Picture("LOGO图片");
Widget btnLogin = new Button("登录");
Widget btnRegister = new Button("注册");
WidgetContainer frame = new Frame("FRAME1");
Widget labelUserName = new Label("用户名");
Widget textUserName = new TextBox("文本框");
Widget pdLabel = new Label("密码");
Widget pdPassword = new PasswordBox("密码框");
Widget cbCheckBox = new CheckBox("复选框");
Widget textRememberMe = new TextBox("记住用户名");
Widget llForgotPw = new LinkLabel("忘记密码");
frame.addWidget(labelUserName);
frame.addWidget(textUserName);
frame.addWidget(pdLabel);
frame.addWidget(pdPassword);
frame.addWidget(cbCheckBox);
frame.addWidget(textRememberMe);
frame.addWidget(llForgotPw);
winForm.addWidget(picLogo);
winForm.addWidget(btnLogin);
winForm.addWidget(btnRegister);
winForm.addWidget(frame);
winForm.print();
}
}



代码运行链接:

https://repl.it/@attrany/ElaborateScaryLanservers



执行结果:

print WinForm(WINDOW窗口)

print Picture(LOGO图片)

print Button(登录)

print Button(注册)

print Frame(FRAME1)

print Label(用户名)

print TextBox(文本框)

print Label(密码)

print PasswordBox(密码框)

print CheckBox(复选框)

print TextBox(记住用户名)

print LinkLabel(忘记密码)



发布于: 2020 年 12 月 11 日阅读数: 25
用户头像

万有引力

关注

还未添加个人签名 2018.05.30 加入

还未添加个人简介

评论

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