Week 03 命题作业

用户头像
卧石漾溪
关注
发布于: 2020 年 06 月 23 日

1.请在草稿纸上手写一个单例模式的实现代码。

(我写字真是。。。太难看了。。。请忽略这个细节吧,阿门)





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

打印输出示例参考图3。







解答:

以下我分别用透明式的组合模式和安全式的组合模式实现的代码:

1.透明式的组合模式:




import java.util.ArrayList;
import java.util.List;

/*
* 透明式的合成模式
*/


//抽象构件
interface IComponent {
void add(IComponent e);
void remove(IComponent e);
List<IComponent> getSubComponentList();
void print();
}

//具体构件(WinForm)(树枝构件)
class WinForm implements IComponent {
private String name;
private List<IComponent> subComponentList;

public WinForm(String name) {
this.name = name;
subComponentList = new ArrayList<IComponent>();
}
@Override
public void add(IComponent e) {
subComponentList.add(e);
}
@Override
public void remove(IComponent e) {
subComponentList.remove(e);
}
@Override
public List<IComponent> getSubComponentList(){
return subComponentList;
}

@Override
public void print() {
System.out.println("print WinForm("+name+")");
for(IComponent subComponent : subComponentList){
subComponent.print();
}
}

}

//具体构件(Frame)(树枝构件)
class Frame implements IComponent {
private String name;
private List<IComponent> subComponentList;

public Frame(String name) {
this.name = name;
subComponentList = new ArrayList<IComponent>();
}
@Override
public void add(IComponent e) {
subComponentList.add(e);
}
@Override
public void remove(IComponent e) {
subComponentList.remove(e);
}
@Override
public List<IComponent> getSubComponentList(){
return subComponentList;
}

@Override
public void print() {
System.out.println("print Frame("+name+")");
for(IComponent subComponent : subComponentList){
subComponent.print();
}
}

}

//具体构件(Picture)(叶子构件)
class Picture implements IComponent {
private String name;
public Picture(String name) {
this.name = name;
}
@Override
public void add(IComponent e) {}
@Override
public void remove(IComponent e) {}
@Override
public List<IComponent> getSubComponentList(){return null;}

@Override
public void print() {
System.out.println("print Picture("+name+")");
}

}

//具体构件(Button)(叶子构件)
class Button implements IComponent {
private String name;
public Button(String name) {
this.name = name;
}
@Override
public void add(IComponent e) {}
@Override
public void remove(IComponent e) {}
@Override
public List<IComponent> getSubComponentList(){return null;}

@Override
public void print() {
System.out.println("print Button("+name+")");
}

}

//具体构件(Label)(叶子构件)
class Label implements IComponent {
private String name;
public Label(String name) {
this.name = name;
}
@Override
public void add(IComponent e) {}
@Override
public void remove(IComponent e) {}
@Override
public List<IComponent> getSubComponentList(){return null;}

@Override
public void print() {
System.out.println("print Label("+name+")");
}

}

//具体构件(TextBox)(叶子构件)
class TextBox implements IComponent {
private String name;
public TextBox(String name) {
this.name = name;
}
@Override
public void add(IComponent e) {}
@Override
public void remove(IComponent e) {}
@Override
public List<IComponent> getSubComponentList(){return null;}

@Override
public void print() {
System.out.println("print TextBox("+name+")");
}

}

//具体构件(PasswordBox)(叶子构件)
class PasswordBox implements IComponent {
private String name;
public PasswordBox(String name) {
this.name = name;
}
@Override
public void add(IComponent e) {}
@Override
public void remove(IComponent e) {}
@Override
public List<IComponent> getSubComponentList(){return null;}

@Override
public void print() {
System.out.println("print PasswordBox("+name+")");
}

}

//具体构件(CheckBox)(叶子构件)
class CheckBox implements IComponent {
private String name;
public CheckBox(String name) {
this.name = name;
}
@Override
public void add(IComponent e) {}
@Override
public void remove(IComponent e) {}
@Override
public List<IComponent> getSubComponentList(){return null;}

@Override
public void print() {
System.out.println("print CheckBox("+name+")");
}

}

//具体构件(LinkLabel)(叶子构件)
class LinkLabel implements IComponent {
private String name;
public LinkLabel(String name) {
this.name = name;
}
@Override
public void add(IComponent e) {}
@Override
public void remove(IComponent e) {}
@Override
public List<IComponent> getSubComponentList(){return null;}

@Override
public void print() {
System.out.println("print LinkLabel("+name+")");
}

}


public class CompositePatternDemo {
public static void main(String[] args) {
IComponent windowForm = new WinForm("Windows窗口");
IComponent picture = new Picture("LOGO图片");
IComponent buttonLogin = new Button("登录");
IComponent buttonRegist = new Button("注册");
IComponent frame = new Frame("FRAME1");
IComponent labelUserName = new Label("用户名");
IComponent textUserName = new TextBox("文本框");
IComponent labelPassword = new Label("密码");
IComponent textPassword = new PasswordBox("密码框");
IComponent checkBoxRem = new CheckBox("复选框");
IComponent labelRem = new Label("记住用户名");
IComponent linkLabelFgtPass = new LinkLabel("忘记密码");
windowForm.add(picture);
windowForm.add(buttonLogin);
windowForm.add(buttonRegist);
windowForm.add(frame);
frame.add(labelUserName);
frame.add(textUserName);
frame.add(labelPassword);
frame.add(textPassword);
frame.add(checkBoxRem);
frame.add(labelRem);
frame.add(linkLabelFgtPass);
windowForm.print();
}
}



2.安全式的组合模式代码:




import java.util.ArrayList;
import java.util.List;

/*
* 安全式的合成模式
*/


//抽象构件
interface IComponent {
void print();
}

//抽象树枝构件
abstract class Composite implements IComponent{
private List<IComponent> subComponentList = new ArrayList<IComponent>();
public void add(IComponent e) {
subComponentList.add(e);
}

public void remove(IComponent e) {
subComponentList.remove(e);
}
public List<IComponent> getSubComponentList(){
return subComponentList;
}
}

//具体构件(WinForm)(树枝构件)
class WinForm extends Composite {
private String name;
public WinForm(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print WinForm("+name+")");
List<IComponent> subComponentList = getSubComponentList();
for(IComponent subComponent : subComponentList){
subComponent.print();
}
}

}

//具体构件(Frame)(树枝构件)
class Frame extends Composite {
private String name;
public Frame(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print Frame("+name+")");
List<IComponent> subComponentList = getSubComponentList();
for(IComponent subComponent : subComponentList){
subComponent.print();
}
}

}

//具体构件(Picture)(叶子构件)
class Picture implements IComponent {
private String name;
public Picture(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print Picture("+name+")");
}

}

//具体构件(Button)(叶子构件)
class Button implements IComponent {
private String name;
public Button(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print Button("+name+")");
}

}

//具体构件(Label)(叶子构件)
class Label implements IComponent {
private String name;
public Label(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print Label("+name+")");
}

}

//具体构件(TextBox)(叶子构件)
class TextBox implements IComponent {
private String name;
public TextBox(String name) {
this.name = name;
}

@Override
public void print() {
System.out.println("print TextBox("+name+")");
}

}

//具体构件(PasswordBox)(叶子构件)
class PasswordBox implements IComponent {
private String name;
public PasswordBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print PasswordBox("+name+")");
}

}

//具体构件(CheckBox)(叶子构件)
class CheckBox implements IComponent {
private String name;
public CheckBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print CheckBox("+name+")");
}

}

//具体构件(LinkLabel)(叶子构件)
class LinkLabel implements IComponent {
private String name;
public LinkLabel(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print LinkLabel("+name+")");
}

}


public class CompositePatternDemo {
public static void main(String[] args) {
Composite windowForm = new WinForm("Windows窗口");
IComponent picture = new Picture("LOGO图片");
IComponent buttonLogin = new Button("登录");
IComponent buttonRegist = new Button("注册");
Composite frame = new Frame("FRAME1");
IComponent labelUserName = new Label("用户名");
IComponent textUserName = new TextBox("文本框");
IComponent labelPassword = new Label("密码");
IComponent textPassword = new PasswordBox("密码框");
IComponent checkBoxRem = new CheckBox("复选框");
IComponent labelRem = new Label("记住用户名");
IComponent linkLabelFgtPass = new LinkLabel("忘记密码");
windowForm.add(picture);
windowForm.add(buttonLogin);
windowForm.add(buttonRegist);
windowForm.add(frame);
frame.add(labelUserName);
frame.add(textUserName);
frame.add(labelPassword);
frame.add(textPassword);
frame.add(checkBoxRem);
frame.add(labelRem);
frame.add(linkLabelFgtPass);
windowForm.print();
}
}




用户头像

卧石漾溪

关注

还未添加个人签名 2020.05.04 加入

还未添加个人简介

评论

发布
暂无评论
Week 03 命题作业