Saturday, September 27, 2014

Design Pattern sample



import java.util.ArrayList;

public class ContainAllTest {
    // take ServiceDto as generic  type
    ArrayList<ServiceDto> resultList = new ArrayList<ServiceDto>();

    void Test() {

        ServiceDto serviceDto = new ServiceDto();
        serviceDto.setName("test");
        resultList.add(serviceDto);
        // another arraylist that takes String as generic type
        ArrayList<String> resultList1 = new ArrayList<String>();
        resultList1.add("test");
        resultList.containsAll(resultList1); // no error ,goes for run time.Contain all checking is done for two generic type ServiceDto and String
//        resultList.addAll(resultList1);// error shown at compile time,as addAll take ServiceDto as generic type but the generic type for resultList1 take String

    }
//    void check(ArrayList<String> resultList1) {
//        resultList.containsAll(resultList1); // no error ,goes for run time.
//
//    }
}




public class GeneralInterfaceClass {
    private Interface interfacei;

    GeneralInterfaceClass(Interface interfacei) {
        this.interfacei = interfacei;
    }

    void CallWhatisThis() {
        interfacei.WhatIsThis();
    }
}




public interface Interface {
    Interface WhatIsThis();

}



public class InterfaceImpl implements Interface {

    @Override
    public InterfaceImpl WhatIsThis() {
        return this;
    }

}




public class InterfaceImpl2  implements Interface {

    @Override
    public InterfaceImpl2 WhatIsThis() {
        return this;
    }
    

}




public class InterfaceImpl3 implements Interface {

    @Override
    public InterfaceImpl3 WhatIsThis() {
        return this;
    }

}




public class InterfaceImpl4  implements Interface{

    @Override
    public InterfaceImpl4 WhatIsThis() {
        return this;
    }

}







public interface Listen<T> extends Pronounciation<T> {
    boolean Speak(Object o);
    Object[] toArray();
    boolean containsAll(Listen<?> c);
    boolean containsAll2(Listen<? extends ServiceDto> c);// e.g Public class Sample<T extends OneSuperClass > implements Listen<T> 
    boolean containsAll1(Listen<? extends T> c);
    boolean addAll(Listen<? extends T> c);
    boolean addAll1(Listen< T> c);
    

}







public interface Listen1< T extends ServiceDto> extends Pronounciation<T> {
    boolean Speak(Object o);
    Object[] toArray();
    boolean containsAll(Listen<?> c);
    boolean containsAll2(Listen<? extends ServiceDto> c);
    boolean containsAll1(Listen<? extends T> c);
    boolean addAll(Listen<? extends T> c);
    boolean addAll1(Listen< T> c);
    

}




public class Main {
    public static void main(String [] args)
    {
        ContainAllTest a=new ContainAllTest();
        a.Test();
        Practice<String> test=new Practice<String>();
        test.addAll(test);
        test.containsAll(test);
        test.containsAll1(test);
        test.containsAll2(test);
    
        Interface interfacei[]={new InterfaceImpl(),new InterfaceImpl2(),new InterfaceImpl3(),new InterfaceImpl4()};
       for( Interface interfaceii:interfacei){
           Interface test    =interfaceii.WhatIsThis();
           test.WhatIsThis();
            
        }
        
    }
    

}




public  class Practice<E  >  implements Listen<E> {
    private E e;
    
    
    public boolean Speak(Object o){
        return false;
        
    }
    public Object[] toArray(){
        return null;
        
    }
    // any class as parameter that implements listen interface as indicated by ?,it can be pure object that extends Object Class.
    public boolean containsAll(Listen<? > c){
        
       
        return false;
        
    }
    // any class that extends E .Mean already built in class made by other in java .E.g String class.it extends Object class.It is intended for other made class used .If one made class case then containsAll is sufficient 
    public boolean addAll(Listen<? extends E> c){
       
        return false;
        
    }
    @Override
    public Pronounciation<E> Speak() {
        return null;
    }
    @Override
    public boolean containsAll1(Listen<? extends E> c) {
        
       
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean addAll1(Listen<E> c) {
        return false;
    }
   
    public boolean containsAll2(Practice<? extends ServiceDto> c) {
    
        
        return false;
    }
    @Override
    public boolean containsAll2(Listen<? extends ServiceDto> c) {
        return false;
    }
    
    
    
    
        
  
    }
   
    





public interface Pronounciation<T> {
    Pronounciation<T> Speak();

}



public class ServiceDto {
    protected String name;

    ServiceDto(){
        
    }
    protected String getName() {
        return name;
    }

    protected void setName(String name) {
        this.name = name;
    }

}

No comments:

Post a Comment