复合设计模式

复合模式说, 只是“允许客户端以通用方式对可能表示对象层次结构的对象进行操作”。
复合设计模式的优势

  • 它定义了包含原始对象和复杂对象的类层次结构。
  • 它使你更容易添加新的组件。
  • 它通过可管理的类或接口提供结构的灵活性。
复合图案的用法 它用于:
  • 当你要表示对象的全部或部分层次结构时。
  • 需要将职责动态添加到单个对象时, 而不影响其他对象。对象的责任可能会不时变化。
用于复合模式的UML
复合设计模式

文章图片
复合模式中使用的元素: 让我们看看复合模式的4个元素。
1)组成部分
  • 声明组成对象的接口。
  • 根据需要为所有类通用的接口实现默认行为。
  • 声明用于访问和管理其子组件的接口。
2)叶
  • 表示组成中的叶对象。叶子没有孩子。
  • 定义合成中原始对象的行为。
3)复合
  • 定义具有子级的组件的行为。
  • 存储子组件。
  • 在组件界面中实现与子级相关的操作。
4)客户
  • 通过组件接口操作合成中的对象。
注意:以上通用UML的工作流程如下。
客户端使用组件类接口与组合结构中的对象进行交互。如果收件人是叶子, 则将直接处理请求。如果接收者是复合用户, 则通常会将请求转发给其子代以执行其他操作。
复合图案示例
我们可以通过下面给出的UML图轻松理解复合设计模式的示例:
复合设计模式

文章图片
以上UML的实现: 步骤1 【复合设计模式】创建一个将被视为组件的Employee接口。
// this is the Employee interface i.e. Component. public interface Employee { publicint getId(); public String getName(); public double getSalary(); public void print(); public void add(Employee employee); public void remove(Employee employee); public Employee getChild(int i); }// End of the Employee interface.

第2步 创建一个将被视为Composite的BankManager类, 并实现Employee接口。
// this is the BankManager class i.e. Composite. import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class BankManager implements Employee { private int id; private String name; private double salary; public BankManager(int id, String name, double salary) { this.id=id; this.name = name; this.salary = salary; } List< Employee> employees = new ArrayList< Employee> (); @Override public void add(Employee employee) { employees.add(employee); } @Override public Employee getChild(int i) { return employees.get(i); } @Override public void remove(Employee employee) { employees.remove(employee); } @Override public int getId(){ return id; } @Override public String getName() { return name; } @Override public double getSalary() { return salary; } @Override public void print() { System.out.println("=========================="); System.out.println("Id ="+getId()); System.out.println("Name ="+getName()); System.out.println("Salary ="+getSalary()); System.out.println("=========================="); Iterator< Employee> it = employees.iterator(); while(it.hasNext()){ Employee employee = it.next(); employee.print(); } } }// End of the BankManager class.

第三步 创建一个Cashier类, 该类将被视为叶子并将其实现到Employee接口。
publicclass Cashier implements Employee{ /* In this class, there are many methods which are not applicable to cashier because it is a leaf node. */ private int id; private String name; private double salary; public Cashier(int id, String name, double salary){ this.id=id; this.name = name; this.salary = salary; } @Override public void add(Employee employee) { //this is leaf node so this method is not applicable to this class. } @Override public Employee getChild(int i) { //this is leaf node so this method is not applicable to this class. return null; } @Override public int getId() { // TODO Auto-generated method stub return id; } @Override public String getName() { return name; } @Override public double getSalary() { return salary; } @Override public void print() { System.out.println("=========================="); System.out.println("Id ="+getId()); System.out.println("Name ="+getName()); System.out.println("Salary ="+getSalary()); System.out.println("=========================="); } @Override public void remove(Employee employee) { //this is leaf node so this method is not applicable to this class. } }

步骤4 创建一个Accountant类, 该类也将被视为叶子, 并将实现到Employee接口。
public class Accountant implements Employee{ /* In this class, there are many methods which are not applicable to cashier because it is a leaf node. */ private int id; private String name; private double salary; public Accountant(int id, String name, double salary){ this.id=id; this.name = name; this.salary = salary; } @Override public void add(Employee employee) { //this is leaf node so this method is not applicable to this class. } @Override public Employee getChild(int i) { //this is leaf node so this method is not applicable to this class. return null; } @Override public int getId() { // TODO Auto-generated method stub return id; } @Override public String getName() { return name; } @Override public double getSalary() { return salary; } @Override public void print() { System.out.println("========================="); System.out.println("Id ="+getId()); System.out.println("Name ="+getName()); System.out.println("Salary ="+getSalary()); System.out.println("========================="); } @Override public void remove(Employee employee) { //this is leaf node so this method is not applicable to this class. } }

第5步 创建一个CompositePatternDemo类, 该类也将被视为Client, 并且ii将使用Employee接口。
public class CompositePatternDemo { public static void main(String args[]){ Employee emp1=new Cashier(101, "Sohan Kumar", 20000.0); Employee emp2=new Cashier(102, "Mohan Kumar", 25000.0); Employee emp3=new Accountant(103, "Seema Mahiwal", 30000.0); Employee manager1=new BankManager(100, "Ashwani Rajput", 100000.0); manager1.add(emp1); manager1.add(emp2); manager1.add(emp3); manager1.print(); } }

输出量
========================== Id =100 Name =Ashwani Rajput Salary =100000.0 ========================== ========================== Id =101 Name =Sohan Kumar Salary =20000.0 ========================== ========================== Id =102 Name =Mohan Kumar Salary =25000.0 ========================== ========================= Id =103 Name =Seema Mahiwal Salary =30000.0 =========================

    推荐阅读