组合模式
基本介绍实例:学校的组成第一步:创建抽象构件(Component)第二步:创建容器构件(Composite) 学校第三步:创建容器构件(Composite)学院第四步:创建叶子构件(Leaf),系
总结优点缺点
基本介绍
组合模式(Composite Pattern):组合多个对象形成树形结构以表示具有“整体—部分”关系的层次结构。组合模式对单个对象(即叶子对象)和组合对象(即容器对象)的使用具有一致性,是一种对象结构型模式。
涉及三个角色: 抽象构件(Component):可以是接口或者抽象类,是叶子构件和容器构件的接口,包含子类行为的声明和实现。
Leaf(叶子构件):在组合中表示叶子节点,叶子节点没有子节点。
Composite(容器构件):非叶子节点,用于存储子部件,在Component 接口中实现子部件的相关操作,比如增加,删除。
实例:学校的组成
一个学校下有多个学院,一个学院下有多个系 代码实现:
第一步:创建抽象构件(Component)
public abstract class OraganizationComponent {
private String name
;
private String des
;
public OraganizationComponent(String name
, String des
) {
this.name
= name
;
this.des
= des
;
}
protected void add(OraganizationComponent oraganizationComponent
){
throw new UnsupportedOperationException();
}
protected void remove(OraganizationComponent oraganizationComponent
){
throw new UnsupportedOperationException();
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public String
getDes() {
return des
;
}
public void setDes(String des
) {
this.des
= des
;
}
protected abstract void print();
}
第二步:创建容器构件(Composite) 学校
public class University extends OraganizationComponent{
List
<OraganizationComponent> componentList
= new ArrayList<OraganizationComponent>();
public University(String name
, String des
) {
super(name
, des
);
}
@Override
protected void add(OraganizationComponent oraganizationComponent
) {
componentList
.add(oraganizationComponent
);
}
@Override
protected void remove(OraganizationComponent oraganizationComponent
) {
componentList
.remove(oraganizationComponent
);
}
@Override
public String
getName() {
return super.getName();
}
@Override
public String
getDes() {
return super.getDes();
}
@Override
protected void print() {
System
.out
.println("----------"+getName()+"-----------");
for(OraganizationComponent component
:componentList
){
component
.print();
}
}
}
第三步:创建容器构件(Composite)学院
public class College extends OraganizationComponent {
List
<OraganizationComponent> componentList
= new ArrayList<OraganizationComponent>();
public College(String name
, String des
) {
super(name
, des
);
}
@Override
protected void add(OraganizationComponent oraganizationComponent
) {
componentList
.add(oraganizationComponent
);
}
@Override
protected void remove(OraganizationComponent oraganizationComponent
) {
componentList
.remove(oraganizationComponent
);
}
@Override
public String
getName() {
return super.getName();
}
@Override
public String
getDes() {
return super.getDes();
}
@Override
protected void print() {
System
.out
.println("----------"+getName()+"-----------");
for(OraganizationComponent component
:componentList
){
component
.print();
}
}
}
第四步:创建叶子构件(Leaf),系
public class Department extends OraganizationComponent{
public Department(String name
, String des
) {
super(name
, des
);
}
@Override
protected void print() {
System
.out
.println(getName());
}
@Override
public String
getName() {
return super.getName();
}
@Override
public String
getDes() {
return super.getDes();
}
}
测试
public class Client {
public static void main(String
[] args
) {
University university
= new University("清华大学", "中国顶级大学");
College college
= new College("计算机学院", "计算机学院");
College college2
= new College("信息工程学院", "信息工程学院");
college
.add(new Department("软件工程","软件工程不错"));
college
.add(new Department("网络工程","网络工程不错"));
college
.add(new Department("计算机科学与技术","计算机科学与技术不错"));
college2
.add(new Department("通信工程","通信工程不好学"));
college2
.add(new Department("信息工程","信息工程不好学"));
university
.add(college
);
university
.add(college2
);
university
.print();
}
}
运行结果
总结
优点
1)简化客户端操作,客户端只需面对一致的对象而不用考虑整体部分或者叶子节点问题。 2)扩展性很强,如果需要在学院跟系中间加一层,只需调整内部的层次关系,客户端不用改变
缺点
1)要求较高的抽象性,就是叶子和中间节点有很多差异性,就是很多方法或者属性不一样,就不适合组合模式。