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

Similar documents
The Template Method Pattern

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

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

SDC Design patterns GoF

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

Design Patterns. An introduction

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

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Introduction to Software Engineering: Object Design I Reuse & Patterns

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

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

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Design Patterns Reid Holmes

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

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

CS251 Software Engineering Lectures 18: Intro to DP

Object-Oriented Oriented Programming

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

UNIT I Introduction to Design Patterns

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

C++ for System Developers with Design Pattern

A Reconnaissance on Design Patterns

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Object-oriented Software Design Patterns

The GoF Design Patterns Reference

Composite Pattern. IV.4 Structural Pattern

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

UNIT I Introduction to Design Patterns

Creational Design Patterns

Software Design COSC 4353/6353 D R. R A J S I N G H

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

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

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Using Design Patterns in Java Application Development

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Design for change. You should avoid

DESIGN PATTERN - INTERVIEW QUESTIONS

What is Design Patterns?

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Tuesday, October 4. Announcements

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Applying Design Patterns to SCA Implementations

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Topics in Object-Oriented Design Patterns

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Lecture 4: Observer Pattern, Event Library and Componentization

CSC207H: Software Design Lecture 6

Design Pattern and Software Architecture: IV. Design Pattern

TDDB84. Lecture 2. fredag 6 september 13

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Exam in TDDB84: Design Patterns,

Software Development Project. Kazi Masudul Alam

INSTITUTE OF AERONAUTICAL ENGINEERING

Traditional Design Patterns -- II

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

1 of 27 9/27/2001 5:34 PM

GoF Design Pattern Categories

design patterns FOR B.tech (jntu - hyderabad & kakinada) (IV/I - CSE AND IV/II - IT) CONTENTS 1.1 INTRODUCTION TO DESIGN PATTERNS TTERNS... TTERN?...

An Introduction to Patterns

Design Patterns: Elements of Reusable Object Oriented Software. Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Chapter 5 Object-Oriented Programming

Object-Oriented Design

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

Design patterns. OOD Lecture 6

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

CSE870: Advanced Software Engineering (Cheng) 1

Design patterns. Jef De Smedt Beta VZW

CS/CE 2336 Computer Science II

DESIGN PATTERNS SURESH BABU M ASST PROFESSOR VJIT

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

Singleton Pattern Creational

CS 247: Software Engineering Principles. Design Patterns

What is Design Patterns?

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

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

Design Patterns. State. Oliver Haase

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Design Patterns. GoF design patterns catalog

Lecture 20: Design Patterns II

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

SWEN425 DESIGN PATTERNS

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Object-Oriented Design

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Design Patterns! Acknowledgements!

Topics. Software Process. Agile. Requirements. Basic Design. Modular Design. Design Patterns. Testing. Quality. Refactoring.

Software Engineering I (02161)

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

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

Ownership in Design Patterns. Master's Thesis Final Presentation Stefan Nägeli

UP Requirements. Software Design - Dr Eitan Hadar (c) Activities of greater emphasis in this book. UP Workflows. Business Modeling.

Software Design COSC 4353/6353 D R. R A J S I N G H

Transcription:

CSCI 253 Object Oriented Design: George Blankenship George Blankenship 1 Creational Patterns Singleton Abstract factory Factory Method Prototype Builder Overview Structural Patterns Composite Façade Proxy Flyweight Adapter Bridge Decorator Behavioral Patterns Chain of Respons. Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor George Blankenship 2 The Elements of a Design Pattern A pattern name The problem that the pattern solves Including conditions for the pattern to be applicable The solution to the problem brought by the pattern The elements (classes-objects) involved, their roles, responsibilities, relationships and collaborations Not a particular concrete design or implementation The consequences of applying the pattern Time and space trade off Language and implementation issues Effects on flexibility, extensibility, portability George Blankenship 3 George Blankenship 1

The : The Problem 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. Sometimes you want to specify the order of operations that a method uses, but allow subclasses to provide their own implementations of some of these operations George Blankenship 4 OpenDocument() Method public void OpenDocument (String name) { if (!CanOpenDocument(name)) { return; Document doc = DoCreateDocument(); if (doc!= null) { docs.adddocument(doc); AboutToOpenDocument(doc); doc.open(); doc.doread(); The OpenDocument() method is a Template Method The template method fixes the order of operations, but allows Application subclasses to vary those steps as needed George Blankenship 5 TextDocument Class public abstract class TextDocument {... public final void printpage (Page page) { printtextheader(); printtextbody(page); printtextfooter(); public abstract void printtextheader(); public final void printtextbody(page page) { System.out.println(page.body()); public abstract void printtextfooter();... The class includes a printpage() template method in a superclass that allows for specific document classes to provide unique implementations of the abstract methods to print the header and footer George Blankenship 6 George Blankenship 2

The : Applicability Use the Template Method pattern: To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary To localize common behavior among subclasses and place it in a common class (in this case, a superclass) to avoid code duplication. This is a classic example of code refactoring. To control how subclasses extend superclass operations. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points. The Template Method is a fundamental technique for code reuse. George Blankenship 7 Template Method class Account { public: void Transaction(float amount); void virtual TransactionSubpartA(); void virtual TransactionSubpartB(); void virtual TransactionSubpartC(); void Account::Transaction(float amount) { TransactionSubpartA(); TransactionSubpartB(); TransactionSubpartC(); // EvenMoreCode; class JuniorAccount : public Account { public: void virtual TransactionSubpartA(); class SavingsAccount : public Account { public: void virtual TransactionSubpartC(); Account* customer; customer = createnewaccount(); customer-&gttransaction(amount); George Blankenship 8 Good Polymorphism/Bad Polymorphism George Blankenship 9 George Blankenship 3

The : Structure George Blankenship 10 The : Participants Template methods tend to call: concrete operations concrete AbstractClass operations primitive operations - must be overridden factory methods hook operations - can be overridden It is important to denote: which methods must overridden which methods can be overridden which methods can not be overridden George Blankenship 11 The : Collaboration Strategy is like Template Method except in its granularity. [Coplien, C++ Report, Mar 96, p88] Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm. [GOF, p330] George Blankenship 12 George Blankenship 4

The : Consequences Method overloading and method overriding are good examples of template method pattern. Coercion polymorphism -- refers to a single operation serving several types through implicit type conversion. Overloading polymorphism -- refers to using a single identifier for different operations. Parametric polymorphism -- refers to a class declaration that allows the same field names and method signatures to associate with a different type in each instance of that class. Note that the side effect of using coercion polymorphism is casting in and casting out if you need specific type to do the work. If you forget to do so, you may have unexpected result and it is hard to debug. Without using template method pattern, you may write more lines of code. The good thing is that you don't have any side effect by using specific designed method and you don't need to cast in or out. George Blankenship 13 The : Implementation Operations which must be overridden by a subclass should be made abstract If the template method itself should not be overridden by a subclass, it should be made final To allow a subclass to insert code at a specific spot in the operation of the algorithm, insert hook operations into the template method. These hook operations may do nothing by default. Try to minimize the number of operations that a subclass must override, otherwise using the template method becomes tedious for the developer In a template method, the parent class calls the operations of a subclass and not the other way around. This is an inverted control structure that's sometimes referred to as "the Hollywood principle," as in, "Don't call us, we'll call you". George Blankenship 14 George Blankenship 5