首页 > 其他 > 详细

组合模式

时间:2020-12-13 08:13:07      阅读:30      评论:0      收藏:0      [点我收藏+]

Composite Patterns

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);
    }
}

Note

  1. 这个模式完美适用于表达整体--部分的关系
  2. 客户端对于整体和个体的操作是一致的(ITeacher接口)
  3. 客户端可以添加新类型的组件(实现接口即可)
  4. 如果要维护子结构的顺序,需要写专门的维护(插入)顺序的代码

使用什么结构来存储子结构?
没有通用规则,动态数组、链表、树、数组都可以,根据实际操作情况来选择

思考

这个模式的核心在于所有对象实现同一个接口,此时符合这个模式对整体和对部分的表现一致性的要求。否则只是一般的对象组合

组合模式

原文:https://www.cnblogs.com/mouseGo/p/14127278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!