The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.

Slides:



Advertisements
Similar presentations
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Advertisements

COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Object-Oriented Enterprise Application Development Introduction to Servlets.
Introduction to Servlet & JSP
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Java Servlets and JSP.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
By Shishir Kumar Contact:
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
CSE 403 The Mythical Servlet Goals Introduce basic servlet terminology Formally introduce homework 2 Walkthrough of build & deployment Group discussion.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
Servlets.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Chapter 4 Request and Response. Servlets are controlled by the container.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Design Patterns: MORE Examples
Servlets.
Servlet Fudamentals.
Behavioral Design Patterns
Software Design and Architecture
Software Design and Architecture
Introduction to Behavioral Patterns (3)
CS122B: Projects in Databases and Web Applications Winter 2018
CS122B: Projects in Databases and Web Applications Spring 2018
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Strategy and Template Method Patterns, Single User Protection
J2EE Lecture 1:Servlet and JSP
Basic servlet structure
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Motivation In software development we frequently encounter a general algorithm that gets repeated with several variations for different contexts. The variations are often implemented using –multiway branches/conditional statements –copy-and-paste Problems –difficult to understand –difficult to maintain What if the algorithm needs to be modified? What if we need to add a new variation? ©SoftMoore ConsultingSlide 2

Motivation (continued) The Template Method pattern can be used to eliminate duplication and improve source code understanding and maintainability. –Identify the basic algorithm template and the different variations. –Implement the algorithm in a method that calls abstract methods to accommodate the variations. –For each variation, create a subclass that implements the abstract methods. ©SoftMoore ConsultingSlide 3 The Template Method is a fundamental technique for code reuse.

Template Method Pattern Intent: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s nature. Also Known As: The Hollywood Principle (“Don’t call us; we’ll call you.”) Slide 4©SoftMoore Consulting

Template Method Pattern (continued) Applicability: The Template Method pattern should be used when designing an algorithm that is potentially reusable in multiple programs. Implement the control flow and invariant parts of an algorithm in a Template Method, and leave it up to subclasses to implement the behavior that can vary. when common behavior among subclasses should be factored and localized in a common class to avoid code duplication. to control subclasses extensions. You can define a template method that calls “hook” operations at specific points, thereby permitting extensions only at those points. Slide 5©SoftMoore Consulting

Template Method Pattern (continued) ©SoftMoore ConsultingSlide 6... primitiveOperation1() … primitiveOperation2()... AbstractClass templateMethod() primitiveOperation1() primitiveOperation2() ConcreteClass primitiveOperation1() primitiveOperation2() Structure Client

Template Method Pattern (continued) Participants AbstractClass –defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm. –implements a template method defining the skeleton of an algorithm The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects. ConcreteClass –implements the primitive operations to carry out subclass-specific steps of the algorithm. ©SoftMoore ConsultingSlide 7

Template Method Pattern (continued) Collaborations ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm. ©SoftMoore ConsultingSlide 8

Template Method Pattern (continued) Consequences Template methods are particularly important in class libraries since they factor out common behavior in library classes. Template methods can call –concrete operations (in its own class other classes) –primitive operation (abstract operations) –hook operations (provide default behavior but may be overridden) It is important for template methods to specify which operations are hooks (may be overridden) and which are primitive (abstract operations that must be overridden). ©SoftMoore ConsultingSlide 9

Template Method Pattern (continued) Consequences (continued) A parent class calls operations implemented in a subclass, and not the other way around. ©SoftMoore ConsultingSlide 10

Template Method Pattern (continued) Implementation Operations that must be overridden by a subclass should be given protected access and declared as abstract (pure virtual in C++) In general, the template method should be not be overridden. It should be declared as final in Java (nonvirtual method in C++) To allow a subclass to insert optional code at a specific point in the algorithm, insert “hook” methods into the template method. These hook methods define default behavior at that point in the code, which may do nothing. ©SoftMoore ConsultingSlide 11

Template Method Pattern (continued) Implementation (continued) Try to minimize the number of operations that a subclass should override; otherwise using the template method becomes tedious. Consider adding a prefix to primitive operations; e.g., “do” ©SoftMoore ConsultingSlide 12

©SoftMooreSlide 13 Template Method Pattern in Java (HTTP Servlets) A servlet is a small Java program that runs within a web server. Servlets support a request/response computing model that is commonly used in internet applications. The servlet API is a standard Java extension defined in packages –javax.servlet –javax.servlet.http Interface Servlet and abstract class GenericServlet define a generic, protocol-independent servlet with a general service() method. public void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException;

©SoftMooreSlide 14 Template Method Pattern in Java (HTTP Servlets continued) Class HttpServlet implements the Servlet interface and provides support for the HTTP protocol. The service() method of HttpServlet dispatches the HTTP messages to one of several special methods –doGet()– doPost() –doPut()– doDelete() –doHead()– doOptions() –doTrace() The “template method” is actually performed by the web server to extract form parameters, create request and response objects, and call the appropriate HTTP method.

©SoftMooreSlide 15 Template Method Pattern in Java (HTTP Servlets continued) The service() method in HttpServlet Web Server HttpServlet service() HTTP Client doGet() doPost() doPut() doDelete() doHead() doOptions() doTrace()

©SoftMooreSlide 16 Template Method Pattern in Java (HTTP Servlets continued) Using HttpServlet Create a new servlet by extending HttpServlet and overriding either doGet() or doPost(), or possibly both. Other methods can be overridden to get more fine- grained control.

©SoftMoore ConsultingSlide 17 Template Method Pattern in Java (Example: HelloWorld Servlet) public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" "); out.println(" Hello "); out.println(" "); out.println(" Hello, world. "); out.println(" "); }

Template Method Pattern in Java (JUnit) The early versions of JUnit used idea that there is a common structure to most tests: –set up a test fixture –run some code against the fixture and check the results –clean up the fixture JUnit used the template method pattern to encapsulate this structure. public void run() { setUp(); runTest(); tearDown(); } ©SoftMoore ConsultingSlide 18 Note: This is a simplified version of the approach originally used by JUnit.

Template Method in Java (Collection Classes) In package java.util, most of the non-abstract methods of AbstractList, AbstractSet, and AbstractMap implement the Template Method pattern. For example, the equals() method of AbstractList is implemented using other methods in the class, primarily the ListIterator. Concrete implementations of AbstractList can provide different implementations of ListIterator that will be called by the equals() method. ©SoftMoore ConsultingSlide 19

Related Patterns Factory Methods are often called by template methods. Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm. ©SoftMoore ConsultingSlide 20

References Template method pattern (Wikipedia) Template Method Pattern (Object-Oriented Design) Template Method Design Pattern (SourceMaking) ©SoftMoore ConsultingSlide 21