当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 大话设计模式笔记(十六)の组合模式

大话设计模式笔记(十六)の组合模式

2019年08月11日  | 移动技术网IT编程  | 我要评论

组合模式

定义

将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

uml图

模板代码

component

/**
 * 组合对象
 * created by callmedevil on 2019/8/11.
 */
public abstract class component {

    protected string name;

    public component(string name){
        this.name = name;
    }

    // 通常都用add 和remove 方法来提供增加或移除树叶或树枝的功能
    public abstract void add(component c);
    public abstract void remove(component c);
    public abstract void dispaly(int depth);

}

composite

/**
 * 枝节点行为,用来存储子部件
 * created by callmedevil on 2019/8/11.
 */
public class composite extends component{

    // 一个子对象集合用来存储其下属枝节点和叶节点
    private list<component> children = new arraylist<>();

    public composite(string name){
        super(name);
    }

    @override
    public void add(component c) {
        children.add(c);
    }

    @override
    public void remove(component c) {
        children.remove(c);
    }

    @override
    public void dispaly(int depth) {
        stringbuilder sb = new stringbuilder(depth);
        for (int i = 0; i < depth; i++) {
            sb.append("-");
        }
        // 显示枝节点名称,并对其下级进行遍历
        system.out.println(string.format("%s %s", sb.tostring(), name));
        for (component child : children) {
            child.dispaly(depth + 3);
        }
    }

}

leaf

/**
 * 叶节点对象
 * created by callmedevil on 2019/8/11.
 */
public class leaf extends component {

    public leaf(string name) {
        super(name);
    }

    @override
    public void add(component c) {
        system.out.println("cannot add to a leaf");
    }

    @override
    public void remove(component c) {
        system.out.println("cannot remove from a leaf");
    }

    @override
    public void dispaly(int depth) {
        stringbuilder sb = new stringbuilder(depth);
        for (int i = 0; i < depth; i++) {
            sb.append("-");
        }
        // 显示名称和级别
        system.out.println(string.format("%s %s", sb.tostring(), name));
    }

}

测试

public class test {
    public static void main(string[] args) {
        // 生成树根root,根上长出两叶leafa 和leafb
        composite root = new composite("root");
        root.add(new leaf("leaf a"));
        root.add(new leaf("leaf b"));
        // 根上长出分枝 composite x ,分枝上也有两叶leafxa 和leafxb
        composite comp = new composite("composite x");
        comp.add(new leaf("leaf xa"));
        comp.add(new leaf("leaf xb"));
        root.add(comp);
        // 分枝 composite x上再长出分枝 composite xy ,分枝上也有两叶leaf xya 和leaf xyb
        composite comp2 = new composite("composite xy");
        comp2.add(new leaf("leaf xya"));
        comp2.add(new leaf("leaf xyb"));
        comp.add(comp2);
        // 根部又长出两叶leafc 和leafd,可惜leafd没长牢,被风吹走了
        root.add(new leaf("leaf c"));
        leaf leafd = new leaf("leaf d");
        root.add(leafd);
        root.remove(leafd);
        // 显示大树
        root.dispaly(1);
    }
}

测试结果

- root
---- leaf a
---- leaf b
---- composite x
------- leaf xa
------- leaf xb
------- composite xy
---------- leaf xya
---------- leaf xyb
---- leaf c

透明方式与安全方式

树可能有无数的分枝,反复使用 composite 就可以实现树状结构,但树叶不可以再长分枝,为什么此处 leaf 当中也有实现 add 和 remove?

这种叫做透明方式,也就是说在 component 中声明所有用来管理子对象的方法,其中包括 add, remove 等。这样实现 component 接口的所有子类都具备了 add 和 remove。这样做的好处是叶节点和枝节点对于外界没有区别,它们具备完全一致的行为接口,但问题也明显,因为本身不具体 add 和 remove 功能,所以实现是没有意义的。

当然也可以不去实现,这种叫做安全方式,也就是在 component 接口中不去声明 add 和 remove 方法,那么子类的 leaf 也就不需要去实现,而是在 composite 声明所有用来管理子类对象的方法,不过由于不够透明,所以树叶和树枝将不具有相同的接口,客户端的调用需要做相应的判断,带来了不便。

何时使用?

当需求中是体现部分与整体层次的结构时,以及希望用户可以忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时,就应该考虑用组合模式了。

举个栗子

问题描述

总公司与分公司关系,总公司的所有管理功能同样能使用在分公司。

代码实现

company

/**
 * 公司抽象类
 * created by callmedevil on 2019/8/11.
 */
public abstract class company {

    protected string name;

    public company(string name){
        this.name = name;
    }

    public abstract void add(company c);
    public abstract void remove(company c);
    public abstract void dispaly(int depth);
    public abstract void lineofduty(); // 履行职责,不同部门需要履行不同的职责

    // 用于输出层次结构,非必须,与模式无关
    public string getdepth(int depth){
        stringbuilder sb = new stringbuilder(depth);
        for (int i = 0; i < depth; i++) {
            sb.append("-");
        }
        return sb.tostring();
    }

}

concretecompany

/**
 * 具体公司(树枝节点)
 * created by callmedevil on 2019/8/11.
 */
public class concretecompany extends company {

    private list<company> children = new arraylist<>();

    public concretecompany(string name) {
        super(name);
    }

    @override
    public void add(company c) {
        children.add(c);
    }

    @override
    public void remove(company c) {
        children.remove(c);
    }

    @override
    public void dispaly(int depth) {
        system.out.println(string.format("%s %s", getdepth(depth), name));
        for (company child : children) {
            child.dispaly(depth + 3);
        }
    }

    @override
    public void lineofduty() {
        for (company child : children) {
            child.lineofduty();
        }
    }

}

hrdepartment

/**
 * 人力资源部(树叶节点)
 * created by callmedevil on 2019/8/11.
 */
public class hrdepartment extends company{

    public hrdepartment(string name){
        super(name);
    }

    @override
    public void add(company c) {}

    @override
    public void remove(company c) {}

    @override
    public void dispaly(int depth) {
        system.out.println(string.format("%s %s", getdepth(depth), name));
    }

    @override
    public void lineofduty() {
        system.out.println(string.format("%s 员工招聘培训管理", name));
    }

}

financedepartment

/**
 * 财务部(树叶节点)
 * created by callmedevil on 2019/8/11.
 */
public class financedepartment extends company{

    public financedepartment(string name){
        super(name);
    }

    @override
    public void add(company c) {}

    @override
    public void remove(company c) {}

    @override
    public void dispaly(int depth) {
        system.out.println(string.format("%s %s", getdepth(depth), name));
    }

    @override
    public void lineofduty() {
        system.out.println(string.format("%s 公司财务收支管理", name));
    }

}

测试

public class test {
    public static void main(string[] args) {
        concretecompany root = new concretecompany("北京总公司");
        root.add(new hrdepartment("总公司人力资源部"));
        root.add(new financedepartment("总公司财务部"));

        concretecompany comp = new concretecompany("上海分公司");
        comp.add(new hrdepartment("上海分公司人力资源部"));
        comp.add(new financedepartment("上海分公司财务部"));
        root.add(comp);

        concretecompany comp1 = new concretecompany("南京办事处");
        comp1.add(new hrdepartment("南京办事处人力资源部"));
        comp1.add(new financedepartment("南京办事处财务部"));
        comp.add(comp1);

        concretecompany comp2 = new concretecompany("杭州办事处");
        comp2.add(new hrdepartment("杭州办事处人力资源部"));
        comp2.add(new financedepartment("杭州办事处财务部"));
        comp.add(comp2);

        system.out.println("\n 结构图:");
        root.dispaly(1);

        system.out.println("\n 职责:");
        root.lineofduty();
    }
}

测试结果

 结构图:
- 北京总公司
---- 总公司人力资源部
---- 总公司财务部
---- 上海分公司
------- 上海分公司人力资源部
------- 上海分公司财务部
------- 南京办事处
---------- 南京办事处人力资源部
---------- 南京办事处财务部
------- 杭州办事处
---------- 杭州办事处人力资源部
---------- 杭州办事处财务部

 职责:
总公司人力资源部 员工招聘培训管理
总公司财务部 公司财务收支管理
上海分公司人力资源部 员工招聘培训管理
上海分公司财务部 公司财务收支管理
南京办事处人力资源部 员工招聘培训管理
南京办事处财务部 公司财务收支管理
杭州办事处人力资源部 员工招聘培训管理
杭州办事处财务部 公司财务收支管理

总结

  • 组合模式定义了包含基本对象、组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,这样不断的递归下去,客户代码中,任何用到基本对象的地方都可以使用组合对象了。
  • 用户是不用关心到底是处理一个叶节点对象还是处理一个组合组件,也就用不着为定义组合而写一些选择判断语句了。
  • 组合模式让客户可以一致的使用组合结构和单个对象。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网