设计模式->结构型模式->组合模式(部分-整体模式)

1.定义:将对象组合成树形结构以表示"部分-整体"的层次结构,该模式时客户端对部分和整体的访问具有一致性。

2.UML(安全模式)


3.理论基础:封装,多态,继承

4.涉及角色:

        抽象构建角色:定义树叶节点以及树枝节点共有的行为和属性

        树叶构件:处于树形结构的末梢,在节点下没有子节点

树枝构件:在该节点中定义了树叶节点的行为,该行为属于新增行为也就是该行为对父类不可见。违背依赖倒置原则。通过新增的行为可以完成对整个树形结构的创建

5.优点

1.对整体和局部的访问具有一致性

2.节点增加自由,只要找该节点的父节点即可实现自由增加

6.缺点

1.安全模式:由于构建整个树形结构的行为定义在树枝节点中,导致在客户端无法使用抽象构件角色对整个树形结构进行操作。违背依赖倒置原则

2.透明模式:树形结构构建的行为放在了抽象构件中,导致叶子结点需要实现节点操作的逻辑,但是叶子结点下没有子节点。需要额外处理。可以采用迪米特原则优化。

7.使用场景

        1.维护和展示部分-整体的场景

        2.能够从整体中独立出来部分功能和模块的场景

在实际的过程中一般采用数据库实现树形结构,可以根据主外键关联来实现,从数据库中将数据捞出后将对应的数据添加到叶子结点和树枝节点即可。

8.Code

//抽象构件角色

public abstract class Root {
    protected String name;
    protected String title;

    public Root(String name, String title) {
        this.name = name;
        this.title = title;
    }

    public Root() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    //树叶以及树枝节点共有的的方法
    public abstract String getInfor();
}
//树枝构件

/树枝节点,在盖内中将自己完成组件的层次构建,即将组件形成一个树形结构
//该类中持有一个抽象构件角色的集合,该集合是形成树形结构关键
//在该类中重写了抽象构件角色中的方法
public class Branch extends Root {

    List<Root> list = new ArrayList<Root>();

    public Branch(String name, String title) {
        super(name, title);
    }

    public Branch() {
    }

    @Override
    public String getInfor() {
        return "Name: " + name + " \t " + "Title: " + title + " \t " + "Childrens: " + list.size();
    }

    public void add(Root root) {
        this.list.add(root);
    }

    public void remove(Root root) {
        this.list.remove(root);
    }

    public List<Root> getChildren() {
        return this.list;
    }
}

//树叶构件
//树叶节点
//该节点是树形结构中最末的节点也就是说该节点不会含有子节点
public class Leaf extends Root {

    public Leaf(String name, String title) {
        super(name, title);
    }

    public Leaf() {
    }

    @Override
    public String getInfor() {
        return "Name: " + name + " \t " + "Title: " + title + " \t " + "Childrens: " + 0;
    }
}

//客户端

public class Client {
    public static void main(String[] args) {
        Branch ceo = new Branch("WJP", "CEO");

        Branch cfo = new Branch("XC" , "CFO");
        Leaf iavn = new Leaf("Jack", "employee");
        cfo.add(iavn);

        Branch it = new Branch("ZS", "IT Leader");
        Leaf jack = new Leaf("Jack", "employee");
        Leaf jacks = new Leaf("Jacks", "employee");
        it.add(jack);
        it.add(jacks);


        Branch cho = new Branch("WF", "CHO");
        Leaf mary = new Leaf("Mary", "employee");
        Leaf maries = new Leaf("maries", "employee");
        cho.add(mary);
        cho.add(maries);

        //设置CEO下属
        ceo.add(initCto());
        ceo.add(cfo);
        ceo.add(it);
        ceo.add(cho);

        System.out.println(ceo.getInfor());
        output(ceo);
    }

    //递归便利CEO下属
    public static void output(Branch branch) {
        for (Root child : branch.getChildren() ) {
            if(child instanceof Branch) {
                System.out.println(child.getInfor());
                //强转
                output((Branch) child);
            } else if (child instanceof Leaf) {
                System.out.println(child.getInfor());
            }
        }
    }

    private static Root initCto() {
        Branch cto = new Branch("DuB","CTO");
        //总经理
        Branch mopg = new Branch("XLH", "MOPG General manager");
        //设置总经理下属
        mopg.add(initMopg1());
        mopg.add(initMopg2());

        cto.add(mopg);
        return cto;
    }

    private static Root initMopg1() {
        //设置业务部1
        Branch mopg1 = new Branch("ZH", "Mopg1 manager");

        Branch U11 = new Branch("Andy", "U11 Leader");
        Leaf makr = new Leaf("Mark", "employee");
        Leaf lycy = new Leaf("Lycy", "employee");
        U11.add(makr);
        U11.add(lycy);

        Branch U12 = new Branch("JT", "U12 Leader");
        Leaf tom = new Leaf("Tom", "employee");
        Leaf james = new Leaf("James", "employee");
        U12.add(tom);
        U12.add(james);

        mopg1.add(U11);
        mopg1.add(U12);

        return mopg1;
    }

    private static Root initMopg2() {
        //设置业务部2
        Branch mopg2 = new Branch("Kevin", "Mopg2 manager");

        Branch U13 = new Branch("Annie", "U13 Leader");
        Leaf mac = new Leaf("mac", "employee");
        Leaf tim = new Leaf("Tim", "employee");
        U13.add(mac);
        U13.add(tim);

        Branch U14 = new Branch("JT", "U14 Leader");
        Leaf tims = new Leaf("Tims", "employee");
        Leaf jame = new Leaf("Jame", "employee");
        U14.add(tims);
        U14.add(jame);

        mopg2.add(U13);
        mopg2.add(U14);

        return mopg2;
    }
}

透明模式

UML


Code

抽象构件角色

//抽象构建角色
public abstract class Root {
    protected String name;
    protected String title;
    protected Root parent;

    public Root(String name, String title) {
        this.name = name;
        this.title = title;
    }

    public Root() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setParent(Root parent) {
        this.parent = parent;
    }

    public Root getParent() {
        return parent;
    }

    //树叶以及树枝节点共有的的方法
    public abstract String getInfor();

    public abstract void add(Root root);

    public abstract void remove(Root root);

    public abstract List<Root> getChildren();


}
树叶构件

//树叶节点
//该节点是树形结构中最末的节点也就是说该节点不会含有子节点
public class Leaf extends Root {

    public Leaf(String name, String title) {
        super(name, title);
    }

    public Leaf() {
    }

    @Override
    public String getInfor() {
        return "Leader: " + this.getParent().getName() + "\t" + "Name: " + name + " \t " + "Title: " + title + " \t " + "Childrens: " + 0;
    }

    @Override
    @Deprecated
    public void add(Root root) throws UnsupportedOperationException{
        throw new UnsupportedOperationException("not support");
    }

    @Override
    @Deprecated
    public void remove(Root root) throws UnsupportedOperationException{
        throw new UnsupportedOperationException("not support");
    }

    @Override
    @Deprecated
    public List<Root> getChildren() throws UnsupportedOperationException{
        throw new UnsupportedOperationException("not support");
    }
}
树枝构件

//树枝节点,在盖内中将自己完成组件的层次构建,即将组件形成一个树形结构
//该类中持有一个抽象构件角色的集合,该集合是形成树形结构关键
//在该类中重写了抽象构件角色中的方法
public class Branch extends Root {

    List<Root> list = new ArrayList<Root>();

    public Branch(String name, String title) {
        super(name, title);
    }

    public Branch() {
    }

    @Override
    public String getInfor() {
        return "Leader: " + this.getParent().getName() + "\t" + "Name: " + name + " \t " + "Title: " + title + " \t " + "Childrens: " + list.size();
    }

    @Override
    public void add(Root root) {
        super.setParent(this);
        this.list.add(root);
    }


    @Override
    public void remove(Root root) {
        this.list.remove(root);
    }

    @Override
    public List<Root> getChildren() {
        return this.list;
    }
}

客户端

public class Client {
    public static void main(String[] args) {
        Root ceo = new Branch("WJP", "CEO");

        Root cfo = new Branch("XC" , "CFO");
        Root iavn = new Leaf("Jack", "employee");
        iavn.setParent(cfo);
        cfo.add(iavn);

        Root it = new Branch("ZS", "IT Leader");
        Root jack = new Leaf("Jack", "employee");
        jack.setParent(it);
        Root jacks = new Leaf("Jacks", "employee");
        jacks.setParent(it);
        it.add(jack);
        it.add(jacks);


        Root cho = new Branch("WF", "CHO");
        Root mary = new Leaf("Mary", "employee");
        mary.setParent(cho);
        Root maries = new Leaf("maries", "employee");
        maries.setParent(cho);
        cho.add(mary);
        cho.add(maries);

        //设置CEO下属
        ceo.add(initCto());
        ceo.add(cfo);
        ceo.add(it);
        ceo.add(cho);

        System.out.println(ceo.getInfor());
        output(ceo);
    }

    //递归便利CEO下属
    public static void output(Root branch) {
        for (Root child : branch.getChildren() ) {
            if(child instanceof Branch) {
                System.out.println(child.getInfor());
                output(child);
            } else if (child instanceof Leaf) {
                System.out.println(child.getInfor());
            }
        }
    }

    private static Root initCto() {
        Root cto = new Branch("DuB","CTO");
        //总经理
        Root mopg = new Branch("XLH", "MOPG General manager");
        //设置总经理下属
        mopg.add(initMopg1());
        mopg.add(initMopg2());

        cto.add(mopg);
        return cto;
    }

    private static Root initMopg1() {
        //设置业务部1
        Root mopg1 = new Branch("ZH", "Mopg1 manager");

        Root U11 = new Branch("Andy", "U11 Leader");
        Root makr = new Leaf("Mark", "employee");
        makr.setParent(U11);
        Root lycy = new Leaf("Lycy", "employee");
        lycy.setParent(U11);
        U11.add(makr);
        U11.add(lycy);

        Root U12 = new Branch("JT", "U12 Leader");
        Root tom = new Leaf("Tom", "employee");
        tom.setParent(U12);
        Root james = new Leaf("James", "employee");
        james.setParent(U12);
        //树叶节点添加子节点
        try {
            tom.add(james);
        } catch (UnsupportedOperationException e) {
            System.out.println(e.getMessage());
        }

        U12.add(tom);
        U12.add(james);

        mopg1.add(U11);
        mopg1.add(U12);

        return mopg1;
    }

    private static Root initMopg2() {
        //设置业务部2
        Root mopg2 = new Branch("Kevin", "Mopg2 manager");

        Root U13 = new Branch("Annie", "U13 Leader");
        Root mac = new Leaf("mac", "employee");
        mac.setParent(U13);
        Root tim = new Leaf("Tim", "employee");
        tim.setParent(U13);
        U13.add(mac);
        U13.add(tim);

        Root U14 = new Branch("JT", "U14 Leader");
        Root tims = new Leaf("Tims", "employee");
        tims.setParent(U14);
        Root jame = new Leaf("Jame", "employee");
        jame.setParent(U14);
        U14.add(tims);
        U14.add(jame);

        mopg2.add(U13);
        mopg2.add(U14);

        return mopg2;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值