Composite模式
应用场景
需求中是体现部分与整体层次的结构时 希望用户忽略组合对象与单个对象的不同, 统一的使用组合结构中的所有对象时
本质
优点
定义了包含基本对象和组合对象的类层次结构 基本对象可以组合成组合对象,组合对象又能组合成更复杂的组合对象,可以不断地递归组合下去,从而构成-一个统一的组 合对象的类层次结构 统一了组合对象和叶子对象 简化了客户端调用 不用区分组合对象和叶子对象 更容易扩展 由于客户端是统一的面对Component来操作,因此,新定义的Composite或leaf子类能够很容易的与已有的结构一起工作,而不需改变客户端
缺点
很难限制组合中的组件类型 这是容易添加新的组件带来的问题,在需要检测组件类型的时候,使得我们不能依靠编译期的类型约束来完成,必须在运行期间动态检测
角色
抽象构件(Component)角色 :这是一个抽象角色,它给参与组合的对象规定一个接口。这个角色给出共有接口及其默认行为。树叶构件(Leaf)角色 :代表参加组合的树叶对象。一个树叶对象没有下级子对象。树枝构件(Composite)角色 :代表参加组合的有子对象的对象,并给出树枝构件对象的行为。
Composite模式的类图
示例程序
示例程序的类图
示例代码
public abstract class Component {
protected String name;
public Component ( String name) {
super ( ) ;
this . name = name;
}
public abstract void add ( Component component) ;
public abstract void remove ( Component component) ;
public abstract void display ( int dept) ;
}
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 dept) {
String str= "" ;
for ( int i= 0 ; i< dept; i++ ) {
str+= "-" ;
}
System. out. println ( str+ name) ;
}
}
public class Composite extends Component {
private List< Component> children= new ArrayList < Component> ( ) ;
public Composite ( String name) {
super ( name) ;
}
@Override
public void add ( Component component) {
children. add ( component) ;
}
@Override
public void remove ( Component component) {
children. remove ( component) ;
}
@Override
public void display ( int dept) {
String str= "" ;
for ( int i= 0 ; i< dept; i++ ) {
str+= "-" ;
}
System. out. println ( str+ name) ;
for ( Component component : children) {
component. display ( dept + 2 ) ;
}
}
}
public class Main {
public static void main ( String[ ] args) {
System. out. println ( "商品识别树" ) ;
Component root = new Composite ( "服装" ) ;
Composite comp1 = new Composite ( "男装" ) ;
comp1. add ( new Leaf ( "衬衣" ) ) ;
comp1. add ( new Leaf ( "夹克" ) ) ;
root. add ( comp1) ;
Composite comp2 = new Composite ( "女装" ) ;
comp2. add ( new Leaf ( "衬衣" ) ) ;
comp2. add ( new Leaf ( "夹克" ) ) ;
root. add ( comp2) ;
root. display ( 1 ) ;
}
}
拓展思路
多个和单个的一致性(容器与内容的一致性) 到处都存在递归结构
相关设计模式
Command模式 使用Command模式编写宏命令时使用了Composite 模式。 Visitor模式 可以使用Visitor模式访问Composite模式中的递归结构。 Decorator模式(第12章) Composite模式通过Component角色使容器( Composite角色)和内容(Leaf角色)具有一 致性。 Decorator模式使装饰框和内容具有一致性。
满足的原则