梦想还是要有的,万一忘了咋办?

0%

组合模式

概述

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

类图

upload successful

角色

  • 抽象构件(Component)角色  
    该角色定义参加组合对象的共有方法和属性,规范一些默认的行为接口。
  • 叶子构件(Leaf)角色  
    该角色是叶子对象,其下没有其他分支,定义出参加组合的原始对象行为。
  • 树枝构件(Composite)角色  
    该角色代表参加组合的、其下有分支的树枝对象,他的作用是将树枝和叶子组合成一个树形结构,并定义出管理子对象的方法,如add()、remove()等。

示例

Component.java

1
2
3
public interface Component{
public void operation();
}

Composite.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Composite implements Component{
private ArrayList<Component> componentList=new ArrayList<Component>();
public void add(Component component){
this.componentList.add(component);
}
public void remove(Component component){
this.componentList.remove(component);
}

public ArrayList<Component> getChild(){
return this.componentList;
}
public void operation(){
//业务代码
}
}

Leaf.java

1
2
3
4
5
public class Leaf implements Component{
public void operation(){
  //业务代码
   }
}

Client.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Client{
public static void main(String args[]){
  //创建一个根节点
Composite root=new Composite();
root.operation();
       //创建树枝节点
Composite branch=new Composite();
       //创建叶子节点
Leaf leaf=new Leaf();
root.add(branch);
branch.add(leaf);
   }

public static void display(Composite root){
for(Composite c:root.getChild()){
      if(c instaceof Leaf){
c.operation();
}else{
c.operation();
               display((Composite)c);//递归调用
           }
       }

}
}

应用

优点

  • 高层模块调用简单。
    一颗树形机构中的所有节点都是Component,局部和整体对调用者来说没有任何区别,即高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。
  • 节点自由增加。  
    使用组合模式后,如果想增加一个树枝节点、树叶节点只需要找到其父节点即可。

缺点

  • 不易控制树枝结构的类型;
  • 不易使用继承的方法来增加新的行为。

场景

  • 需要描述对象的部分和整体的等级结构,如树形菜单、文件和文件夹管理。
  • 需要客户端忽略个体构件和组合构件的区别,平等对待所有的构件。

组合模式也是应用广泛的一种设计模式,例如,java基础类库的swing部分中就大量使用了组合模式,大部分控件都是JComponent的子类,同时其add()方法又可向界面添加JComponent类型的控件,从而使得使用者可以统一的方式操作各种控件。

实例

upload successful
Company.java

1
2
3
4
public interface Company{
//获取信息
public String getInfo();
}

ConcreteCompany.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//树枝节点类
public class ConcreteCompany{
private ArrayList<Company> companyList=new ArrayList<Company>();
private String name;
private String position;
private int salary;

public ConcreteCompany(String name,String position,int salary ){
this.name=name;
this.position=position;
this.salary=salary;
}
public void add(Company company){
this.companyList.add(company);
}
public void remove(Company company){
this.companyList.remove(company);
}
public ArrayList<Company> getChild(){
return this.companyList;
}
public String getInfo(){
String info="";
       info="名称:"+this.name;
       info=info+"\t职位:"+this.position;
       info=info+"\t薪水:"+this.salary;
return info;
   }
}

Employee.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Employee implements Company{
private String name;
private String position;
private int salary;

public Employee(String name,String position,int salary){
this.name=name;
this.position=position;
this.salary=salary;
}
public String getInfo(){
Stirng info="";
       info+="名称:"+this.name;
       info+="\t职称:"+this.position;
       info+="\t薪水:"+this.salary;
}
}

ClientDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class ClientDemo{
public static void main(String args[]){
  ConcreteCompany root=new ConcreteCompany("张三","CEO",10000);
       ConcreteCompany developDep=new ConcreteCompany("李四","研发经理",8000);
       ConcreteCompany salDep=new ConcreteCompany("王五","销售经理",8000);
       ConcreteCompany financeDep=new ConcreteCompan("马六","财务经理",8000);

       Employee e1=new Employee("A","研发部",3000);
Employee e2=new Employee("B","研发部",3000);
       Employee e3=new Employee("C","研发部",3000);
       Employee e4=new Employee("D","销售部",4000);
Employee e5=new Employee("E","销售部",3000);
Employee e6=new Employee("F","销售部",2000);
Employee e7=new Employee("G","销售部",3000);
       Employee e8=new Employee("H","财务部",4000);
Employee e9=new Employee("I","财务部",2000);
       root.add(developDep);
root.add(salesDep);
root.add(financeDep);
developDep.add(e1);
developDep.add(e2);
developDep.add(e3);
salesDep.add(e4);
salesDep.add(e5);
salesDep.add(e6);
salesDep.add(e7);
financeDep.add(e8);
financeDep.add(e9);
System.out.println(root.getInfo());
display(root);
}

public static void display(ConcreteCompany root){
for(Company c:root.getChild()){
if(c instanceof Employee){
System.out.println(e.getInfo());
}else{
System.out.println("\n"+c.getInfo());
display((ConcreteCompany)c);
}
}
}
}