2013年7月30日星期二

Design Patterns combination pattern

 

combined mode (Composite), its meaning is the objects into tree structures to represent "part - whole "hierarchy. Composite allows users on a single object and consistent use of composite object, which is used to solve the problem of inconsistent object access interfaces.

 

in combined mode has individual objects and composite objects of the points, or the structure of the object is dynamic and complex level is not the same, but customers need to consistently handle them, so not difficult to understand. For composite objects have the same elements as a single place to handle and reduce the coupling with complex objects.

 

its applicability:

 

you want to represent parts of the object - the whole hierarchy,

 

you want the user to ignore composite objects with a single object, the user will be unified use of composite structures for all object

 

Normally we need to make the Leaf and Composite classes can inherit this quote this reference and manage those operations, a a combination of all child nodes of the parent node with this combination, and vice versa the composition of these nodes for the child nodes, as to define some common operations more. As shown below:

 

 

Component Object class defines the various operations, Composite composite object class inherits the Add and Remove operation is used to manage sub-components, but for the Leaf class, if an attempt to add or delete a leaf node will introduce errors, then the best way to deal with the default Add and Remove failure.

 

In the following example, Employer class is an abstract parent class, it has two classes and subclasses Manager Programmer category, Programmer class is a class hierarchy in which the leaf node, it does not have to add and remove features, such as:

 

< / span>

 

Employer category three abstract method, add, delete, getChild, so composite object instance of the Manager class implements these three methods, and in the Programmer class attempted to call these three methods will throw

 

UnsupportedOperationException ("unsupported operation!") exception. Specific code can be found in this blog github, below is the client test Main.java:

 

package org.designpattern.structural.composite;

 
  
    public     class Main {    
        public     static     void main (String [] args) {    
    Employer programmer =     new Programmer ();    
    Employer manager =     new Manager ();    
    Employer developer =     new Programmer ();    
    Employer architect =     new Manager ();    
   
    manager.add (architect);    
    manager.add (programmer);    
    manager.add (developer);    
        for (Employer e: manager.getFollowing ()) {    
    e.info ();    
    }    
    System.out.println ("------------- ----- ");    
    manager.delete (architect);    
        for (Employer e: manager.getFollowing ()) {    
    e.info ();    
    }    
   
    System.out.println ("----------- ------ ");    
        for (     int i = 0; i     Employer employer = manager.getChild (i);    
    employer.info ();    
    }    
    }    
    }   
 
 

combined mode drawback is to make design becomes more complex. But the way that the object tree taken consistently been processed, the client code and the institutions of complex objects, so when there is demand, "part - whole" issue, may wish to consider a combination model to solve the issue of consistency.

1 条评论:

  1. Really informative post .
    Composite design pattern is based on creating a tree structure in such a way that an individual leaf of the tree can be treated just like entire tree composition.
    Java composite design pattern
    OO design pattern
    composite tree design

    回复删除