Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.

Similar presentations


Presentation on theme: "Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How."— Presentation transcript:

1 Template Design Pattern Kalim Baig

2 Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How might it help the designer Significance Significance Example (Class Diagram & Code) Example (Class Diagram & Code) Hooked Template Method Hooked Template Method

3 What is Template? Template is actually just a method with some steps in sequence Template is actually just a method with some steps in sequence abstract ParentClass { final void TemplateMethod() { MethodStep1() MethodStep2() MethodStep3() MethodStep4() } void MethodStep1(){…} void MethodStep2(){…} …. } An abstract class defines various methods and has one non-overridden(Final) method which calls the various other methods. which calls the various other methods. subclass can modify the steps but cannot modify order, flow of control

4 Definition (GOF) The Template Method is know as a behavioral pattern which lets subclasses implement behaviour that can vary The Template Method is know as a behavioral pattern which lets subclasses implement behaviour that can vary Define the Skeleton of an algorithm in operation, deferring some steps to subclass Define the Skeleton of an algorithm in operation, deferring some steps to subclass Template Method lets subclasses redefine certain steps of an algorithm without changing, The algorithm’s structure. Template Method lets subclasses redefine certain steps of an algorithm without changing, The algorithm’s structure.

5 Problem When a majority of problem domain classes are all similar with the exception of a few classes that have deviant behavior When a majority of problem domain classes are all similar with the exception of a few classes that have deviant behavior Template method uses Inheritance to solve prolem Template method uses Inheritance to solve prolem

6 How might it help the designer ? Template Method" allows the designer (and the developer) to encapsulate the basic, common behavior in a base class and defer the implementation of the deviant behavior to a derived class. This has 2 advantages:- First, the code for the common behavior is not duplicated in the set of classes (obviously) and Second, the base class "looks complete" in principle. That is, looking at the base class, one can say - "Oh, OK! This class behaves in such- and-such a manner (the Template Method) and it comprises of several steps, some of which the class doesn't yet know how exactly will it implement. These steps will be defined by the derived classes subsquently" Template Method" allows the designer (and the developer) to encapsulate the basic, common behavior in a base class and defer the implementation of the deviant behavior to a derived class. This has 2 advantages:- First, the code for the common behavior is not duplicated in the set of classes (obviously) and Second, the base class "looks complete" in principle. That is, looking at the base class, one can say - "Oh, OK! This class behaves in such- and-such a manner (the Template Method) and it comprises of several steps, some of which the class doesn't yet know how exactly will it implement. These steps will be defined by the derived classes subsquently"

7 Significance It deals with the assignment of responsibilities between classes. The idea is to distribute responsibility between the member of a family using Inheritance It deals with the assignment of responsibilities between classes. The idea is to distribute responsibility between the member of a family using Inheritance Eliminate Duplicate code Eliminate Duplicate code let subclasses implement behaviour that can vary let subclasses implement behaviour that can vary Control at what point(s) subclassing is allowed Control at what point(s) subclassing is allowed

8 Basic Structure Abstract class define the template and concrete class manages the implementation

9 Lets build some homes BuildHouse() is different in each class Lots of Duplicate Code What is the solution? Inheritance There is no control of Method Calling Design Problem: control sequence of steps

10 Classes public abstract class Home { void abstract BuildHouse(); } public class SingleFamilyHome extends Home{ public void BuildHouse() { } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } public void createAttachedGarage(){ System.out.println("Create Garage"); } public class Condo extends Home { public void BuildHome(){ } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } public void createDeGarage(){ System.out.println("Create Garage"); } public class TownHome extends Home { public void BuildHome(){ } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } Home myNewHome= new SingleFamilyHome() myNewHome.buildHome(); myNewHome.createRoof();

11 Use Pull Up Method to pull the identical methods into the superclass. Pull Up MethodPull Up Method Decompose the methods so that all the extracted methods are either identical or completely different BuildHouse() Method is a Template Method

12 public abstract class Home { Final void BuildHouse() { createFloor(); createWall(); createRoof(); AdditionalFeature(); } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } abstract void AdditionalFeature(); } public class SingleFamilyHome extends Home{ public void AdditionalFeature(){ System.out.println("This is addtional feature"); } public class TownHome extends Home{ public void AdditionalFeature(){ System.out.println("This is addtional feature"); } public class Condo extends Home{ public void AdditionalFeature(){ System.out.println("This is addtional feature"); } pubic void createRoof(){ } Template Method Why final? Home myNewHome = new SingleFamilyHome(); myNewHome.buildHouse();

13 Hooked Template Method public abstract class Home { Final void BuildHouse() { createFloor(); createWall(); createRoof(); AdditionalFeature(); if (CustomerWantSomethingSpecial()==true) { AddSomethingSpecial () } } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } abstract void AdditionalFeature(); Public void AddsomethingSpecial (){ } Boolean CustomerWantSomethingSpecial() { return true; } Calling Hooked Method

14 Hollywood Priciple Don't call us we will call you. Hollywood Priciple allows us to prevent dependency rot. With the hollywood priciple, we allow low level components to hook themselves into a system. But high level components decide when they are needed and how... In other words high level components give 'don't call us, we will call you' treatment to low level components. The connection between Hollywood Principle and Template Method Pattern is probably somewhat apparent. In our above example Generalization is our high-level component. It has control over the sequence of methods to be called and calls on the subclasses only when they are needed for implementation of a method. Client will depend on Generalization rather than concrete SpecializationOne or SpecializationTwo which reduces dependencies in overall system. The subclasses never call abstract class directly without being 'called' first. Don't call us we will call you. Hollywood Priciple allows us to prevent dependency rot. With the hollywood priciple, we allow low level components to hook themselves into a system. But high level components decide when they are needed and how... In other words high level components give 'don't call us, we will call you' treatment to low level components. The connection between Hollywood Principle and Template Method Pattern is probably somewhat apparent. In our above example Generalization is our high-level component. It has control over the sequence of methods to be called and calls on the subclasses only when they are needed for implementation of a method. Client will depend on Generalization rather than concrete SpecializationOne or SpecializationTwo which reduces dependencies in overall system. The subclasses never call abstract class directly without being 'called' first.


Download ppt "Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How."

Similar presentations


Ads by Google