week 3

发布于: 20 小时前

一、 Singleton

二、 组合模式处理树结构

  1. 统一接口 Component#rend

  2. Window 集合

这个编辑器真难用啊,不支持 markdown 表格,鼠标光标识别不了。强制 50 字,代码还不算。*****

public interface Component {
public void rend();
}
public class BaseCompoment {
private String label;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
public class Window extends BaseCompoment implements Component {
public Window(String label) {
setLabel(label);
}
private List<Component> components = new ArrayList<>();
public void addComponent(List<Component> components) {
this.components.addAll(components);
}
@Override
public void rend() {
System.out.println(this.getClass().getName() + " - " + this.getLabel());
for (int i = 0; i < components.size(); i++) {
Component component = components.get(i);
component.rend();
}
}
}
public class Button extends BaseCompoment implements Component {
public Button(String label) {
setLabel(label);
}
@Override
public void rend() {
System.out.println(this.getClass().getName() + " - " + this.getLabel());
}
}
package medo.pattern.oo.composite;
import java.util.ArrayList;
import java.util.List;
import medo.pattern.oo.composite.component.Button;
import medo.pattern.oo.composite.component.Frame;
import medo.pattern.oo.composite.component.Input;
import medo.pattern.oo.composite.component.Label;
import medo.pattern.oo.composite.component.Picture;
import medo.pattern.oo.composite.component.Window;
public class Login {
public void login() {
Window window = new Window("WINDOW");
List<Component> subWindow = new ArrayList<>();
subWindow.add(new Picture("Logo"));
window.addComponent(subWindow);
Frame frame = new Frame("Frame");
List<Component> asList = new ArrayList<>();
asList.add(new Input("input"));
asList.add(new Label("label"));
asList.add(new Button("login"));
asList.add(new Button("regist"));
frame.addComponent(asList);
List<Component> frameSub = new ArrayList<>();
frameSub.add(frame);
window.addComponent(frameSub);
window.rend();
}
}

发布于: 20 小时前 阅读数: 4
用户头像

陈皮

关注

还未添加个人签名 2018.04.26 加入

还未添加个人简介

评论

发布
暂无评论
week 3