Go4 Template Method, Strategy, State Patterns Presented By: Matt Wilson.

Slides:



Advertisements
Similar presentations
Inheritance Inheritance Reserved word protected Reserved word super
Advertisements

SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Polymorphism &Virtual Functions
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Setting up for TTD in Visual Studio 2012 Project | Manage NuGet Packages Select the online tab Search for Nunit Select the Nunit package Follow these instructions.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Go4 Visitor Pattern Presented By: Matt Wilson. Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
1 Computer Science 340 Software Design & Testing Inheritance.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
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.
Introducing Allors Applications, Tools & Platform.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Object-Oriented Programming Chapter Chapter
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
ISBN Object-Oriented Programming Chapter Chapter
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
It started with a simple … A highly successful duck pond simulation game called SimUDuck The game can show a large variety of duck swimming and making.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Overview of C++ Polymorphism
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Design Patterns: Behavioral Design Patterns General and reusable solutions to common problems in software design Software University
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
CSCE 240 – Intro to Software Engineering Lecture 3.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Design Patterns: MORE Examples
Unit II-Chapter No. : 5- design Patterns
Strategy Design Pattern
How to be a Good Developer
Inheritance and Polymorphism
Behavioral Design Patterns
Strategy Design Pattern
More About Inheritance & Interfaces
DESIGN PATTERNS : Strategy Pattern
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Overview of C++ Polymorphism
Presentation transcript:

Go4 Template Method, Strategy, State Patterns Presented By: Matt Wilson

Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct program) developers who wanted to continue to get together on occasion to discuss technical topics.  It’s expected that all Senior Software Engineers understand the G04 Design Patterns well, and thus their occasional review is useful.

Introduction 3  This Presentation Series was created to not only teach what the patterns are, but explain best-practices of when and why you would want to use them  Sources used here included the Gang of 4 Design Patterns Book, Head First Design Patterns book, and many online articles and discussion boards on the topic.

Template Method 4

What-is/Why-use Template Method? 5  Good news: Template Method is an Easy/Simple effective G04 pattern to learn.  Very commonly used Design Pattern.  Template Method is a Non-Virtual class’ Method that calls one or more other of the class’ methods that are Virtual.  Subclasses override the Class with the Template Method, and implement their own behavior for the Virtual methods.  A Herb Sutter Guideline for good OO design:  Prefer to make interfaces nonvirtual, using Template Method.  Gives benefits with little to no downside. 

What-is/Why-use Template Method? 6 Formal definition: Template Method: “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. ” Like other G04 Pattern descriptions, it’s very technical/complicated sounding, but it actually very straightforward.

Template Method – UML 7

Template Method Benefits 8 A single class owns the Algorithm. SubClasses don’t need to know about it anymore. Eliminates duplicate code across SubClasses, maximizing reuse. Provides a Framework for SubClass extension. Use Template Method whenever you want to create a method for SubClasses to implement.

Template Method Example (C#) 9 abstract class DataAccessObject { protected string connectionString; protected DataSet dataSet; public virtual void Connect() { // Make sure mdb is available to app connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source=..\\..\\..\\db1.mdb"; } public abstract void Select(); public abstract void Process(); public virtual void Disconnect() { connectionString = ""; } // The 'Template Method' public void Run() { Connect(); Select(); Process(); Disconnect(); }

Template Method Example (C#) 10 class Categories : DataAccessObject { public override void Select() { string sql = "select CategoryName from Categories"; OleDbDataAdapter dataAdapter = new OleDbDataAdapter( sql, connectionString); dataSet = new DataSet(); dataAdapter.Fill(dataSet, "Categories"); } public override void Process() { Console.WriteLine("Categories ---- "); DataTable dataTable = dataSet.Tables["Categories"]; foreach (DataRow row in dataTable.Rows) { Console.WriteLine(row["CategoryName"]); } Console.WriteLine(); }

Template Method Example (C#) 11 class Products : DataAccessObject { public override void Select() { string sql = "select ProductName from Products"; OleDbDataAdapter dataAdapter = new OleDbDataAdapter( sql, connectionString); dataSet = new DataSet(); dataAdapter.Fill(dataSet, "Products"); } public override void Process() { Console.WriteLine("Products ---- "); DataTable dataTable = dataSet.Tables["Products"]; foreach (DataRow row in dataTable.Rows) { Console.WriteLine(row["ProductName"]); } Console.WriteLine(); }

Template Method Example (C#) 12 class MainApp { /// /// Entry point into console application. /// static void Main() { DataAccessObject daoCategories = new Categories(); daoCategories.Run(); DataAccessObject daoProducts = new Products(); daoProducts.Run(); // Wait for user Console.ReadKey(); }

Template Method Example (C#) - OUTPUT 13 Categories ---- Beverages Condiments Confections Dairy Products Grains/Cereals Meat/Poultry Produce Seafood Products ---- Chai Chang Aniseed Syrup Chef Anton's Cajun Seasoning Chef Anton's Gumbo Mix Grandma's Boysenberry Spread Uncle Bob's Organic Dried Pears Northwoods Cranberry Sauce Mishi Kobe Niku

Template Method Herb Sutter Tips - Virtuality 14 As referenced earlier, Herb Sutter is an advocate of Template Method. See the link for details. In summary, his Guidelines for C++ development regarding this pattern are the following: #1: Prefer to make interfaces nonvirtual, using Template Method. #2: Prefer to make virtual functions private. #3: Only if derived classes need to invoke the base implementation of a virtual function, make the virtual function protected. #4: A base class destructor should be either public and virtual (common), or protected and nonvirtual (rare).

Template Method Questions ? 15

Strategy Pattern 16

What-is/Why-use Strategy Pattern? 17  When C++ Guru’s (Sutter, Myers, etc.) tell us: “Prefer Composition over Inheritance”, it’s a mistake to simply believe that they mean ”Inheritance is bad” and discourage its use in your designs. That is not what they mean.  What they are actually telling us, is to implement the Strategy Pattern!  Why are they telling us to use it? Because Strategy Pattern is a more flexible, Composition alternative to Inheritance.

What-is/Why-use Strategy Pattern? 18  Formal definition: “The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”  In other words: The Strategy Pattern allows you to dynamically change the code Methods execute during run time.

What-is/Why-use Strategy Pattern? 19  Instead of utilizing Inheritance to encapsulate different behaviors inside different SubTypes, it uses Composition to encapsulate different behaviors inside member variables.  Strategy Pattern is useful for eliminating fragile if-else/switch code.  Note: Strategy Pattern is very similar to State Pattern (more on this later)

Strategy Pattern - UML 20  Strategy Pattern uses Inheritance to enable Composition:

Strategy Pattern – SOLID Design Principles 21 Strategy Pattern is in harmony with SOLID Design Principles for good OO DesignSOLID Open Closed Principle Open for extension, but closed for modification Or as I like to put it: Writing new code shouldn’t require a change to existing code As new behaviors are needed, new Strategies can be created without effecting any existing code. Dependency Inversion Principle: Classes shouldn’t have references to Concrete Classes Violation creates dependency problems, longer compile times, and prohibits Unit Testing/dependency injection.

Strategy Pattern – SOLID Design Principles 22 Strategy Pattern also helps keep the interface of SuperClasses clean and uncluttered from needing to have methods that don’t belong on some SubClasses. Example: Should fly() be a method on a CBirdClass? Some bird Subclasses can’t fly! Instead, use Strategy Pattern to define a flyBehavior() interface.

Strategy Pattern – Example (Java) 23 /// /// The 'Strategy' abstract class /// abstract class SortStrategy { public abstract void Sort(List list); }

Strategy Pattern – Example (Java) 24 class QuickSort : SortStrategy { public override void Sort(List list) { list.Sort(); // Default is Quicksort Console.WriteLine("QuickSorted list "); } class ShellSort : SortStrategy { public override void Sort(List list) { ShellSort(list); Console.WriteLine("ShellSorted list "); } class MergeSort : SortStrategy { public override void Sort(List list) { MergeSort(list); Console.WriteLine("MergeSorted list "); }

Strategy Pattern – Example (Java) 25 class SortedList { // NOTE: INSTEAD OF HAVING MULTIPLE SUBCLASSES OF SortedList, // you only have one that contains a dynamic strategy/behavior. private List _list = new List (); private SortStrategy _sortstrategy; public void SetSortStrategy(SortStrategy sortstrategy) { this._sortstrategy = sortstrategy; } public void Add(string name) { _list.Add(name); } public void Sort() { _sortstrategy.Sort(_list); // Iterate over list and display results foreach (string name in _list) { Console.WriteLine(" " + name); } Console.WriteLine(); }

Strategy Pattern – Example (Java) 26 class MainApp { /// /// Entry point into console application. /// static void Main() { // Two contexts following different strategies SortedList studentRecords = new SortedList(); studentRecords.Add("Samual"); studentRecords.Add("Jimmy"); studentRecords.Add("Sandra"); studentRecords.Add("Vivek"); studentRecords.Add("Anna"); studentRecords.SetSortStrategy(new QuickSort()); studentRecords.Sort(); studentRecords.SetSortStrategy(new ShellSort()); studentRecords.Sort(); studentRecords.SetSortStrategy(new MergeSort()); studentRecords.Sort(); // Wait for user Console.ReadKey(); }

Strategy Pattern vs. Template Method 27 Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior, based on using different Types that encapsulate different behavior. However, Temple Method enables this through Inheritance, while Strategy enables it though Composition. Template Method provides a concrete method that “owns” the algorithm, whereas in Strategy pattern, there is no “owner” of an algorithm. You may or may not prefer an “owner” depending upon the context of your specific needs. Strategy is more in line with SOLID dependency inversion principles, and is more flexible, however using Template Method may give you simpler designs which is also important. Basic rule of thumb: Use Template Method whenever you are creating Virtual Methods. Use Strategy Pattern whenever you want to change class behavior at runtime, without the use of Inheritance.

Strategy Pattern Questions ? 28

State Pattern 29

What-is/Why-use State Pattern? 30  The Go4 State Pattern is an OO way to create a State Machine in software.  Definition of State Machine:  “A state machine, is a model used to design both computer programs and sequential logic circuits. It can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition. A state machine is defined by a list of its states, and the triggering condition for each transition.”computer programssequential logic states

State Machine Model Example: 31

What-is/Why-use State Pattern? 32  Why implement a State Machine?  State machines are incredibly useful for modeling and implementing complex processes.  It enables your program/state-machine to change its behavior during runtime, such that different code executes in Methods based on the current State of the machine.  State Pattern is useful for eliminating fragile if-else/switch code.

What-is/Why-use State Pattern? 33  Without a State Machine:  CYourClass::DoSomething() { if (x) then dox(); else if (y) then do(y)… etc. }  With a State Machine  CYourClass::DoSomething() { state->doit():  } // CLEAN extensible OO - good SOLID Principles:

What-is/Why-use State Pattern? 34  Does this Pattern sound Familiar?  “Enables your program to change its behavior during runtime”  “Pattern is useful for eliminating fragile if-else/switch code.”  ?  Well let’s look at the UML? Does this look familiar?

State Pattern - UML 35

State Pattern UML 36  Compare to Strategy Pattern UML

State Pattern vs. Strategy Pattern 37 The State Pattern has the same UML Structure as the Strategy Pattern. So is it the same Pattern? Practically yes, but Technically no. A “State” and a “Strategy” can be looked at as the same thing, a class owned via composition, that encapsulates different behaviors. When WMS had OO Training, the teacher said these two patterns “can be looked at as the same thing”, and that is true in regards to their Structure. However the difference is in implementation details. The State Pattern “is-a” or “type-of” Strategy Pattern, but State Pattern adds State Machine logic to drive the setting of the current States/Strategies. The main difference: In Strategy Pattern, the composited Strategies/States are changed at runtime typically by the client who wants control over what Strategy/State they want to use. In State Pattern, the composited States/Strategies are not changed by the client, but by the rules of the State Machine enforced in code.

Simple State Pattern Example (C#) 38 using System; class MainApp { static void Main() { // Setup context in a state Context c = new Context(new ConcreteStateA()); // Issue requests, which toggles state c.Request(); // Wait for user Console.Read(); }

Simple State Pattern Example (C#) 39 // "State" abstract class State { public abstract void Handle(Context context); } // "ConcreteStateA" class ConcreteStateA : State { public override void Handle(Context context) { context.State = new ConcreteStateB(); } // "ConcreteStateB" class ConcreteStateB : State { public override void Handle(Context context) { context.State = new ConcreteStateA(); }

Simple State Pattern Example (C#) 40 // "Context" class Context { private State state; // THIS IS THE Composited State/Strategy // Constructor public Context(State state) { this.State = state; } // Property public State State { get{ return state; } set { state = value; Console.WriteLine("State: " + state.GetType().Name); } public void Request() { state.Handle(this); } }

Simple State Pattern Example (C#) 41 Output: State: ConcreteStateA State: ConcreteStateB State: ConcreteStateA State: ConcreteStateB State: ConcreteStateA

State Pattern 42 Most of the time to create a State pattern, you will begin by creating a State UML diagram for your States and their Transitions, then implement the rules in code. Many tools exist that generate code from State Diagrams.

State Pattern Questions ? 43