Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,

Slides:



Advertisements
Similar presentations
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Advertisements

SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
1 Software Testing and Quality Assurance Lecture 28 – Testing Class Hierarchies.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
The Template Method By Sinclair Schuller. What is the Template Method? “Skeleton” definition of an algorithm Allows redefinition of predetermined points.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Programming Languages and Paradigms Object-Oriented Programming.
Introduction to Object-oriented programming and software development Lecture 1.
Structural Pattern: Decorator There are times when the use of subclasses to modify the behavior of individual objects is problematic. C h a p t e r 4.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
1. 2 Object-Oriented Concept Class & Object Object-Oriented Characteristics How does it work? Relationships Between Classes Development Tools Advantage.
Behavioral Pattern: Command C h a p t e r 5 – P a g e 139 There are times when the need arises to issue a request to an object without knowing anything.
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
1 Computer Science 340 Software Design & Testing Inheritance.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Chapter 10 Inheritance and Polymorphism
Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult.
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Object Oriented Programming
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Inspired by the Oulipu. The 3 Tenets of OO Encapsulation Polymorphism Inheritance.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
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.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Behavioral Pattern: Mediator C h a p t e r 5 – P a g e 169 When a program is made up of several classes, the logic and computation is divided among these.
Structural Patterns C h a p t e r 4 – P a g e 55 StructuralPatterns Design patterns that describe how classes and objects can be combined to form larger.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
Motivation for Generic Programming in C++
Design Patterns: MORE Examples
Sections Inheritance and Abstract Classes
CS 3370 – C++ Object-oriented Programming
Introduction to Design Patterns
Inheritance and Polymorphism
Behavioral Design Patterns
Software Design and Architecture
Object-Oriented Programming
object oriented Principles of software design
Object Oriented Analysis and Design
More Design Patterns 1.
More Design Patterns 1.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Lesson 5: More on Creational Patterns
Chapter 14 Abstract Classes and Interfaces
CS 325: Software Engineering
Presentation transcript:

Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities, but will demonstrate no reuse of common interface or implementation. The Template Method Pattern handles this problem by having the skeleton of an algorithm defined in a base class, with placeholders replacing certain key steps. Derived classes then implement the placeholders, allowing those steps of the algorithm to be altered without modifying the algorithm’s basic structure. If a change common to both components becomes necessary, duplicate effort must be expended.

The Template Method Pattern C h a p t e r 5 – P a g e 218 The AbstractClass defines abstract primitive operations that concrete subclasses define to implement the steps of an algorithm, and implements a template method defining the skeleton of an algorithm. The ConcreteClass implements the primitive operations to carry out subclass-specific steps of the algorithm. The template method calls primitive operations as well as operations defined in the AbstractClass or those of other objects.

C h a p t e r 5 – P a g e 219 Non-Software Example: Buildings Builders use the Template Method when developing a new subdivision or office complex. A typical subdivision consists of a limited number of floor plans with different variations available for each. Within a floor plan, the foundation, framing, plumbing, and wiring will be identical for each house. Variation is introduced in the later stages of construction to produce a wider variety of models.

C h a p t e r 5 – P a g e 220 Software Example: Accounts The abstract Account class contains the template method Withdraw, as well as placeholders for the primitive methods Start(), Allow(), and End(). Each of the derived classes contain their own implementations for the primitive operations. Essentially, the Account will use the MaxLimit value associated with each derived class to determine whether a specific withdrawal will be allowed.

C h a p t e r 5 – P a g e 221 Accounts Template Method C++ Code #include using namespace std; // Base class // Template class class Account { public: // Abstract Methods virtual void Start() = 0; virtual void Allow() = 0; virtual void End() = 0; virtual int MaxLimit() = 0; // Template Method void Withdraw(int amount) { Start(); cout << "Withdraw Request: $" << amount << endl; int limit = MaxLimit(); if ( amount <= limit ) { cout << "(Within $" << limit << " Limit)" << endl; Allow(); } else cout << "Not Allowed: Over $" << limit << " Limit" << endl; End(); } };

C h a p t e r 5 – P a g e 222 // Derived class class AccountNormal : public Account { public: void Start() { cout << "Normal Start..." << endl; } void Allow() { cout << "Normal Allow..." << endl; } void End() { cout << "Normal End..." << endl << endl; } int MaxLimit() { return 1000; } }; // Derived class class AccountPower : public Account { public: void Start() { cout << "Power Start..." << endl; } void Allow() { cout << "Power Allow..." << endl; } void End() { cout << "Power End..." << endl << endl; } int MaxLimit() { return 5000; } }; void main() { AccountPower power; power.Withdraw(1500); AccountNormal normal; normal.Withdraw(1500); }

Template Method Pattern Advantages C h a p t e r 5 – P a g e 223 The primary advantage of the Template Method Pattern is the elimination of code duplication. In general, composition is favored over inheritance, primarily because of composition’s tendency not to break encapsulation. The Template Method Pattern, however, favors inheritance, since it achieves greater flexibility by allowing subclasses to use some operations of the superclass while overriding others. In addition, this pattern takes advantage of polymorphism, allowing the base class to automatically call the methods of the correct subclasses.