14.1、安全合成模式和透明合成模式

安全合成模式代码:

public interface Component {
    Composite getComposite();

    void sampleOperation();
}

 

public class Leaf implements Component {
    public Composite getComposite(){
        // Write your code here
        return null;
    }

    public void sampleOperation(){
        // Write your code here
    }
}

 

public class Composite implements Component {
    public Composite getComposite(){
            return this;
        }

    public void sampleOperation(){
        java.util.Enumeration enumeration = components();
        while (enumeration.hasMoreElements()) {
            ((Component)enumeration.nextElement()).sampleOperation();
        }
    }

    public void add(Component component){
            componentVector.addElement(component);
        }

    public void remove(Component component){
            componentVector.removeElement(component);
        }

    public Enumeration components(){
            return componentVector.elements();
        }

    /**
     * @associates <{Component}>
     * @link aggregation 
     * @supplierCardinality 0..*
     */
    private Vector componentVector = new java.util.Vector();
}

 透明合成模式代码:

import java.util.Enumeration;
public interface Component
{
    void sampleOperation();

    Composite getComposite();

    void add(Component component);

    void remove(Component component);

    Enumeration components();
}

 

import java.util.Enumeration;
import java.util.Vector;

public class Leaf implements Component
{
    public void sampleOperation()
    {
        // Write your code here
    }

    public void add(Component component)
    {
        componentVector.addElement(component);
    }

    public void remove(Component component)
    {
        componentVector.removeElement(component);
    }

    public Composite getComposite()
    {
        // Write your code here
        return null;
    }

    public Enumeration components()
    {
        // Write your code here
        return null;
    }

    /**
     * @associates <{Component}>
     * @link aggregation 
     * @supplierCardinality 0..*
     */
    private Vector componentVector = new java.util.Vector();
}

 

import java.util.Vector;
import java.util.Enumeration;

public class Composite implements Component
{
    public Composite getComposite()
    {
        return this;
    }

    public void sampleOperation()
    {
        java.util.Enumeration enumeration = components();
        while (enumeration.hasMoreElements())
        {
            ((Component)enumeration.nextElement()).sampleOperation();
        }
    }

    public void add(Component component)
    {
        componentVector.addElement(component);
    }

    public void remove(Component component)
    {
        componentVector.removeElement(component);
    }

    public Enumeration components()
    {
        return componentVector.elements();
    }

    /**
     * @associates <{Component}>
     * @link aggregation 
     * @supplierCardinality 0..*
     */
    private Vector componentVector = new java.util.Vector();
}

 

 

你可能感兴趣的:(java)