第三周作业
发布于: 2020 年 06 月 24 日
作业一:
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
package com.example.CompositePattern;/** * @author */public abstract class Component { private String nodeName; public Component(String nodeName) { this.nodeName = nodeName; } /** * */ public String printfInfo(){ return this.nodeName; }}package com.example.CompositePattern;import java.util.ArrayList;import java.util.List;public class Composite extends Component { public Composite(String nodeName) { super(nodeName); } private List<Component> subNode = new ArrayList<>(); public void addNode(Component component){ this.subNode.add(component); } public List<Component> getNode(){ return this.subNode; }}package com.example.CompositePattern;public class Leaf extends Component { public Leaf(String nodeName) { super(nodeName); }}package com.example.CompositePattern;public class Test { public static void main(String[] args) { System.out.println(getAllNode(orgnazidData())); } /** * 场景 组织数据 * @return */ public static Composite orgnazidData(){ Composite winForm = new Composite("WinForm(Window窗口)"); Leaf picture = new Leaf("Picture(Logo图片)"); Leaf buttonLogin = new Leaf("button登录"); Leaf buttonRegister = new Leaf("button注册"); Composite frame = new Composite("Frame1"); Leaf lable = new Leaf("Lable用户名"); Leaf textBoxName = new Leaf("文本框"); Leaf lablePassword = new Leaf("密码"); Leaf passwordBox = new Leaf("PasswordBox密码框"); Leaf checkBox = new Leaf("CheckBox复选框"); Leaf textBox = new Leaf("TextBox记住用户名"); Leaf linkLable = new Leaf("LinkLable忘记密码"); winForm.addNode(picture); winForm.addNode(buttonLogin); winForm.addNode(buttonRegister); winForm.addNode(frame); frame.addNode(lable); frame.addNode(textBoxName); frame.addNode(lablePassword); frame.addNode(passwordBox); frame.addNode(checkBox); frame.addNode(textBox); frame.addNode(linkLable); return winForm; } /** * 遍历节点 * @param winForm * @return */ public static String getAllNode(Composite winForm){ String str = ""; for (Component c:winForm.getNode()){ if(c instanceof Leaf){ str += c.printfInfo() +"\n"; }else { str += getAllNode((Composite)c) +"\n"; } } return str; }}运行结果Picture(Logo图片)button登录button注册Lable用户名文本框密码PasswordBox密码框CheckBox复选框TextBox记住用户名LinkLable忘记密码
划线
评论
复制
发布于: 2020 年 06 月 24 日 阅读数: 32
魔曦
关注
我思故我在! 2018.01.15 加入
凡事有交代,件件有着落,事事有回音。
评论