写点什么

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

用户头像
竹森先生
关注
发布于: 2020 年 06 月 22 日

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

饿汉式单例模式的简单实现



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

abstract class Component {
public abstract void add(Component c);
public abstract void remove(Component c);
public abstract Component getChild(int i);
public abstract void printOpt();
}



class WindowForm extends Component {
private ArrayList<Component> componentList = new ArrayList<Component>();
private String name;
public WindowForm(String name) {
this.name = name;
}
public void add(Component c) {
componentList.add(c);
}
public void remove(Component c) {
componentList.remove(c);
}
public Component getChild(int i) {
return (Component)componentList.get(i);
}
public void printOpt() {
System.out.println("print Winform("+name+")");
for(Object obj : componentList) {
((Component)obj).printOpt();
}
}
}



class Frame extends Component {
private ArrayList<Component> componentList = new ArrayList<Component>();
private String name;
public Frame(String name) {
this.name = name;
}
public void add(Component c) {
componentList.add(c);
}
public void remove(Component c) {
componentList.remove(c);
}
public Component getChild(int i) {
return (Component)componentList.get(i);
}
public void printOpt() {
System.out.println("print Frame("+name+")");
for(Object obj : componentList) {
((Component)obj).printOpt();
}
}
}



class Picture extends Component {
private String name;
public Picture(String name) {
this.name = name;
}
public void add(Component c) {
System.out.println("不支持");
}
public void remove(Component c) {
System.out.println("不支持");
}
public Component getChild(int i) {
System.out.println("不支持");
}
public void printOpt() {
System.out.println("print Picture("+name+")");
}
}



class Button extends Component {
private String name;
public Button(String name) {
this.name = name;
}
public void add(Component c) {
System.out.println("不支持");
}
public void remove(Component c) {
System.out.println("不支持");
}
public Component getChild(int i) {
System.out.println("不支持");
}
public void printOpt() {
System.out.println("print Button("+name+")");
}
}



class Label extends Component {
private String name;
public Label(String name) {
this.name = name;
}
public void add(Component c) {
System.out.println("不支持");
}
public void remove(Component c) {
System.out.println("不支持");
}
public Component getChild(int i) {
System.out.println("不支持");
}
public void printOpt() {
System.out.println("print Label("+name+")");
}
}



class CheckBox extends Component {
private String name;
public CheckBox(String name) {
this.name = name;
}
public void add(Component c) {
System.out.println("不支持");
}
public void remove(Component c) {
System.out.println("不支持");
}
public Component getChild(int i) {
System.out.println("不支持");
}
public void printOpt() {
System.out.println("print CheckBox("+name+")");
}
}



class TextBox extends Component {
private String name;
public TextBox(String name) {
this.name = name;
}
public void add(Component c) {
System.out.println("不支持");
}
public void remove(Component c) {
System.out.println("不支持");
}
public Component getChild(int i) {
System.out.println("不支持");
}
public void printOpt() {
System.out.println("print TextBox("+name+")");
}
}



class LinkLabel extends Component {
private String name;
public LinkLabel(String name) {
this.name = name;
}
public void add(Component c) {
System.out.println("不支持");
}
public void remove(Component c) {
System.out.println("不支持");
}
public Component getChild(int i) {
System.out.println("不支持");
}
public void printOpt() {
System.out.println("print LinkLabel("+name+")");
}
}



class PassWordBox extends Component {
private String name;
public PassWordBox(String name) {
this.name = name;
}
public void add(Component c) {
System.out.println("不支持");
}
public void remove(Component c) {
System.out.println("不支持");
}
public Component getChild(int i) {
System.out.println("不支持");
}
public void printOpt() {
System.out.println("print PassWordBox("+name+")");
}
}



class Client {
public static void main(String [] args) {
Component comp1,comp2,comp3,comp4,comp5,comp6,comp7,comp8,comp9,comp10,comp11,comp12;
comp1 = new WindowForm("WINDOW窗口")
comp2 = new Frame("FRAME1");
comp3 = new Picture("LOGO图片");
comp4 = new Button("登录");
comp5 = new Button("注册");
comp6 = new Label("用户名");
comp7 = new TextBox("文本框");
comp8 = new Label("密码");
comp9 = new PassWordBox("密码框");
comp10 = new CheckBox("复选框");
comp11 = new TextBox("记住用户名");
comp12 = new LinkLabel("忘记密码");
comp2.add(comp6);
comp2.add(comp7);
comp2.add(comp8);
comp2.add(comp9);
comp2.add(comp10);
comp2.add(comp11);
comp2.add(comp12);
comp1.add(comp3);
comp1.add(comp4);
comp1.add(comp5);
comp1.add(comp2);
comp1.printOpt();
}
}



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

竹森先生

关注

还未添加个人签名 2020.03.26 加入

还未添加个人简介

评论

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