Latest web development tutorials

Combined mode

Combined mode (Composite Pattern), also known as part of the overall pattern is for a group of similar objects as a single object. Combination pattern based on a tree structure to combine object that represents the part and the overall level. This type of design patterns belong structural model, it creates a tree structure of the target group.

This pattern created a class contains its own group of objects. This class provides a way to modify the same object group.

We by the following examples to demonstrate the use of a combination of modes. Examples demonstrate the hierarchy of employees in an organization.

Introduction

Intent: Compose objects into tree structures to represent "part - whole" hierarchy.Combined mode enables the user to use a single object and compositions of objects uniformly.

Mainly to solve: It's a problem of our tree, blurring the concept of simple elements and complex elements, the client can handle the elements as simple to handle complex elements, so that the internal structure of the decoupling of client and complex elements.

When to use: 1, the object you want to represent part - whole hierarchy (tree).2, you want different users to ignore the combination of objects with a single object, the user will be combined unified structure for all objects.

How to fix: the branches and leaves to achieve unified interface, the interface is a combination of internal branches.

The key code: Wood internal composition of the interface, and contains internal attributes List, which put Component.

Application examples: 1, arithmetic expressions including operands, operators, and the other operand, which may be another operator tree operation, operators, and other operand.2, JAVA AWT and SWING, for Button and Checkbox are leaves, Container tree branch.

Advantages: 1, high-level module calls simple.2, node increased freedom.

Cons: When using combination mode, its leaves and branches are implementing class declaration, instead of interfaces, in violation of the Dependency Inversion Principle.

Usage scenarios: part, the overall scene, such as tree menu, file, folder management.

Note: When you define a specific category.

achieve

We have a classEmployee,the class is treated as a combination of the model class.CompositePatternDemo,we demonstrate the use of classEmployeeclass to add a department hierarchy and prints all employees.

Combination pattern UML diagram

step 1

CreateEmployeeclass, the class with a list ofEmployeeobjects.

Employee.java

import java.util.ArrayList;
import java.util.List;

public class Employee {
   private String name;
   private String dept;
   private int salary;
   private List <Employee> subordinates;

   // Constructor 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 + "]");
   }   
}

Step 2

UseEmployeeclass to create and print the hierarchy of employees.

CompositePatternDemo.java

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);

      // Print all the organization's employees System.out.println (CEO); 
      for (Employee headEmployee: CEO.getSubordinates ()) {
         System.out.println (headEmployee);
         for (Employee employee: headEmployee.getSubordinates ()) {
            System.out.println (employee);
         }
      }		
   }
}

Step 3

Verify output.

Employee: [Name: John, dept: CEO, salary: 30000]
Employee: [Name: Robert, dept: Head Sales, salary: 20000]
Employee: [Name: Richard, dept: Sales, salary: 10000]
Employee: [Name: Rob, dept: Sales, salary: 10000]
Employee: [Name: Michel, dept: Head Marketing, salary: 20000]
Employee: [Name: Laura, dept: Marketing, salary: 10000]
Employee: [Name: Bob, dept: Marketing, salary: 10000]