1 GoF Template Method (pp. 325-330) GoF Strategy (pp. 315-323) PH Single User Protection (pp. 34-40) Presentation by Julie Betlach 6/08/2009.

Slides:



Advertisements
Similar presentations
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Advertisements

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Plab – Tirgul 12 Design Patterns
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
Strategy Design Patterns CS 590L - Sushil Puradkar.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
GoF Sections Design Problems and Design Patterns.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
Stéphane Ducasse 1 Strategy.
CSC 480 Software Engineering Design With Patterns.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
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.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Behavioral Design Patterns
Software Design and Architecture
More Design Patterns 1.
PH page GoF Singleton p Emanuel Ekstrom.
Multiuser Protection and the Mediator Pattern
More Design Patterns 1.
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
SE-2811 Software Component Design
Design pattern Lecture 9.
Strategy and Template Method Patterns, Single User Protection
Strategy Design Pattern
Lesson 5: More on Creational Patterns
Design by Abstraction (Continuation) CS 3331 Spring 2005
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009

2 Template Method Pattern - Intent / Applicability Intent –Define the skeleton of an algorithm, deferring some steps to subclasses. –Template Method lets subclasses redefine certain steps of an algorithm, without changing the algorithm’s structure. Applicability – Use Template Method Pattern… –To implement the invariant part of an algorithm, and leave it up to subclasses to implement the part that can vary. –When common code in subclasses should be factored and localized in a common base class to avoid code duplication. –To control extensions to subclasses. (Hook operations) GoF Template Method (pp )

3 Template Method Pattern - 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. It calls primitive operations as well as other operations in the AbstractClass (or other classes). GoF Template Method (pp ) ConcreteClass –Implements primitive operations to carry out subclass-specific steps of the algorithm. Note: Some concrete AbstractClass methods can also be defined. Hollywood Principle – “Don’t call us, we’ll call you.” => Parent class calls operations of subclass.

4 Template Method Pattern - Participants void Application::OpenDocument (const char* name) { if (!CanOpenDocument(name)) { return; } Document* doc = DoCreateDocument(); if (doc) { _docs->AddDocument(doc); AboutToOpenDocument(doc); doc->Open(); doc->DoRead(); } GoF Template Method (pp ) Basic Algorithm is the same for all documents. Template Method PrimitiveOperation Concrete AbstractClass Operation

5 Template Method Pattern - Implementation void Application::OpenDocument (const char* name) { if (!CanOpenDocument(name)) { return; } Document* doc = DoCreateDocument(); if (doc) { _docs->AddDocument(doc); AboutToOpenDocument(doc); doc->Open(); doc->DoRead(); } GoF Template Method (pp ) Primitive Operation that has a default behavior in abstract class = Hook operation. Template Method – Don’t want this to be overridden, so make it nonvirtual member function. Primitive Operations that must be overridden (make these pure virtual in abstract class). Concrete AbstractClass Operation Naming Convention suggestion is confusing. Are DoCreateDocument and DoRead from the example called Template Methods? I think book should have said that MacApp framework prefixes primitive operation names with “Do”.

6 Related Patterns Factory Methods – Are often called by template methods. Example: Factory Method DoCreateDocument is called by Template Method OpenDocument. Strategy – Template Methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm. GoF Template Method (pp )

7 Strategy Pattern - Intent / Applicability Intent –Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Applicability – Use Strategy Pattern when… –Many related classes differ only in their behavior. –You need different variants of an algorithm. For example, defining different algorithms based on space-time tradeoffs. –An algorithm uses data that a client shouldn’t know about. –A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead, move related conditional branches into their own Strategy class. GoF Strategy (pp )

8 Strategy Pattern - Participants Strategy –Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy. ConcreteStrategy –Implements the algorithm using the Strategy interface. Context –Is configured with a ConcreteStrategy object –Maintains a reference to a Strategy object –May define an interface that lets Strategy access its data. GoF Strategy (pp ) Picture & Example from:

9 Strategy Pattern - Participants Show code in RealWorld Example Strategy –SortStrategy ConcreteStrategy –QuickSort –ShellSort –MergeSort Context –SortedList GoF Strategy (pp ) Picture & Example from:

10 Strategy Pattern – Consequences Good Consequences –Good for families of related algorithms. Inheritance can help factor out common functionality of algorithms. –Alternative to subclassing which allows the developer to vary the algorithm independent of its context. –Strategies eliminate conditional statements. –Provide different implementations of the same behavior. Example: Sorting is goal, but many ways to sort. Drawbacks –Clients must be aware of different strategies. –Communication overhead between Strategy and Context – some concrete strategies may not need all the parameters passed into them. –Increased number of objects. See Flyweight pattern for an approach to solve this. GoF Strategy (pp )

11 Related Patterns Flyweight – Strategy objects often make good flyweights. GoF Strategy (pp )

12 Single User Protection Read Protection (do not reveal contents) –Disallow streamOut –Disallow getChild (makes sense if you consider example of hiding the family tree information of a person, where getChild returns the name of the person’s descendents.) Write Protection (protect both attributes and structure) –Disallow streamIn, setName, adopt, orphan –Treat setProtection carefully (explained in multi-user section) PH Single User Protection (pp )

13 Single User Protection Write Protection (continued) –Prevent deletion Can’t declare a node as a const in order to get compiler to check this for us, because a node’s protection may change at runtime. Instead, we can Protect the Destructor by putting it in the Protected section. –Don’t want to make it private because that wouldn’t let subclasses extend the destructor to delete their children or any other objects that they aggregate. We want a static member function (makes syntax nicer) and we want the subclasses to be able to extend the definition. PH Single User Protection (pp )

14 Single User Protection Solution = Destroy() will be a Template Method! PH Single User Protection (pp ) class Node { public: static void destroy (Node*); // template method protected: virtual ~Node(); virtual bool isWriteable() = 0; // primitive operation virtual void doWarning() = 0; // primitive operation }; void Node::destroy (Node* node) { if (node->isWriteable()) { delete node; } else { node->doWarning(undeletableWarning); } Technically, isWriteable should start with “do” to be clear it is a primitive operation. Because destroy is static, it can’t call Node operations directly. But it doesn’t need to because it calls primitive operations in the subclasses.

15 Single User Protection PH Single User Protection (pp ) class Node { public: static void destroy (Node*); // static template method void streamOut (ostream& out); // non-static template method (also not virtual) protected: virtual ~Node(); virtual bool isWriteable() = 0; // primitive operation virtual bool isReadable() = 0; // primitive operation virtual void doWarning() = 0; // primitive operation virtual void doStreamOut() = 0; // primitive operation }; void Node::streamOut (ostream& out) { if (isReadable()) { doStreamOut(); } else { doWarning(unreadableWarning); } However, if we define another template method which is not static, it is allowed to call Node functions, so it doesn’t need the node passed in.