欢迎大家来到IT世界,在知识的湖畔探索吧!
组合模式(Composite Pattern):
一种将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性;又称为部分-整体模式。
组合模式,使得客户端代码可以一致地处理单个对象和组合对象,而无须关心处理的是单个对象,还是组合对象;更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足OCP原则;
组合模式的角色:
抽象构件(Component)、树叶构件(Leaf)和树枝构件(Composite);
组合模式的案例:
一般情况下,一个学校的院系包含多个学院,一个学院又包含多个系,如下,使用组合模式实现;
案例类图:
案例代码:
/**
* 抽象组件
*/
public abstract class OrganizeComponent {
// 名称
private String name;
// 描述
private String desc;
/**
* 增加
*/
protected void add(OrganizeComponent organizeComponent) {
throw new UnsupportedOperationException();
}
/**
* 移除
*/
protected void remove(OrganizeComponent organizeComponent) {
throw new UnsupportedOperationException();
}
/**
* 打印
*/
protected abstract void print();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
欢迎大家来到IT世界,在知识的湖畔探索吧!
欢迎大家来到IT世界,在知识的湖畔探索吧!import java.util.ArrayList;
import java.util.List;
/**
* 树枝构件
*/
public class UniversityComposite extends OrganizeComponent {
private List<OrganizeComponent> organizeList = new ArrayList<OrganizeComponent>();
public UniversityComposite(String name, String desc) {
super.setName(name);
super.setDesc(desc);
}
public UniversityComposite(List<OrganizeComponent> organizeList) {
this.organizeList = organizeList;
}
@Override
protected void add(OrganizeComponent organizeComponent) {
organizeList.add(organizeComponent);
}
@Override
protected void remove(OrganizeComponent organizeComponent) {
organizeList.remove(organizeComponent);
}
@Override
protected void print() {
System.out.println("学校名称:" + this.getName());
for (OrganizeComponent organize : this.organizeList) {
organize.print();
}
}
}
/**
* 树叶构件
*/
public class DepartmentLeaf extends OrganizeComponent {
public DepartmentLeaf(String name,String desc) {
super.setName(name);
super.setDesc(desc);
}
@Override
protected void print() {
System.out.println("系名称:" + this.getName() + "" );
}
}
欢迎大家来到IT世界,在知识的湖畔探索吧!public class CollegeComposite extends OrganizeComponent {
private List<OrganizeComponent> organizeList = new ArrayList<OrganizeComponent>();
public CollegeComposite(String name, String desc) {
super.setName(name);
super.setDesc(desc);
}
public CollegeComposite(List<OrganizeComponent> organizeList) {
this.organizeList = organizeList;
}
@Override
protected void add(OrganizeComponent organizeComponent) {
organizeList.add(organizeComponent);
}
@Override
protected void remove(OrganizeComponent organizeComponent) {
organizeList.remove(organizeComponent);
}
@Override
protected void print() {
System.out.println("学院名称:" + this.getName() + "");
for (OrganizeComponent organize : this.organizeList) {
organize.print();
}
}
}
public class CompositeClient {
public static void main(String[] args) {
// 大学
OrganizeComponent university = new UniversityComposite("清华大学","中国顶级大学");
// 学院
OrganizeComponent computerCollege = new CollegeComposite("计算机学院","计算机学院");
university.add(computerCollege);
OrganizeComponent infoEngineerCollege = new CollegeComposite("信息工程学院","信息工程学院");
university.add(infoEngineerCollege);
// 系
OrganizeComponent computerApply = new DepartmentLeaf("计算机应用","计算机应用");
computerCollege.add(computerApply);
OrganizeComponent computerNetwork = new DepartmentLeaf("计算机网络","计算机网络");
computerCollege.add(computerNetwork);
OrganizeComponent computerTel = new DepartmentLeaf("计算机科学与技术","计算机科学与技术");
computerCollege.add(computerTel);
OrganizeComponent communication = new DepartmentLeaf("通信工程","通信工程");
infoEngineerCollege.add(communication);
OrganizeComponent information = new DepartmentLeaf("信息工程","信息工程");
infoEngineerCollege.add(information);
// 打印信息
// university.print();
// computerCollege.print();
infoEngineerCollege.print();
}
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/13309.html