GoF定义:将对象组织成树状结构来表示整体--部分的关系,组合模式使得客户端对一个单独对象和一组对象的行为不变(认知相同)
客户端使用一个组合对象就和使用一个单独对象一样。在OO语言中,当我们有很多对象,它们有一些共有特性时,我们会将其组成组合对象。它们的关系被称为“has-a”的关系
现实世界:企业的组织结构(所有员工是这个企业的组成单元,不同的员工组成不同的部门,不同的部门构成了整个企业组织)
代码世界:任何的树状数据结构都符合这个模式
这里模拟一个大学组织。有两个系,每个系有几名导师
public class CompositePatternEx
{
public static void main(String[] args)
{
Teacher Principal = new Teacher("Dr.S.Som","Principal");
Teacher hodMaths = new Teacher("Mrs.S.Das","Hod-Math");
Teacher hodCompSc = new Teacher("Mr. V.Sarcar","Hod-ComputerSc.");
Teacher mathTeacher1 = new Teacher("Math Teacher-1","MathsTeacher");
Teacher mathTeacher2 = new Teacher("Math Teacher-2","MathsTeacher");
Teacher cseTeacher1 = new Teacher("CSE Teacher-1","CSETeacher");
Teacher cseTeacher2 = new Teacher("CSE Teacher-2","CSETeacher");
Teacher cseTeacher3 = new Teacher("CSE Teacher-3","CSETeacher");
//Principal is on top of college
/*HOD -Maths and Comp. Sc. directly reports to him*/
Principal.add(hodMaths);
Principal.add(hodCompSc);
/*Teachers of Mathematics directly reports to HOD-Maths*/
hodMaths.add(mathTeacher1);
hodMaths.add(mathTeacher2);
/*Teachers of Computer Sc. directly reports to HOD-Comp.Sc.*/
hodCompSc.add(cseTeacher1);
hodCompSc.add(cseTeacher2);
hodCompSc.add(cseTeacher3);
/*Leaf nodes. There is no department under Mathematics*/
mathTeacher1.add(null);
mathTeacher2.add(null);
/*Leaf nodes. There is no department under CSE.*/
cseTeacher1.add(null);
cseTeacher2.add(null);
cseTeacher3.add(null);
//Printing the details
System.out.println("***COMPOSITE PATTERN DEMO ***");
System.out.println("\nThe college has following structure\n");
System.out.println(Principal.getDetails());
List<ITeacher> hods=Principal.getControllingDeps();
for(int i=0;i<hods.size();i++)
{
System.out.println("\t"+hods.get(i).getDetails());
}
List<ITeacher> mathTeachers=hodMaths.getControllingDeps();
for(int i=0;i<mathTeachers.size();i++)
{
System.out.println("\t\t"+mathTeachers.get(i).getDetails());
}
List<ITeacher> cseTeachers=hodCompSc.getControllingDeps();
for(int i=0;i<cseTeachers.size();i++)
{
System.out.println("\t\t"+cseTeachers.get(i).getDetails());
}
//One computer teacher is leaving
hodCompSc.remove(cseTeacher2);
System.out.println("\n After CSE Teacher-2 leaving the organization- CSE department has following employees:");
cseTeachers = hodCompSc.getControllingDeps();
for(int i=0;i<cseTeachers.size();i++)
{
System.out.println("\t\t" + cseTeachers.get(i).getDetails());
}
}
}
interface ITeacher
{
public String getDetails();
}
class Teacher implements ITeacher
{
private String teacherName;
private String depName;
private List<ITeacher> controls;
public Teacher(String teacherName, String depName)
{
this.teacherName = teacherName;
this.depName = depName;
this.controls = new ArrayList<>(16);
}
public void add(ITeacher teacher)
{
controls.add(teacher);
}
public void remove(ITeacher teacher)
{
controls.remove(teacher);
}
public List<ITeacher> getControllingDeps()
{
return controls;
}
@Override
public String getDetails()
{
return (teacherName + " is the " + depName);
}
}
使用什么结构来存储子结构?
没有通用规则,动态数组、链表、树、数组都可以,根据实际操作情况来选择
这个模式的核心在于所有对象实现同一个接口,此时符合这个模式对整体和对部分的表现一致性的要求。否则只是一般的对象组合
原文:https://www.cnblogs.com/mouseGo/p/14127278.html