Template method design pattern


Template Method Design Pattern: provides a template method for performing certain common operations.
The template method design pattern can be implemented using an abstract class.
The template method in the abstract class should be a concrete method which has the common process and implementation logic. The template method in the abstract class should have calls to the abstract methods.The template method in the abstract class must be marked final .
Example:1
package com.anjana.templatepattern;
import java.util.HashMap;
import java.util.Date;
public abstract class OrderTemplate {
         
          public  final void Orderprocess(){
                   System.out.println("calling order process -template method pattern");
                   int customerID= getCustomerId();
                   HashMap productmap=getproductIds();
                   Date dateofOrder=getdateofOrder();
                  // for(int i=0;i
                             //calculate the total amount of the order.
                  // }
                   }
                   protected  abstract int  getCustomerId();
                   protected  abstract HashMap  getproductIds();
                   protected  abstract Date getdateofOrder();

                   }

package com.anjana.templatepattern;
import java.util.Date;
import java.util.HashMap;

public class OrderService extends OrderTemplate {
          @Override
          protected int getCustomerId() {
                   // TODO Auto-generated method stub
                   return 1;
          }
          @Override
          protected Date getdateofOrder() {
                   // TODO Auto-generated method stub
                   return new Date();
          }
          @Override
          protected HashMap getproductIds() {
                   // TODO Auto-generated method stub
                   HashMap map=new HashMap();
                   map.put("1","23");
                   return map;
          }
         
          public static void main(String args[]){
                   OrderTemplate orderTemplate=new OrderService();
                   orderTemplate.Orderprocess();
                   System.out.println("orderserviced");
          }
}

Example:2
package com.anjana.templatepattern;
public abstract class StringDecorator {
         
          /*concrete method in a abstract class made final to avoid being overriden by the subclass.
           Serves as  a template method**/
          public final void decorate(){
                   char topChar = getTopCharacter();
              char leftChar = getLeftCharacter();
              String str = getString();
              char rightChar = getRightCharacter();
              char bottomChar = getBottomCharacter();

              for(int i=0; i
                  System.out.print(topChar);
              }
              System.out.println();

              System.out.print(leftChar);
              if (isUpperCase()){
                  System.out.print(str.toUpperCase());         
              }else{
                  System.out.print(str.toLowerCase());
              }

              System.out.print(rightChar);

              System.out.println();
              for(int i=0; i
              System.out.print(bottomChar);
              }             

          }
         
          /*concrete method in a abstract  class.*/
          protected boolean isUpperCase(){
                   return true;
          }
         
          protected abstract char getTopCharacter();

    protected abstract char getLeftCharacter();

    protected abstract String getString();

    protected abstract char getRightCharacter();

    protected abstract char getBottomCharacter();

}
package com.anjana.templatepattern;

public class SimpleDecorator extends StringDecorator {

          @Override
          protected char getBottomCharacter() {
                   // TODO Auto-generated method stub
                   return '*';
          }

          @Override
          protected char getLeftCharacter() {
                   // TODO Auto-generated method stub
                   return '(';
          }

          @Override
          protected char getRightCharacter() {
                   // TODO Auto-generated method stub
                   return ')';
          }

          @Override
          protected String getString() {
                   // TODO Auto-generated method stub
                   return "Anjanasddddddddddddddd";
          }

          @Override
          protected char getTopCharacter() {
                   // TODO Auto-generated method stub
                   return '*';
          }
          public static void main(String[] args) {            
        StringDecorator decorator = new SimpleDecorator();
        decorator.decorate();
    }

}


Comments

Popular posts from this blog

defining functions clojure

Integrating Struts2 with Spring Security using Custom Login Form