架构师 3 期 3 班 -week3- 作业
发布于: 2020 年 12 月 06 日
作业
题目 1:请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
题目 2:请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出
作业完成
作业 1
分析:手写代码可以多写注释,体现思路,我觉得这可能比代码的正确性重要,只要关键的位置注释明确应该就可以了。
作业 2
类图
说明:顶级设计成一个抽象类,这里抽象出来的 Container 和 NonContainer 是为了向子类屏蔽 isContainer、NonContainer 的子类的 nodes 与 add 方法;
LinkLable 与 Lable、TextBox 与 PasswordBox 设计成同级是考虑继承层次不应该很深;
代码实现
类的设计实现
import java.util.ArrayList;import java.util.List;public abstract class Control { private int level;//层级 private String name;//组件实例的名称 private boolean isContainer;//true:容器;false:非容器 private List<Control> nodes = new ArrayList<>();//子结点 public void print() { System.out.println(getNodePrintStr()); } public abstract void add(Control control); public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isContainer() { return isContainer; } public void setContainer(boolean container) { isContainer = container; } public List<Control> getNodes() { return nodes; } public void setNodes(List<Control> nodes) { this.nodes = nodes; } public String getNodePrintStr(){ StringBuffer buffer = new StringBuffer(); String tab = " "; for (int i = 0; i < this.getLevel(); i++) { buffer.append(tab); } buffer.append("-").append(this.getClass().getSimpleName()) .append("(").append(this.name).append(")"); return buffer.toString(); }}public class Container extends Control { private int weight; private int height; public Container(String name,int level){ this.setContainer(true); super.setName(name); this.setLevel(level); } public void print(){ System.out.println(getNodePrintStr()); for (Control c : this.getNodes()){ c.print(); } } public void add(Control control) { control.setLevel(this.getLevel()+1); this.getNodes().add(control); } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; }}public class WinForm extends Container { public WinForm(String name) { super(name,0); }}public class Frame extends Container{ public Frame(String name, int p_level) { super(name,p_level+1); }}public class NonContainer extends Control { public NonContainer(String name){ super.setName(name); super.setContainer(false); super.setNodes(null); } //加final 阻止子类重写 public final void add(Control control) {}}public class Button extends NonContainer { public Button(String name) { super(name); }}public class CheckBox extends NonContainer { public CheckBox(String name) { super(name); }}public class Lable extends NonContainer { public Lable(String name) { super(name); }}public class LinkLable extends NonContainer { public LinkLable(String name) { super(name); }}public class PasswordBox extends NonContainer{ public PasswordBox(String name) { super(name); }}public class Picture extends NonContainer{ public Picture(String name) { super(name); }}public class TextBox extends NonContainer { public TextBox(String name) { super(name); }}复制代码
main 函数:
public class Main { public static void main(String[] args) { WinForm winForm = buildWinForm(); winForm.print(); } public static WinForm buildWinForm(){ WinForm winForm = new WinForm("windows 窗口"); Picture picture = new Picture("logo 图片"); winForm.add(picture); Frame frame = buildFrame(winForm.getLevel() ); winForm.add(frame); Button l_button = new Button("登录"); winForm.add(l_button); Button r_button = new Button("注册"); winForm.add(r_button); return winForm; } public static Frame buildFrame(int level){ Frame frame = new Frame("frame 窗口",level); Lable u_lable = new Lable("用户名"); frame.add(u_lable); TextBox textBox = new TextBox("文本框"); frame.add(textBox); Lable p_lable = new Lable("密码"); frame.add(p_lable); PasswordBox passwordBox = new PasswordBox("密码框"); frame.add(passwordBox); CheckBox checkBox = new CheckBox("复选框"); frame.add(checkBox); Lable r_lable = new Lable("记住用户名"); frame.add(r_lable); LinkLable linkLable = new LinkLable("忘记密码"); frame.add(linkLable); return frame; }}复制代码
运行结果:
-WinForm(windows 窗口)-Picture(logo 图片)-Frame(frame 窗口)-Lable(用户名)-TextBox(文本框)-Lable(密码)-PasswordBox(密码框)-CheckBox(复选框)-Lable(记住用户名)-LinkLable(忘记密码)-Button(登录)-Button(注册)
复制代码
划线
评论
复制
发布于: 2020 年 12 月 06 日阅读数: 31
版权声明: 本文为 InfoQ 作者【zbest】的原创文章。
原文链接:【http://xie.infoq.cn/article/e8f19e0283168e611ece60945】。未经作者许可,禁止转载。
zbest
关注
一个胖子 2020.11.04 加入
一个不正经的java程序员, 整天写着openresty和go的代码, 努力从键摄向非职业摄影师迈进, 快要溺死在内耗里的中年人, 胖子。











评论