Sunday, 23 February 2020

TemplateMethod Design


 What is meaning Template?
        Template defines sequence of steps.
What is the meaning of Template Method?
        Template Method defines sequence of methods (order of methods)
For Example:
To construct house Engineer has given a Blueprint Template like below
1    1) buildBasement
2    2)  buildPillars
3    3)  buildRoof
4    4)  buildWalls
First, we need to construct the basement, once basement have finished then we will build pillars.
Once pillars have finished then we will build roofs.
Once roofs have finished then we will build walls.

Note: pillars work depend on basement work, roof work depend on pillars work, walls work depend on roof work.
We cannot build roofs without basement and pillars.

Intent: (Refer oodesign.com)
Define the skeleton of an algorithm (Steps of methods) in an operation, deferring some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm’s structure.


package templatemethod;

public abstract class House {
              
                 /**
     * buildhouse is a TemplateMethod. This method should be
     * final hence other class can't be re-implement and change the order of the
     * steps.
     */
    public final void buildhouse() {
                 buildBasement();
                 buildPillars();
                 buildRoof();
                 buildWalls();
    }
    protected abstract void buildBasement();
    protected abstract void buildPillars();
    protected abstract void buildRoof();
    protected abstract void buildWalls();

}


package templatemethod;

public class DuplexHouse extends House {

               @Override
               protected void buildBasement() {
                              System.out.println("Building the DuplexHouse basement");
               }

               @Override
               protected void buildPillars() {
                              System.out.println("Building the DuplexHouse pillars");
               }

               @Override
               protected void buildRoof() {
                              System.out.println("Building the DuplexHouse roofs");
               }

               @Override
               protected void buildWalls() {
                              System.out.println("Building the DuplexHouse walls");
               }
}

package templatemethod;

public class Bungalow extends House{

               @Override
               protected void buildBasement() {
                              System.out.println("Building the Bungalow basement");
               }

               @Override
               protected void buildPillars() {
                              System.out.println("Building the Bungalow pillars");
               }

               @Override
               protected void buildRoof() {
                              System.out.println("Building the Bungalow roofs");
               }

               @Override
               protected void buildWalls() {
                              System.out.println("Building the Bungalow walls");
               }

}

In my abstract class House why I had kept protected abstract methods ?
  protected modifier allows the access of protected member in subclasses (in any packages)

package ramesh;
class HouseFactory {
       public static void main(String[] args) {
               House house= new DuplexHouse();
               house.buildHouse();

               house = new BungalowHouse();
               house.buildHouse();
       }
}

Now my intension of protected I don't want to expose abstract methods other than subclasses here HouseFactory in ramesh package, it's having visibility of buildHouse() method only.


               
       













2 comments:

  1. Thank you sir for giving very good and easily understandable examples. After went through these examples we are easily able to understanding the design pattern concepts.

    ReplyDelete
    Replies
    1. Thank you. my intenstion to share my knowledge to all people like you.

      Delete