周练习 3

用户头像
何毅曦
关注
发布于: 2020 年 10 月 04 日
  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

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



  1. component :

package com.test;
import java.util.List;
public abstract class Component {
public abstract void addChild(Component component);
public abstract void removeChild(Component component);
public abstract List<Component> getChildList();// only for this practice
// operation work
public void operation(){
List<Component> childs = this.getChildList();
if (null != childs && !childs.isEmpty()) {
for (Component child : childs) {
child.operation();
}
}
}
}
  1. window form :

package com.test;
import java.util.ArrayList;
import java.util.List;
public class WindowForm extends Component{
private List<Component> childList = new ArrayList<>();
@Override
public void addChild(Component component) {
this.childList.add(component);
}
@Override
public void removeChild(Component component) {
this.childList.remove(component);
}
@Override
public List<Component> getChildList(){
return this.childList;
}
@Override
public void operation() {
System.out.println("print WinForm(WINDOW窗口)");
super.operation();
}
}



  1. Picture:

package com.test;
import java.util.List;
public class Picture extends Component{
@Override
public void addChild(Component component) {
}
@Override
public void removeChild(Component component) {
}
@Override
public List<Component> getChildList(){
return null;
}
@Override
public void operation() {
System.out.println("print Picture(LOGO图片)");
}
}
  1. Button :

package com.test;
import java.util.List;
public class Button extends Component{
private String name;
@Override
public void addChild(Component component) {
}
@Override
public void removeChild(Component component) {
}
@Override
public List<Component> getChildList() {
return null;
}
@Override
public void operation() {
System.out.println(String.format("print Button(%s)", this.getName()));
}
public Button(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}



组件Lable,TextBox,CheckBox和LinkLable代码与Button类似,基本都是改了名字,这里不再赘述;



  1. Client :

package com.test;
public class Client {
public static void main(String[] args) {
WindowForm windowForm = new WindowForm();
// deep 1
Picture picture = new Picture();
Button loginButton = new Button("登录");
Button registerButton = new Button("注册");
Frame frame = new Frame("FRAME1");
windowForm.addChild(picture);
windowForm.addChild(loginButton);
windowForm.addChild(registerButton);
windowForm.addChild(frame);
// deep 2
Lable userNameLable = new Lable("用户名");
TextBox textBox = new TextBox("文本框");
Lable passwordLable = new Lable("密码");
PasswordBox passwordBox = new PasswordBox("密码框");
CheckBox checkBox = new CheckBox("复选框");
TextBox rememberUser = new TextBox("记住用户名");
LinkLable linkLable = new LinkLable("忘记密码");
frame.addChild(userNameLable);
frame.addChild(textBox);
frame.addChild(passwordLable);
frame.addChild(checkBox);
frame.addChild(rememberUser);
frame.addChild(linkLable);
// finish
windowForm.operation();
}
}



输出结果:



用户头像

何毅曦

关注

还未添加个人签名 2019.03.20 加入

还未添加个人简介

评论

发布
暂无评论
周练习3