+ All Categories
Home > Documents > Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some...

Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some...

Date post: 10-Mar-2021
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
42
Go4 Template Method, Strategy, State Patterns Presented By: Matt Wilson
Transcript
Page 1: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Go4 Template Method, Strategy, State Patterns

Presented By:

Matt Wilson

Page 2: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Introduction

2

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.

Page 3: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method

3

Page 4: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use Template Method?

4

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.

http://www.gotw.ca/publications/mill18.htm

Page 5: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use Template Method?

5

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.

Page 6: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method – UML

6

Page 7: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Benefits

7

• 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.

Page 8: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Example (C#)

8

abstract class DataAccessObject{

protected string connectionString;protected DataSet dataSet;

public virtual void Connect(){ // Make sure mdb is available to appconnectionString = "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();

}}

Page 9: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Example (C#)

9

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();

}}

Page 10: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Example (C#)

1

0

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();

}}

Page 11: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Example (C#)

11

class MainApp{

/// <summary>/// Entry point into console application./// </summary>static void Main(){DataAccessObject daoCategories = new Categories();daoCategories.Run();

DataAccessObject daoProducts = new Products();daoProducts.Run();

// Wait for userConsole.ReadKey();

}}

Page 12: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Example (C#) - OUTPUT

1

2

Categories ----BeveragesCondimentsConfectionsDairy ProductsGrains/CerealsMeat/PoultryProduceSeafood

Products ----ChaiChangAniseed SyrupChef Anton's Cajun SeasoningChef Anton's Gumbo MixGrandma's Boysenberry SpreadUncle Bob's Organic Dried PearsNorthwoods Cranberry SauceMishi Kobe Niku

Page 13: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Herb Sutter Tips - Virtuality

1

3

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).

Page 14: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Template Method Questions

?

14

Page 15: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern

15

Page 16: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use Strategy Pattern?

1

6

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.

Page 17: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use Strategy Pattern?

1

7

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.

Page 18: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use Strategy Pattern?

1

8

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)

Page 19: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern - UML

1

9

Strategy Pattern uses Inheritance to enable Composition:

Page 20: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern – SOLID Design Principles

2

0

• Strategy Pattern is in harmony with SOLID Design Principles for good OO Design

• 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.

Page 21: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern – SOLID Design Principles

2

1

• 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.

Page 22: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern – Example (Java)

2

2

/// <summary>/// The 'Strategy' abstract class/// </summary>abstract class SortStrategy{

public abstract void Sort(List<string> list);}

Page 23: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern – Example (Java)

2

3

class QuickSort : SortStrategy{

public override void Sort(List<string> list){

list.Sort(); // Default is QuicksortConsole.WriteLine("QuickSorted list ");

}}

class ShellSort : SortStrategy{

public override void Sort(List<string> list){

ShellSort(list);Console.WriteLine("ShellSorted list ");

}}

class MergeSort : SortStrategy{

public override void Sort(List<string> list){

MergeSort(list);Console.WriteLine("MergeSorted list ");

}}

Page 24: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern – Example (Java)

2

4

class SortedList {

// NOTE: INSTEAD OF HAVING MULTIPLE SUBCLASSES OF SortedList, // you only have one that contains a dynamic strategy/behavior.

private List<string> _list = new List<string>();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 resultsforeach (string name in _list){

Console.WriteLine(" " + name);}Console.WriteLine();

}}

Page 25: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern – Example (Java)

2

5

class MainApp{

/// <summary>/// Entry point into console application./// </summary>static void Main(){

// Two contexts following different strategiesSortedList 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 userConsole.ReadKey();

}}

Page 26: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern vs. Template Method

2

6

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.

Page 27: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Strategy Pattern Questions

?

27

Page 28: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

State Pattern

28

Page 29: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use State Pattern?

2

9

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.”

Page 30: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

State Machine Model Example:

3

0

Page 31: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use State Pattern?

3

1

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.

Page 32: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use State Pattern?

3

2

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:

Page 33: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

What-is/Why-use State Pattern?

3

3

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?

Page 34: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

State Pattern - UML

3

4

Page 35: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

State Pattern UML

3

5

Compare to Strategy Pattern UML

Page 36: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

State Pattern vs. Strategy Pattern

3

6

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.

A OO 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.

Page 37: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Simple State Pattern – Traffic Light Example (C#)

3

7

class MainApp{

static void Main(){

// Setup context in a state TrafficLight tl = new TrafficLight(new GreenLight());

// Issue requests, which toggles state tl.ChangeLight();tl.ChangeLight();tl.ChangeLight();tl.ChangeLight();tl.ChangeLight();tl.ChangeLight();

// Wait for user Console.Read();

}}

abstract class State{

public abstract void Handle(TrafficLight context);}

Page 38: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Simple State Pattern Example (C#)

3

8

class RedLight : State{

public override void Handle(TrafficLight context){

context.State = new GreenLight();}

}

class YellowLight : State{

public override void Handle(TrafficLight context){

context.State = new RedLight();}

}

class GreenLight : State{

public override void Handle(TrafficLight context){

context.State = new YellowLight();}

}

Page 39: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Simple State Pattern Example (C#)

3

9

class TrafficLight{

private State state; // The Composited State/Strategy

// Constructor public TrafficLight(State initialState) { this.State = initialState; }

// Property public State State{

get { return state; }set{

state = value;Console.WriteLine("State: " + state.GetType().Name);

}}

public void ChangeLight() { state.Handle(this); }}

Page 40: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

Simple State Pattern Example (C#)

4

0

Output:

Page 41: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

State Pattern

4

1

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.

Page 42: Go4 Template Method, Strategy, State Patterns€¦ · Strategy Pattern & Template Method have some things in common. Both patterns provide clients a way to execute different behavior,

State Pattern Questions

?

42


Recommended