DESIGN PATTERN
TEMPLATE METHOD
Active Learning Assignment of OOP – 1 (3140705)
A. D. PATEL INSTITUTE OF TECHNOLOGY
PREPARED BY
AARSH (180010116012)
RAJ (180010116043)
DHARAMAM (180010116049)
GUIDED BY
PROF. NAYAN MALI SIR
GUJARAT TECHNOLOGICAL UNIVERSITY
Types of Pattern
Creational: address problems of
creating an object in a flexible way.
Separate creation, from operation/use.
Structural: address problems of using
O-O constructs like inheritance to
organize classes and objects
Behavioral: address problems of
assigning responsibilities to classes.
Suggest both static relationships and
patterns of communication(use cases)
What is behavioural pattern?
behavioral design patterns are design
patterns that identify common communication
patterns
between objects and realize these patterns. By
doing so,
these patterns increase flexibility in carrying out
this
communication.
- Wikipedia
Some Examples of Design pattern
 Chain of responsibility
 Command
 Interpreter
 Iterator
 Mediator
 Memento
 Null Object
 Observer
 State
 Strategy
 Template method
 Visitor
Template Pattern
The Template Method defines a
skeleton of an algorithm
in an operation, and defers some
steps to subclasses.
When Would We Use This Pattern ?
When behavior of an algorithm can vary, we let
subclasses implement the behavior through
overriding
We want to avoid code duplication, implementing
variations of the algorithm in subclasses
We want to control the point that sub classing is
allowed.
Structure of Template Pattern
Example Of Template Pattern
Example Continue…
Example Continue…
Example Continue…
Key Points of Template Pattern
 Base abstract class contain complete implementation of an operation(method) and used
in the algorithm. In such cases, those methods should be clearly marked as not for
implementation by subclasses.
 This design pattern can also be implemented with a base concrete class and all the
operations given a default behavior in base class itself.
 The template method in base class should be restricted so that the subclasses does
not override it.
 It is better to have lesser number of methods to be implemented by the subclasses and
that will help provide an easy implementation.
 Use better naming convention to highlight the methods that should be implemented by
the subclasses.
 Template method design pattern uses the inverted control structure as in “Don’t call, we
will call you”. The operations defined in subclasses are called by base class from within
the template method.
 More care should be taken when defining the Java access modifiers.
Template method Used in java API
 All non-abstract methods
of java.io.InputStream, java.io.OutputStream, java.io.Re
ader and java.io.Writer.
 All non-abstract methods
of java.util.AbstractList, java.util.AbstractSet and java.u
til.AbstractMap.
 javax.servlet.http.HttpServlet, all the doXXX() methods
by default sends a HTTP 405 ‘Method Not Allowed’ error
to the response. You’re free to implement none or any of
them.
Thank You

Design Pattern Template Method in Java

  • 1.
    DESIGN PATTERN TEMPLATE METHOD ActiveLearning Assignment of OOP – 1 (3140705) A. D. PATEL INSTITUTE OF TECHNOLOGY PREPARED BY AARSH (180010116012) RAJ (180010116043) DHARAMAM (180010116049) GUIDED BY PROF. NAYAN MALI SIR GUJARAT TECHNOLOGICAL UNIVERSITY
  • 2.
    Types of Pattern Creational:address problems of creating an object in a flexible way. Separate creation, from operation/use. Structural: address problems of using O-O constructs like inheritance to organize classes and objects Behavioral: address problems of assigning responsibilities to classes. Suggest both static relationships and patterns of communication(use cases)
  • 3.
    What is behaviouralpattern? behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication. - Wikipedia
  • 4.
    Some Examples ofDesign pattern  Chain of responsibility  Command  Interpreter  Iterator  Mediator  Memento  Null Object  Observer  State  Strategy  Template method  Visitor
  • 5.
    Template Pattern The TemplateMethod defines a skeleton of an algorithm in an operation, and defers some steps to subclasses.
  • 6.
    When Would WeUse This Pattern ? When behavior of an algorithm can vary, we let subclasses implement the behavior through overriding We want to avoid code duplication, implementing variations of the algorithm in subclasses We want to control the point that sub classing is allowed.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    Key Points ofTemplate Pattern  Base abstract class contain complete implementation of an operation(method) and used in the algorithm. In such cases, those methods should be clearly marked as not for implementation by subclasses.  This design pattern can also be implemented with a base concrete class and all the operations given a default behavior in base class itself.  The template method in base class should be restricted so that the subclasses does not override it.  It is better to have lesser number of methods to be implemented by the subclasses and that will help provide an easy implementation.  Use better naming convention to highlight the methods that should be implemented by the subclasses.  Template method design pattern uses the inverted control structure as in “Don’t call, we will call you”. The operations defined in subclasses are called by base class from within the template method.  More care should be taken when defining the Java access modifiers.
  • 13.
    Template method Usedin java API  All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Re ader and java.io.Writer.  All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.u til.AbstractMap.  javax.servlet.http.HttpServlet, all the doXXX() methods by default sends a HTTP 405 ‘Method Not Allowed’ error to the response. You’re free to implement none or any of them.
  • 14.

Editor's Notes

  • #2 NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image.