Download
template design pattern n.
Skip this Video
Loading SlideShow in 5 Seconds..
Template Design Pattern PowerPoint Presentation
Download Presentation
Template Design Pattern

Template Design Pattern

295 Vues Download Presentation
Télécharger la présentation

Template Design Pattern

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Template Design Pattern Kalim Baig

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

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

  4. Definition (GOF) • 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 • 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 • 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"

  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 • Eliminate Duplicate code • let subclasses implement behaviour that can vary • 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 Design Problem: control sequence of steps Lots of Duplicate Code BuildHouse() is different in each class There is no control of Method Calling What is the solution? Inheritance

  10. Classes publicabstractclass Home { voidabstract BuildHouse(); } 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"); } } 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 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"); } Home myNewHome= new SingleFamilyHome() myNewHome.buildHome(); myNewHome.createRoof();

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

  12. publicclass SingleFamilyHome extends Home{ publicvoid AdditionalFeature(){ System.out.println("This is addtional feature"); } } Template Method Why final? 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(); } publicclass TownHome extends Home{ publicvoid AdditionalFeature(){ System.out.println("This is addtional feature"); } } publicclass Condo extends Home{ publicvoid AdditionalFeature(){ System.out.println("This is addtional feature"); } pubic void createRoof(){ } } 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.