写点什么

第 3 周作业

用户头像
娄江国
关注
发布于: 2020 年 06 月 24 日

一、单例模式



二、组合模式(装饰器)



一共3个类,WinForm、WinFormDecorate(用于装饰WinForm)、Client(用于通过WinFormDecorate动态组装各种WinForm)

1、WinForm

public class WinForm {
private String type;
private String showName;
public WinForm(String type, String showName) {
this.type = type;
this.showName = showName;
}
public void print() {
System.out.println(String.format("print %s(%s)", this.type, this.showName));
}
public static void main(String[] args) {
WinForm winForm = new WinForm("WinForm", "WINDOW窗口");
winForm.print();
}
}



2、WinFromDecorate



import java.util.ArrayList;
import java.util.List;
public class WinFromDecorate extends WinForm {
private List<WinForm> winFormList = new ArrayList<>();
public WinFromDecorate(String type, String showName) {
super(type, showName);
}
public void decorate(WinForm winForm) {
winFormList.add(winForm);
}
@Override
public void print() {
super.print();
for (WinForm winForm: winFormList) {
winForm.print();
}
}
}



3、Client



public class Client {
public static void main(String[] args) {
WinFromDecorate winForm = new WinFromDecorate("WinForm", "WINDOW窗口");
WinForm picture = new WinForm("Picture", "LOGO图片");
WinForm buttonLogin = new WinForm("Button", "登录");
WinForm buttonRegister = new WinForm("Button", "注册");
winForm.decorate(picture);
winForm.decorate(buttonLogin);
winForm.decorate(buttonRegister);
WinFromDecorate frame = new WinFromDecorate("Frame", "FRAME1");
WinForm lableUser = new WinForm("Lable", "用户名");
WinForm textBoxUser = new WinForm("TextBox", "文本框");
WinForm lablePwd = new WinForm("Lable", "密码");
WinForm passwordBox = new WinForm("PasswordBox", "密码框");
WinForm checkBox = new WinForm("CheckBox", "复选框");
WinForm labelRember = new WinForm("Lable", "记住用户名");
WinForm linkLable = new WinForm("LinkLable", "忘记密码");
frame.decorate(lableUser);
frame.decorate(textBoxUser);
frame.decorate(lablePwd);
frame.decorate(passwordBox);
frame.decorate(checkBox);
frame.decorate(labelRember);
frame.decorate(linkLable);
winForm.decorate(frame);
winForm.print();
}
}



用户头像

娄江国

关注

还未添加个人签名 2017.11.10 加入

还未添加个人简介

评论

发布
暂无评论
第3周作业