设计模式-组合模式
2018-03-21 23:10
190 查看
1.UML类图
Component是组合中的对象声明接口,在适当情况下,实现所有类共有接口的默认行为,声明一个接口用于访问和管理Component的子部件。Leaf在组合中表示叶节点对象,叶节点对象没有子节点。Composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加add和删除remove。
2.示例代码
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract void add(Component component);
public abstract void remove(Component component);
public abstract void display(int depth);
}
public class Composite extends Component {
private List<Component> componentList = new ArrayList<>();
public Composite(String name) {
super(name);
}
@Override
public void add(Component component) {
componentList.add(component);
}
@Override
public void remove(Component component) {
componentList.remove(component);
}
@Override
public void display(int depth) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < depth; i++) {
stringBuilder.append('-');
}
System.out.println(stringBuilder + name);
for (Component component : componentList) {
component.display(depth + 2);
}
}
}
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public void add(Component component) {
System.out.println("cannot add to a leaf");
}
@Override
public void remove(Component component) {
System.out.println("cannot remove from a leaf");
}
@Override
public void display(int depth) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < depth; i++) {
stringBuilder.append('-');
}
System.out.println(stringBuilder + name);
}
}
public class Client {
public static void main(String[] args) {
Composite root = new Composite("root");
root.add(new Leaf("Leaf A"));
root.add(new Leaf("Leaf B"));
Composite comp = new Composite("Composite X");
comp.add(new Leaf("Leaf XA"));
comp.add(new Leaf("Leaf XB"));
root.add(comp);
Composite comp2 = new Composite("Composite XY");
comp2.add(new Leaf("Leaf XYA"));
comp2.add(new Leaf("Leaf XYB"));
comp.add(comp2);
root.add(new Leaf("Leaf C"));
Leaf leaf = new Leaf("Leaf D");
root.add(leaf);
root.remove(leaf);
root.display(1);
}
}
3.特点
组合模式,将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。需求中体现部分与整体层次结构时,以及你希望用户可以忽略组合对象与单个对象的不同,同意的使用组合结构中的所有对象时,考虑使用组合模式。
Component是组合中的对象声明接口,在适当情况下,实现所有类共有接口的默认行为,声明一个接口用于访问和管理Component的子部件。Leaf在组合中表示叶节点对象,叶节点对象没有子节点。Composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加add和删除remove。
2.示例代码
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract void add(Component component);
public abstract void remove(Component component);
public abstract void display(int depth);
}
public class Composite extends Component {
private List<Component> componentList = new ArrayList<>();
public Composite(String name) {
super(name);
}
@Override
public void add(Component component) {
componentList.add(component);
}
@Override
public void remove(Component component) {
componentList.remove(component);
}
@Override
public void display(int depth) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < depth; i++) {
stringBuilder.append('-');
}
System.out.println(stringBuilder + name);
for (Component component : componentList) {
component.display(depth + 2);
}
}
}
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public void add(Component component) {
System.out.println("cannot add to a leaf");
}
@Override
public void remove(Component component) {
System.out.println("cannot remove from a leaf");
}
@Override
public void display(int depth) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < depth; i++) {
stringBuilder.append('-');
}
System.out.println(stringBuilder + name);
}
}
public class Client {
public static void main(String[] args) {
Composite root = new Composite("root");
root.add(new Leaf("Leaf A"));
root.add(new Leaf("Leaf B"));
Composite comp = new Composite("Composite X");
comp.add(new Leaf("Leaf XA"));
comp.add(new Leaf("Leaf XB"));
root.add(comp);
Composite comp2 = new Composite("Composite XY");
comp2.add(new Leaf("Leaf XYA"));
comp2.add(new Leaf("Leaf XYB"));
comp.add(comp2);
root.add(new Leaf("Leaf C"));
Leaf leaf = new Leaf("Leaf D");
root.add(leaf);
root.remove(leaf);
root.display(1);
}
}
3.特点
组合模式,将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。需求中体现部分与整体层次结构时,以及你希望用户可以忽略组合对象与单个对象的不同,同意的使用组合结构中的所有对象时,考虑使用组合模式。
相关文章推荐
- 设计模式之组合模式
- Android设计模式系列-组合模式
- Java设计模式_(结构型)_组合模式
- 设计模式之组合模式
- 组合模式 - 设计模式 - PHP版
- .NET设计模式-组合模式(Composite Pattern)
- 编程常用设计模式详解--(中篇)(适配器、装饰、代理、外观、桥接、组合、享元)
- 大话设计-组合模式
- 【设计模式篇】Android设计模式之-组合模式 自我见解
- 4、常用设计模式之组合模式
- 设计模式--组合模式(composite)
- 设计模式之Composite(组合)
- 设计模式04-组合设计模式(component)
- Java语言设计模式之组合模式(Composite)
- 设计模式—组合模式
- 组合设计模式
- 设计模式-组合模式
- 设计模式(8) 组合模式(COMPOSITE)
- Python的结构型设计模式之组合模式
- 23种设计模式之组合模式(composite)