week3- 作业一
发布于: 2020 年 06 月 23 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2.请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
类图关系:
代码如下:
public interface Component { /** * 输出内容 **/ void print();}public class BaseComponent implements Component { private final String text; private final String type; protected BaseComponent(String text, String type) { this.text = text; this.type = type; } public String getText() { return text; } public String getType() { return type; } @Override public void print() { System.out.println(String.format("print %s(%s)", getType(), getText())); }}public interface ListableComponent extends Component { /** * 添加组件 * * @param component */ void addComponent(Component component);}public class BaseListableComponent extends BaseComponent implements ListableComponent { private List<Component> children; protected BaseListableComponent(String text, String type) { super(text, type); children = new ArrayList<>(); } @Override public void addComponent(Component component) { this.children.add(component); } @Override public void print() { super.print(); for (Component child : children) { child.print(); } }}public interface ComponentFactory { /** * 构建组件 * * @param text * @return */ Component build(String text);}public enum ComponentType implements ComponentFactory { Button(new ComponentFactory() { @Override public Component build(String text) { return new BaseComponent(text, Button.name()); } }), Picture(new ComponentFactory() { @Override public Component build(String text) { return new BaseComponent(text, Picture.name()); } }), WinForm(new ComponentFactory() { @Override public Component build(String text) { return new BaseListableComponent(text, WinForm.name()); } }), Frame(new ComponentFactory() { @Override public Component build(String text) { return new BaseListableComponent(text, Frame.name()); } }), Label(new ComponentFactory() { @Override public Component build(String text) { return new BaseComponent(text, Label.name()); } }), TextBox(new ComponentFactory() { @Override public Component build(String text) { return new BaseComponent(text, TextBox.name()); } }), PasswordBox(new ComponentFactory() { @Override public Component build(String text) { return new BaseComponent(text, PasswordBox.name()); } }), CheckBox(new ComponentFactory() { @Override public Component build(String text) { return new BaseComponent(text, CheckBox.name()); } }), LinkLabel(new ComponentFactory() { @Override public Component build(String text) { return new BaseComponent(text, LinkLabel.name()); } }) ; private final ComponentFactory factory; ComponentType(ComponentFactory factory) { this.factory = factory; } public static ComponentType nameOf(String name) { return ComponentType.valueOf(ComponentType.class, name); } @Override public Component build(String text) { return factory.build(text); }
上面是接口及实现,下面是测试代码:
public class ComponentMain { public static void main(String[] args) { String[][] components = { {"WinForm", "WINDOW窗口"}, {"Picture", "LOGO图片"}, {"Button", "登录"}, {"Button", "注册"}, {"Frame", "FRAME1"}, {"Label", "用户名"}, {"TextBox", "文本框"}, {"Label", "密码"}, {"PasswordBox", "密码框"}, {"CheckBox", "复选框"}, {"TextBox", "记住用户名"}, {"LinkLabel", "忘记密码"} }; Map<Integer, int[]> groupings = new HashMap<>(); groupings.put(0, new int[]{1, 2, 3, 4}); groupings.put(4, new int[]{5, 6, 7, 8, 9, 10, 11}); Component root = buildRoot(components, groupings, 0); root.print(); } private static Component buildRoot(String[][] components, Map<Integer, int[]> groupings, int rootIndex) { List<Component> componentList = new ArrayList<>(components.length); for (String[] component : components) { componentList.add(ComponentType.nameOf(component[0]).build(component[1])); } Component root = componentList.get(rootIndex); for (Map.Entry<Integer, int[]> entry : groupings.entrySet()) { Component parent = componentList.get(entry.getKey()); if (!(parent instanceof ListableComponent)) { throw new IllegalStateException("组件类型错误"); } ListableComponent her = (ListableComponent) parent; for (int i : entry.getValue()) { her.addComponent(componentList.get(i)); } } return root; }}
划线
评论
复制
发布于: 2020 年 06 月 23 日阅读数: 41
版权声明: 本文为 InfoQ 作者【Coder】的原创文章。
原文链接:【http://xie.infoq.cn/article/4b9bb8e39c97303793ca6c440】。未经作者许可,禁止转载。
Coder
关注
还未添加个人签名 2018.05.04 加入
还未添加个人简介
评论