图1 组合模式【点击查看图片】
1,以公司职员为例的结构
package com.cnblogs.mufasa.demo3;
import java.util.ArrayList;
import java.util.List;
public class Employee {
private String name;
private String dept;
private int salary;
private List<Employee> subordinates;
//构造函数
public Employee(String name,String dept, int sal) {
this.name = name;
this.dept = dept;
this.salary = sal;
subordinates = new ArrayList<Employee>();//【核心所在】
}
public void add(Employee e) {
subordinates.add(e);
}
public void remove(Employee e) {
subordinates.remove(e);
}
public List<Employee> getSubordinates(){
return subordinates;
}
public String toString(){
return ("Employee :[ Name : "+ name
+", dept : "+ dept + ", salary :"
+ salary+" ]");
}
}
package com.cnblogs.mufasa.demo3;
public class CompositePatternDemo {
public static void main(String[] args) {
Employee CEO = new Employee("John","CEO", 30000);
Employee headSales = new Employee("Robert","Head Sales", 20000);
Employee headMarketing = new Employee("Michel","Head Marketing", 20000);
Employee clerk1 = new Employee("Laura","Marketing", 10000);
Employee clerk2 = new Employee("Bob","Marketing", 10000);
Employee salesExecutive1 = new Employee("Richard","Sales", 10000);
Employee salesExecutive2 = new Employee("Rob","Sales", 10000);
CEO.add(headSales);
CEO.add(headMarketing);
headSales.add(salesExecutive1);
headSales.add(salesExecutive2);
headMarketing.add(clerk1);
headMarketing.add(clerk2);
//打印该组织的所有员工
System.out.println(CEO);
for (Employee headEmployee : CEO.getSubordinates()) {
System.out.println(headEmployee);
for (Employee employee : headEmployee.getSubordinates()) {
System.out.println(employee);
}
}
}
}
2,以枝叶为例的结构
package com.cnblogs.mufasa.demo1;
import java.util.ArrayList;
import java.util.List;
public class ComponentDemo {
public abstract class Component {//内部类
String name;
public abstract void add(Component c);
public abstract void remove(Component c);
public abstract void eachChild();
}
// 组合部件类
public class Leaf extends Component {//树叶
// 叶子节点不具备添加的能力,所以不实现
@Override
public void add(Component c) {
// TODO Auto-generated method stub
System.out.println("");
}
// 叶子节点不具备添加的能力必然也不能删除
@Override
public void remove(Component c) {
// TODO Auto-generated method stub
System.out.println("");
}
// 叶子节点没有子节点所以显示自己的执行结果
@Override
public void eachChild() {
// TODO Auto-generated method stub
System.out.println(name + "执行了");
}
}
// 组合类
public class Composite extends Component {//树枝
// 用来保存节点的子节点
List<Component> list = new ArrayList<Component>();
// 添加节点 添加部件
@Override
public void add(Component c) {
// TODO Auto-generated method stub
list.add(c);
}
// 删除节点 删除部件
@Override
public void remove(Component c) {
// TODO Auto-generated method stub
list.remove(c);
}
// 遍历子节点
@Override
public void eachChild() {
// TODO Auto-generated method stub
System.out.println(name + "执行了");
for (Component c : list) {
c.eachChild();
}
}
}
}
package com.cnblogs.mufasa.demo1;
public class Client {
public static void main(String[] args) {
ComponentDemo demo = new ComponentDemo();
// 构造根节点
ComponentDemo.Composite rootComposite = demo.new Composite();
rootComposite.name = "根节点";
// 左节点
ComponentDemo.Composite compositeLeft = demo.new Composite();
compositeLeft.name = "左节点";
// 构建右节点,添加两个叶子几点,也就是子部件
ComponentDemo.Composite compositeRight = demo.new Composite();
compositeRight.name = "右节点";
ComponentDemo.Leaf leaf1 = demo.new Leaf();
leaf1.name = "右-子节点1";
ComponentDemo.Leaf leaf2 = demo.new Leaf();
leaf2.name = "右-子节点2";
compositeRight.add(leaf1);
compositeRight.add(leaf2);
// 左右节点加入 根节点
rootComposite.add(compositeRight);
rootComposite.add(compositeLeft);
// 遍历组合部件
rootComposite.eachChild();
}
}
3,核心
将元素进行分级