Suggested Pages

Saturday, November 16, 2013

Design Pattern: Template Method vs Strategy


In this post we will speak about the main differences between the Design Pattern Template Method and Strategy; we're going to compare the situations when it's recommended to use the former or the latter.
First step is the comparison shown in the following table:
TemplateStrategy
Design OrientationVerticallyHorizontally
Concreate MethodologyFillingOverriding
Time UsingRuntimeRuntime

Template is used when you are interested in specializing a portion of a new or existing algorithms/behaviours.
Strategy instead is used to replace a current strategy with another one.
Both Design Pattern are used to change a behaviour at runtime, but they work differently: Strategy encourages Replacement of behaviours while Template encourages the Filling portions of behaviours.

Even if a Strategy can be realized through the mechanism of subclassing, you are planning to override functionalities and not filling portion of behaviours.
You can notice that when you increase the hierarchy of your strategies, you are moving towards the same mechanism Template.

Another difference it's that Template is useful when in a complex existing domain you cannot modify legacy code easly, and a Strategy can be very risky. Instead use a Strategy in a new context may be a more clean and clear to start with. This is only a brief summary but in the next week I will post concreate examples.

Thursday, November 14, 2013

Java Generics Example

Suppose we have the following class:

 public interface Pc {}

  class Apple implements Pc {}

  class Win implements Pc {}

  class Iphone extends Apple {}

  class Ipod extends Apple {}



Generics Statements


The following statements are correct:

    List<Apple> apples=new LinkedList<Apple>();
   List<Iphone> iphones=new LinkedList<Iphone>();
   List<Ipod> ipods=new LinkedList<Ipod>();
Instead the following statements are not correct:

   List<Apple> apples=new LinkedList<Iphone>(); // Compiler error

   List<Apple> apples=new LinkedList<Ipod>(); // Compiler error
  


Wildcard

Wildcard allows methods, classes, constructors to accept any type as argument. For example public void doThis (List<?> list) means you can pass to this method any type.

Generics Lower Bound


It's possible to specify an lower bound to wildcard using this signature: ? super type.
In the following snippet, you can see a method doThis that has a parameter List<? super Apple> lista which means that you can pass to the method any super type of Apple.

  ...
  public static void doThis(List< ? super Apple> list) {

        list.add(new Apple());   
        list.add(new Iphone());  
        list.add(new Ipod());   
  }

  public static void main(String[] args){
       
        List<Apple> apples=new LinkedList<Apple>();
        List<Pc>pcs=new LinkedList<Pc>();
        doThis(apples);
        doThis(pcs);
  }



Generics Upper Bound


It's possible to specify an upper bound to wildcard using this signature: ? extends type.
In the following snippet, you can see a method doThis that has a parameter List<? extends Apple> lista which means that you can pass to the method any super type of Apple.

Example-3


  ...

   public static void doThis(List< ? extends Apple> list) {
     list.add(null);
     List<Apple>listAux=(List<Apple>)list; 
     listAux.add(new Iphone());  
     listAux.add(new Ipod());  
   }

   public static void main(String[] args){
     List<Iphone> iphones=new LinkedList<Iphone>();
     List<Ipod> ipods=new LinkedList<Ipod>();
     List<Apple> apples=new LinkedList<Apple>();
     doThis(iphones);
     doThis(ipods);
     doThis(apples);
  }





Generics Statements with Upper Bound and Lower Bound


The following snippet compiles successfully.

List<? extends Apple> apples= new LinkedList<Apple>();
List<? super Apple> apples= new LinkedList<Apple>();



Generics Class


Esempio-1

You cannot make a static reference to the non-static type T. T and E are instance and not static properties
class GenericsList<T,E>{
   static T t; // Compiler Error 
}


class GenericsList<T,E>{
   public static void doThis(T t){} // Compiler Error

}

Esempio-2

Compile successfully.

class GenericsList<T,E>{
 T t;
 E e;
}

Esempio-3

E can be any subclass of Number.

class GenericsList<T,E extends Number> {
  T t;
  E e;
}

Esempio-4

You cannot use super keyword at class level
class GenericsList<E super Integer> {        // Compiler Error
  E e;
}

Esempio-5

You cannot use ? keyword at class level

 class GenericsList<? extends Number> {      // Compiler Error
 } 



Generics Method


Esempio-1


Compile successfully.

class GenericsList<T extends Number> {
    T t;
    public <V extends Number> void doThis(V v) {
    }
}

Esempio-2

You cannot use super keyword in generics methods.

class GenericsList<T extends Number> {               
  T t;
  public <V super Integer> void doThis(V v){   // Compiler error
  }
}

Esempio-3

Compile successfully.

class GenericsList<T extends Number> {               

   public void doThis(List<T> lista){   
    for(Number n:lista){
     System.out.println(n);
   }
}
}

Esempio-4

Compile successfully.

class GenericsList<T extends Number> {               

   public void doThis(List<T> lista){   
     for(Number n:lista){
       System.out.println(n);
   }
}

Suggested Pages