写点什么

Week3 命题作业

用户头像
星河寒水
关注
发布于: 2020 年 06 月 19 日
Week3 命题作业

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

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

解答:

组件关系参考下图

CompositePatternDemo.java

package composite;
public class CompositePatternDemo {
public static void main(String[] args) {
Component winForm = new WinForm("WINDOW窗口");
Component picture = new Picture("LOGO图片");
Component btnLogin = new Button("登录");
Component btnRegister = new Button("注册");
Component frame1 = new Frame("FRAME1");
Component labelUsername = new Label("用户名");
Component textBoxUsername = new TextBox("文本框");
Component labelPassword = new Label("密码");
Component pwdBox = new PasswordBox("密码框");
Component checkBoxRemember = new CheckBox("复选框");
Component textBoxRemember = new TextBox("记住用户名");
Component linkLabelForget = new LinkLabel("忘记密码");
winForm.add(picture);
winForm.add(btnLogin);
winForm.add(btnRegister);
winForm.add(frame1);
frame1.add(labelUsername);
frame1.add(textBoxUsername);
frame1.add(labelPassword);
frame1.add(pwdBox);
frame1.add(checkBoxRemember);
frame1.add(textBoxRemember);
frame1.add(linkLabelForget);
winForm.print();
}
}

Component.java

package composite;
import java.util.List;
public interface Component {
public void print();
public void add(Component e);
public void remove(Component e);
public List<Component> getSubComponents();
}

Container.java

package composite;
import java.util.ArrayList;
import java.util.List;
public class Container implements Component {
private String name;
private List<Component> subComponents;
public Container(String name) {
this.name = name;
this.subComponents = new ArrayList<Component>();
}
public void add(Component e) {
this.subComponents.add(e);
}
public void remove(Component e) {
this.subComponents.remove(e);
}
public List<Component> getSubComponents() {
return this.subComponents;
}
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
if (null != this.subComponents) {
for (Component subComponent : this.subComponents) {
subComponent.print();
}
}
}
}

WinForm.java

package composite;
public class WinForm extends Container {
public WinForm(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}

Frame.java

package composite;
public class Frame extends WinForm {
public Frame(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}

Picture.java

package composite;
import java.util.List;
public class Picture implements Component {
private String name;
public Picture(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
@Override
public void add(Component e) {
// TODO Auto-generated method stub
}
@Override
public void remove(Component e) {
// TODO Auto-generated method stub
}
@Override
public List<Component> getSubComponents() {
// TODO Auto-generated method stub
return null;
}
}

Button.java

package composite;
import java.util.List;
public class Button implements Component {
private String name;
public Button(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
@Override
public void add(Component e) {
// TODO Auto-generated method stub
}
@Override
public void remove(Component e) {
// TODO Auto-generated method stub
}
@Override
public List<Component> getSubComponents() {
// TODO Auto-generated method stub
return null;
}
}

Label.java

package composite;
import java.util.List;
public class Label implements Component {
private String name;
public Label(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
@Override
public void add(Component e) {
// TODO Auto-generated method stub
}
@Override
public void remove(Component e) {
// TODO Auto-generated method stub
}
@Override
public List<Component> getSubComponents() {
// TODO Auto-generated method stub
return null;
}
}

TextBox.java

package composite;
import java.util.List;
public class TextBox implements Component {
private String name;
public TextBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
@Override
public void add(Component e) {
// TODO Auto-generated method stub
}
@Override
public void remove(Component e) {
// TODO Auto-generated method stub
}
@Override
public List<Component> getSubComponents() {
// TODO Auto-generated method stub
return null;
}
}

PasswordBox.java

package composite;
import java.util.List;
public class PasswordBox implements Component {
private String name;
public PasswordBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
@Override
public void add(Component e) {
// TODO Auto-generated method stub
}
@Override
public void remove(Component e) {
// TODO Auto-generated method stub
}
@Override
public List<Component> getSubComponents() {
// TODO Auto-generated method stub
return null;
}
}

CheckBox.java

package composite;
import java.util.List;
public class CheckBox implements Component {
private String name;
public CheckBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
@Override
public void add(Component e) {
// TODO Auto-generated method stub
}
@Override
public void remove(Component e) {
// TODO Auto-generated method stub
}
@Override
public List<Component> getSubComponents() {
// TODO Auto-generated method stub
return null;
}
}

LinkLabel.java

package composite;
import java.util.List;
public class LinkLabel implements Component {
private String name;
public LinkLabel(String name) {
this.name = name;
}
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
@Override
public void add(Component e) {
// TODO Auto-generated method stub
}
@Override
public void remove(Component e) {
// TODO Auto-generated method stub
}
@Override
public List<Component> getSubComponents() {
// TODO Auto-generated method stub
return null;
}
}

使用VSCode终端运行结果如下:



发布于: 2020 年 06 月 19 日阅读数: 184
用户头像

星河寒水

关注

还未添加个人签名 2018.09.17 加入

还未添加个人简介

评论

发布
暂无评论
Week3 命题作业