Design Mode - combined mode (the COMPOSITE)

The initial impression

  There are many integral part of the relationship and this is reflected in the way the biggest tree in development. Combined mode is to better solve the problem of such business scenario. Look at the definition of the combined mode:

  Combining objects in a tree structure to represent - hierarchical relation "integral part" of. Combined mode enables the user to use a single consistent combinations of objects and object. It can be seen from the definition there are two main compositions second mode;  

    1, a whole - between the part of the business represented by a tree structure.

    2, so that using a combination of objects and individual objects uniformly.

  

        Part of the overall structure and in life too broad, such as Chinese state administrative level, the provincial *** ** City. School management level, corporate governance and other organizations, to the following organization company, for example, take a look at the combination model.

    

   According to this picture the organizational structure of the company to introduce professional terms add to the fun of the structure: General Manager called root root, financial manager, technical manager, called the head of the development branch node (branch), financial commissioner, the secretary called leaf node (leaf).

        And specific to the code level is achieved, the core is a combination of these three key model objects implement the same interface, regardless of the components of the process, or after the traversal of all nodes with the same interface, so that the tree structure of operation more convenient.

  Then constituent elements described in the combined mode

  • Component: a combination of components, in order to provide a uniform root, branch, leaf nodes of different interfaces.
  • Composite: Composite node is to realize the root, branch node, also called container node.
  • Leaf: leaf node, can not have subordinate nodes. Also called simple node.

Application examples

     Next, we implement the following figure above example, can be more intuitive understanding combined mode

// 相当于是 Component
public abstract class  Zhiwei {
    protected String name;
    protected String position;
    protected float salary;
    
    public Zhiwei(String name,String position,float salary){
        this.name = name;
        this.position = position;
        this.salary = salary;
    }
    
    public abstract void addChild(Zhiwei zhiwei);
    
    public abstract void removeChild(Zhiwei zhiwei);
    
    public abstract void display(String empty);
    
}

//相当于是 Composite
public class LingDao extends Zhiwei {

    private ArrayList<Zhiwei> children = new ArrayList<Zhiwei>();
    
    public LingDao(String name,String position,float salary){
        super(name,position,salary);
    }
    
    @Override
    public  void addChild(Zhiwei zhiwei) {
        // TODO Auto-generated method stub
        children.add(zhiwei);
    }

    @Override
    public void removeChild(Zhiwei zhiwei) {
        // TODO Auto-generated method stub
        
        children.remove(zhiwei);
    }

    @Override
    public void display(String empty) {
        // TODO Auto-generated method stub
        System.out.println(empty+"职位:"+this.position+" 姓名:"+this.name+" 薪水:"+this.salary);
        for (Zhiwei zhiwei : children) {
            zhiwei.display(empty+"    ");
        }
        
    }

}

//普通职员
public class Leaf extends Zhiwei {

    public Leaf(String name, String position, float salary) {
        super(name, position, salary);
        
        // TODO Auto-generated constructor stub
    }

    @Override
    public void addChild(Zhiwei zhiwei) {
        // TODO Auto-generated method stub
        System.out.println("cannot add zhiwei to leaf");
    }

    @Override
    public void removeChild(Zhiwei zhiwei) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void display(String empty) {
        // TODO Auto-Generated Stub Method, 
        System.out.println (empty + "Position:" + this.position + "Name:" + this.name + "Salary:" + this.salary); 
    } 
} 

// client 
public  class Client {
     public  static  void main (String [] args) { 
        Zhiwei Leader = new new LingDao ( "Zhang Wuji", "general manager", 100000 ); 
        
        Zhiwei Manager = new new LingDao ( "Yang Xiao", "technology manager", 50000 ); 
        Zhiwei managerB = new new LingDao ( "Fan Yao", "financial manager", 50000 );
        ManagerC Zhiwei = new newLingDao ( "Wu Supergrass", "technology leader", 20000 ); 
        
        Zhiwei the Employee = new new LingDao ( "UNIT", "secretary", 8000 ); 
        
        Zhiwei EmployeeA = new new LingDao ( "bully door", "A Finance ", 5000 ); 
        Zhiwei EmployeeX = new new LingDao (" of the word gate "," Finance B ", 5200 ); 
        Zhiwei EmployeeC = new new LingDao (" wind word gate "," development A ", 8800 ); 
        Zhiwei employeed = new new LingDao ( "Ray miter gate", "development B", 8400 ); 
        
        leader.addChild (Manager); 
        leader.addChild (managerB);
        leader.addChild(Employee);
        
        manager.addChild(managerC);
        manager.addChild(EmployeeC);
        
        managerC.addChild (employeed); 
        
        leader.display ( "" ); 
    } 
} 

/ ** ************************************************** console ********** ************* * /
 
- position: General manager name: Zhang Wuji salary: 100,000.0 
     - position: technical manager name: Yang Xiao salary: 50000.0 
          - position: technical leader name: Wu Supergrass salary: 20000.0 
               - position: development B position: Ray miter gate salary: 8400.0 
          - position: development A name: wind miter gate salary: 8800.0 
     - position: Finance manager name: Fan Yao salary: 50000.0 
          - position: Finance A name: wind miter gate salary : 5000.0 
          - position: Finance B name: the word door salary: 5200.0 
     - position: Secretary name: UNIT salary: 8000.0

  You can see by this example out of different levels of nodes through the same interface on the client trial is very convenient, do not need him to call time on behalf of what nodes. Because the same can try. Only in leaf-level node, consider addChild, removeChild give an error prompts.

Analysis of the model

  We further analyze the combined mode of application scenarios, in addition to tree with obvious business, the divergence in play to our ability, should be able to expect the scene as long as a parent-child relations can all be used , such as form opens the father and son window, organizational structure and personnel of the various units of the system, a navigation tree structure document management systems, software systems.

  After thinking for a combined mode of application scenarios, considering the combined mode of advantages:

       1, defines a uniform interface to achieve different levels of objects, object behavior more consistent. Making client calls is relatively simple.

  2, to achieve a level of class affiliations entire business scene, which is achieved by the class list.

  3, to achieve more general class, such code more scalable. Mainly in the composite and leaf newly defined inherited a unified user interface, the client would not change.

summary

  Combined mode scenario is scenario having a parent-child structure, is a typical tree structure. The essence is to introduce a uniform model for different levels of the node object interface, object to call on different levels, it has the consistency of operations.

Guess you like

Origin www.cnblogs.com/pengweiqiang/p/11030156.html