设计模式14---组合模式

组合模式可以理解为是树的结构的一种构建方式。

Composite是可以拓展的树的分支,树理论上可以无限长。

 

package com.jayfulmath.designpattern.composite;

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 Display(int depth);
    public abstract void LineofDuty();
    
    protected void printfObjectName(int depth) {
        StringBuilder builder = new StringBuilder();
        for(int i=0;i<depth;i++)
        {
            builder.append("-");
        }
        
        System.out.println(builder.toString()+name);
    }
}
package com.jayfulmath.designpattern.composite;

public class FianceDepartment extends Company {

    public FianceDepartment(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Add(Company c) {
        // TODO Auto-generated method stub

    }

    @Override
    public void Remove(Company c) {
        // TODO Auto-generated method stub

    }

    @Override
    public void Display(int depth) {
        // TODO Auto-generated method stub
        printfObjectName(depth);
    }

    @Override
    public void LineofDuty() {
        // TODO Auto-generated method stub
        System.out.println(name+"\t staff payoff");
    }

}
package com.jayfulmath.designpattern.composite;

public class HRDepartment extends Company {

    public HRDepartment(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Add(Company c) {
        // TODO Auto-generated method stub

    }

    @Override
    public void Remove(Company c) {
        // TODO Auto-generated method stub

    }

    @Override
    public void Display(int depth) {
        // TODO Auto-generated method stub
        printfObjectName(depth);
    }

    @Override
    public void LineofDuty() {
        // TODO Auto-generated method stub
        System.out.println(name+"\t staff recruitment ");
    }

}
package com.jayfulmath.designpattern.composite;

import com.jayfulmath.designpattern.main.BasicExample;

public class CompositeMain extends BasicExample {

    @Override
    public void startDemo() {
        // TODO Auto-generated method stub
        CompositeCompany root = new CompositeCompany("北京总公司");
        root.Add(new HRDepartment("总公司HR"));
        root.Add(new FianceDepartment("总公司FD"));
        
        CompositeCompany comp = new CompositeCompany("上海分公司");
        comp.Add(new HRDepartment("上海分公司HR"));
        comp.Add(new FianceDepartment("上海分公司FD"));
        root.Add(comp);
        
        CompositeCompany comp1 = new CompositeCompany("浦东办事处");
        comp1.Add(new HRDepartment("浦东办事处HR"));
        comp1.Add(new FianceDepartment("浦东办事处FD"));
        comp.Add(comp1);
        
        CompositeCompany comp2 = new CompositeCompany("人民广场办事处");
        comp2.Add(new HRDepartment("人民广场办事处HR"));
        comp2.Add(new FianceDepartment("人民广场办事处FD"));
        comp.Add(comp2);
        
        System.out.println("结构图:");
        root.Display(1);
        
        System.out.println("职责:");
        root.LineofDuty();
    }

}
package com.jayfulmath.designpattern.composite;

import com.jayfulmath.designpattern.main.BasicExample;

public class CompositeMain extends BasicExample {

    @Override
    public void startDemo() {
        // TODO Auto-generated method stub
        CompositeCompany root = new CompositeCompany("北京总公司");
        root.Add(new HRDepartment("总公司HR"));
        root.Add(new FianceDepartment("总公司FD"));
        
        CompositeCompany comp = new CompositeCompany("上海分公司");
        comp.Add(new HRDepartment("上海分公司HR"));
        comp.Add(new FianceDepartment("上海分公司FD"));
        root.Add(comp);
        
        CompositeCompany comp1 = new CompositeCompany("浦东办事处");
        comp1.Add(new HRDepartment("浦东办事处HR"));
        comp1.Add(new FianceDepartment("浦东办事处FD"));
        comp.Add(comp1);
        
        CompositeCompany comp2 = new CompositeCompany("人民广场办事处");
        comp2.Add(new HRDepartment("人民广场办事处HR"));
        comp2.Add(new FianceDepartment("人民广场办事处FD"));
        comp.Add(comp2);
        
        System.out.println("结构图:");
        root.Display(1);
        
        System.out.println("职责:");
        root.LineofDuty();
    }

}

运行结果:

结构图:
-北京总公司
---总公司HR
---总公司FD
---上海分公司
-----上海分公司HR
-----上海分公司FD
-----浦东办事处
-------浦东办事处HR
-------浦东办事处FD
-----人民广场办事处
-------人民广场办事处HR
-------人民广场办事处FD
职责:
总公司HR     staff recruitment 
总公司FD     staff payoff
上海分公司HR     staff recruitment 
上海分公司FD     staff payoff
浦东办事处HR     staff recruitment 
浦东办事处FD     staff payoff
人民广场办事处HR     staff recruitment 
人民广场办事处FD     staff payoff

 

Leaf 是没有add和remove功能的,但是保留这两个接口可以是代码保持统一性.

 

posted on 2014-12-17 16:21  Joyfulmath  阅读(154)  评论(0编辑  收藏  举报

导航