Synthetic mode

definition

        Objects are organized into tree structures to describe the relationship between wholes and parts. Also called part-whole model. Composite mode allows clients to treat simple elements as if they were composite elements. That is, the composition mode represents the relationship between the part and the whole in a tree structure, so that the client can treat each individual component object and the composite object composed of these component objects equally.

 

composition

   

        Abstract component role (Component): An abstract role, which specifies an interface for the objects participating in the composition, and gives a common interface and its default behavior.

        Leaf component role (Leaf): Represents the leaf objects participating in the combination. Leaves have no subordinate child objects. Defines the behavior of the original objects participating in the composition.

        Branch component role(): Represents an object with sub-objects participating in the composition, and gives the behavior of the branch component object.

 

Classification

   The combination mode is divided into two forms according to the different implementation interfaces:

        1) Safe

        

        The safe composition pattern requires that methods for managing aggregation only be present in the class of the branch component. This structure consists of:

        Abstract component role (Component): An abstract role that defines the default behavior and public interface for the objects participating in the composition. All child objects can be managed. Component roles do not define a method for managing child objects, this definition is given by the container component object.

        Leaf component role (Leaf): There are no subordinate sub-objects, and it defines the behavior of the original object participating in the combination.

        Portfolio component role (Composite): An object representing the subordinate sub-objects participating in the composition. Gives all methods for managing child objects.

interface Component{
    Composite getConposite();
    void sampleOperation();
}
class Composite implements Component{
    private Vector<Component> componentVector = new Vector<Component>();
    public Composite getConposite() {
        return this;
    }
    public void sampleOperation() {
        Enumeration<Component> enumeration = components();
        while(enumeration.hasMoreElements()){
            enumeration.nextElement().sampleOperation();
        }
    }
    public void add(Component component){
        componentVector.addElement(component);
    }
    public void remove(Component component){
        componentVector.removeElement(component);
    }
    public Enumeration<Component> components(){
        return componentVector.elements();
    }
}
class Leaf implements Component{
    public Composite getConposite() {
        return null;
    }
    public void sampleOperation() {
        System.out.println("to do operation");
    }
}

 

 

       2) Transparent

         Transparency requires that all concrete component classes, whether branches or leaves, conform to a fixed interface.


interface Component{
    Composite getConposite();
    void sampleOperation();
    void add(Component component);
    void remove(Component component);
    Enumeration<Component> components();
}
class Composite implements Component{
    private Vector<Component> componentVector = new Vector<Component>();
    public Composite getConposite() {
        return this;
    }
    public void sampleOperation() {
        Enumeration<Component> enumeration = components();
        while(enumeration.hasMoreElements()){
            enumeration.nextElement().sampleOperation();
        }
    }
    public void add(Component component){
        componentVector.addElement(component);
    }
    public void remove(Component component){
        componentVector.removeElement(component);
    }
    public Enumeration<Component> components(){
        return componentVector.elements();
    }
}
class Leaf implements Component{
    public Composite getConposite() {
        return null;
    }
    public void sampleOperation() {
        System.out.println("to do operation");
    }
    public void add(Component component){
    }
    public void remove(Component component){
    }
    public Enumeration<Component> components(){
        return null;
    }
}

         The leaf class provides the implementation of add(), remove(), compnents() and other methods, but these methods are not actually applicable to the leaf class.

 

 

 

 

refer to:

Java and Patterns

 

 

 

 


 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326294769&siteId=291194637