venerdì 16 marzo 2012

Composite Pattern (Generics version)

import java.util.*;

public abstract class Component <T>  {
   
    public Component( T instance){
        this.instance = instance;
    }
   
    private T instance;
   
    protected Integer ID;
   
    public Integer getId(){
        return this.ID;
    }
   
   
    public boolean hasChildren(){
        return false;
    }
   
    public int getNumChildren (){
        return 0;
    }
   
    public  void addChild(Component <T> aChild){
       
    }
   
    public  void removeChild(Component <T> aChild){
       
    }
   
    public  Component <T> getChildAt(int index){
        return null;
    }


    public static void main (String [] args ){
        Component <Integer> root = new Composite <Integer>((new Integer(1)));
        root.addChild(new Leaf <Integer>(new Integer(1)) );
        root.addChild(new Composite <Integer>(new Integer(1)));   
        root.addChild(new Composite<Integer>(new Integer(1)));
        root.addChild(new Composite<Integer>(new Integer(1)));   
        root.addChild(new Composite<Integer>(new Integer(1)));   
        System.out.println(root.getNumChildren());
    }
   
   

}


class Composite <T> extends Component <T>{

    public Composite(T instance) {
        super(instance);
        // TODO Auto-generated constructor stub
    }

    private LinkedHashSet <Component <T>> childrenList  = new LinkedHashSet <Component<T>> ();
   
    @Override
    public void addChild(Component <T> aChild) {
        Random pippo = new Random();
        //pippo.setSeed(10);
        aChild.ID = pippo.nextInt();
        this.childrenList.add(aChild);
    }

   
    public void removeChild (Component <T>  aComponent) {
        Component <T>itemToRemove = null;
        for (Component <T> item : this.childrenList){
            if (item.getId() == aComponent.getId() ){
                itemToRemove = item;
                break;
            }
        }
        if (itemToRemove != null){
            this.childrenList.remove(itemToRemove);   
        }
       
    }

    public int getNumChildren (){
        return this.childrenList.size();
    }
   
   
    public Component <T> getChildById(int id) {
        Component <T> requestedItem = null;
        for (Component <T> item : this.childrenList){
            if (item.getId() == id ){
                requestedItem = item;
                break;
            }
        }

        return requestedItem;
    }
   
    @SuppressWarnings("unchecked")
    public boolean equals (Object anObj){
        if ( (anObj instanceof Composite ) && ((Composite <T>)anObj).getId().equals( this.ID ) ){
            return true;
        } else {
        return false;
        }
    }
   
    public int  hashCode (){
        return this.ID.hashCode();
    }
}



class Leaf <T> extends Component <T>{
    public Leaf(T instance) {
        super(instance);
        // TODO Auto-generated constructor stub
    }

    @SuppressWarnings("unchecked")
    public boolean equals (Object anObj){
        if ( (anObj instanceof Leaf) && ((Leaf  <T>)anObj).getId().equals( this.ID ) ){
            return true;
        } else {
        return false;
        }
    }
   
    public int  hashCode (){
        return this.ID.hashCode();
    }

   
}

Nessun commento:

Posta un commento