架构师第三周作业

用户头像
傻傻的帅
关注
发布于: 2020 年 06 月 24 日

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





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



package com.dhc;



public abstract class ObjNode {

protected String nodeName;

public ObjNode(String nodeName){

this.nodeName = nodeName;

}

public abstract void draw();

public String getNodeName(){

return nodeName;

}

}



package com.dhc;



public class Component extends ObjNode {

public Component(String nodeName){

super(nodeName);

}

@Override

public void draw() {

System.out.println("print "+getNodeName());

}

}



package com.dhc;

import java.util.ArrayList;

import java.util.List;



public class Container extends ObjNode {

private List<ObjNode> treeNodes = new ArrayList<>();

public Container(String nodeName){

super(nodeName);

}

public void addSubNode(ObjNode node){

treeNodes.add(node);

}

@Override

public void draw() {

System.out.println("print "+getNodeName());

for(ObjNode node:treeNodes){

node.draw();

}

}

}



package com.dhc;



public class TreeDemo {

public static void main(String[] args) {

Container form = new Container("WinForm(WINDOW窗口)");

Component picture = new Component("Picture(LOGO图片)");

Component button1 = new Component("Button(登陆)");

Component button2 = new Component("Button(注册)");

form.addSubNode(picture);

form.addSubNode(button1);

form.addSubNode(button2);



Container frame = new Container("Frame(FRAME1)");

Component lable1 = new Component("Lable(用户名)");

Component textbox1 = new Component("TextBox(文本框)");

Component lable2 = new Component("Lable(密码)");

Component pwdbox = new Component("PasswordBox(密码框)");

Component chkbox = new Component("CheckBox(复选框)");

Component tbox = new Component("TextBox(记住用户名)");

Component lklable = new Component("LinkLable(忘记密码)");

frame.addSubNode(lable1);

frame.addSubNode(textbox1);

frame.addSubNode(lable2);

frame.addSubNode(pwdbox);

frame.addSubNode(chkbox);

frame.addSubNode(tbox);

frame.addSubNode(lklable);



form.addSubNode(frame);



form.draw();

}

}



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

傻傻的帅

关注

走自已的路,让别人无路可走 2019.09.18 加入

还未添加个人简介

评论

发布
暂无评论
架构师第三周作业