Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Similar documents
The Template Method Pattern

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Factory Method Pattern Creational. » Define an interface for creating an object but lets subclasses decide the specific class to instantiate

Singleton Pattern Creational

Design Patterns. State. Oliver Haase

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Singleton Pattern Creational. » Ensure a class has only one instance» Provide a global point of access

Object Oriented Concepts. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Design Patterns Reid Holmes

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Template Method. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 24 11/15/2007. University of Colorado, 2007

SDC Design patterns GoF

Produced by. Mobile Application Development. Eamonn de Leastar

Design Pattern: Composite

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Lecture Material. Design Patterns. Visitor Client-Server Factory Singleton

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

Introduction to Design Patterns

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Algorithms. Produced by. Eamonn de Leastar

EINDHOVEN UNIVERSITY OF TECHNOLOGY

EECS 4314 Advanced Software Engineering. Topic 04: Design Pattern Review Zhen Ming (Jack) Jiang

Topics in Object-Oriented Design Patterns

Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Tackling Design Patterns Chapter 3: Template Method design pattern and Public Inheritance. 3.1 Introduction... 2

Creational Patterns. Factory Method (FM) Abstract Factory (AF) Singleton (SI) Prototype (PR) Builder (BU)

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Introduction to Design Patterns

Exam in TDDB84: Design Patterns,

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

Object-Oriented Oriented Programming

Template Method Pa.ern and Iterator Pa.ern

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Agile Software Development. Eamonn de Leastar

Automated Detection of Code Duplication Clusters

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Design for change. You should avoid

GoF Design Pattern Categories

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-Oriented Oriented Programming Factory Method Pattern Abstract Factory Pattern. CSIE Department, NTUT Woei-Kae Chen

Design Pattern and Software Architecture: IV. Design Pattern

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Produced by. App Development & Modelling. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

CS111: PROGRAMMING LANGUAGE II

8. Polymorphism and Inheritance

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Annotations in Java (JUnit)

Lecture 13: Design Patterns

An Introduction to Patterns

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Design Pattern- Creational pattern 2015

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Produced by. App Development & Modelling. BSc in Applied Computing. Eamonn de Leastar

CSCE3193: Programming Paradigms

CSE870: Advanced Software Engineering (Cheng) 1

CHAPTER 9 DESIGN ENGINEERING. Overview

Software Engineering I (02161)

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

Tecniche di Progettazione: Design Patterns

Chapter 3 - Introduction to Java Applets

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Design Patterns: Template Method, Strategy, State, Bridge

Java Object Oriented Design. CSC207 Fall 2014

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

Behavioral Design Patterns Used in Data Structures Implementation

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

MORE DESIGN PATTERNS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 24 11/10/2011

Adapter Pattern Structural

OO design. Classes, Responsibilities, Collaborations (CRC) 13/9/1999 COSC

James Newkirk

Design Patterns Revisited

Design Patterns. GoF design patterns catalog

Object-Oriented Oriented Programming Command Pattern. CSIE Department, NTUT Woei-Kae Chen

Patterns for polymorphic operations

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

The GoF Design Patterns Reference

Idioms for Building Software Frameworks in AspectJ

Decorator Pattern. CS356 Object-Oriented Design and Programming November 7, 2014

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

MSO Lecture 12. Wouter Swierstra (adapted by HP) October 26, 2017

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

LABORATORY 1 REVISION

Liskov Substitution Principle

Chapter 14 Abstract Classes and Interfaces

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Transcription:

Design Patterns Produced by Eamonn de Leastar edeleastar@wit.ie Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

Template Method Design Pattern

Summary Create an abstract class that implements a procedure using abstract methods. These abstract methods must be implemented in derived classes to actually perform each step of the procedure. 3

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 structure. 4

Motivation (example 1) Consider an application framework that provides Application and Document classes. The Application class is responsible for opening existing documents stored in an external format, such as a file. A Document object represents the information in a document once it's read from the file. Applications built with the framework can subclass Application and Document to suit specific needs. For example, a drawing application defines DrawApplication and DrawDocument subclasses; A spreadsheet application defines SpreadsheetApplication and SpreadsheetDocument subclasses. 5

Motivation (2) 6

Motivation (4) opendocument() is a template method. A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior. Application subclasses define the steps of the algorithm that check if the document can be opened (canopendocument) and that create the Document (docreatedocument). Document classes define the step that reads the document (doread). 7

Motivation (4) The template method also defines an operation that lets Application subclasses know when the document is about to be opened (abouttoopendocument), in case they care. By defining some of the steps of an algorithm using abstract operations, the template method fixes their ordering, but it lets Application and Document subclasses vary those steps to suit their needs. 8

opendocument template method class Document //... void opendocument(string name) if (!canopendocument(name)) // cannot handle this document return; Document doc = docreatedocument(); if (doc!= null) docs.adddocument(doc); abouttoopendocument(doc); doc.open(); doc.doread(); //... The abstract Application class defines the algorithm for opening and reading a document in its opendocument operation opendocument defines each step for opening a document. It checks if the document can be opened, creates the application-specific Document object, adds it to its set of documents, and reads the Document from a file. 9

Applicability To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behaviour that can vary. When common behaviour among subclasses should be factored and localized in a common class to avoid code duplication. To control subclasses extensions. Define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points. 10

Structure 11

Participants AbstractClass (Application) 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 (MyApplication) implements the primitive operations to carry out subclass-specific steps of the algorithm. 12

Collaborations ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm. 13

Consequences (1) Template Method is used prominently in frameworks. The framework implements the invariant pieces of a domain's architecture, and defines "placeholders" for all necessary or client customization options. The framework will then call client code, e.g. primitive operations of a template method. This inverted control structure has been labeled "the Hollywood principle" - "don't call us, we'll call you". 14

Consequences (2) Template methods typically call the following type of methods Concrete AbstractClass operations (that are common to all subclasses) Primitive operations that must be overridden Factory methods (creation of a certain type of object is a variability in the template method, implemented as a Factory. Hook operations which provide default behavior that the subclasses can extend if necessary. Often hook operations do nothing as a default. The specialization interface must be described (i.e. inheritance interface). At minimum indicate which methods Must overridden Can be overridden Can not be overridden 15

Example 2 A familiar example of a framework that is extended using a Template Method is Java s applet. When you create an applet you re using an application framework: you inherit from JApplet and then override init( ). The applet mechanism (which is a Template Method in this case) does the rest by drawing the screen, handling the event loop, resizing, etc. An important characteristic of the Template Method is that it is defined in the base class and changing it is prohibited, thus it should be declared final. It calls other base-class methods (the ones you override) in order to do its job, but it is usually called only as part of an initialization process (and thus the client programmer isn t necessarily able to call it directly). 16

UML Pattern Notation 17

Example 3 PlainTextDocument & HtmlTextDocument public class PlainTextDocument... public void printpage (Page page) printplaintextheader(); System.out.println(page.body()); printplaintextfooter();... public class HtmlTextDocument... public void printpage (Page page) printhtmltextheader(); System.out.println(page.body()); printhtmltextfooter();...

TextDocument superclass Template Method public abstract class TextDocument... public final void printpage (Page page) printtextheader(); printtextbody(page); printtextfooter(); abstract void printtextheader(); final void printtextbody(page page) System.out.println(page.body()); abstract void printtextfooter();...

PlainTextDocument public class PlainTextDocument extends TextDocument... public void printtextheader () // Code for header plain text header here. public void printtextfooter () // Code for header plain text footer here....

HTMLTextDocument public class HTMLTextDocument extends TextDocument... public void printtextheader () // Code for header HTML text header here. public void printtextfooter () // Code for header HTML text footer here....

Except where otherwise noted, this content is licensed under a Creative Commons Attribution- NonCommercial 3.0 License. For more information, please see http:// creativecommons.org/licenses/by-nc/3.0/