Docsity

Strategy Pattern: Encapsulating Variable Algorithms and Avoiding Conditional Statements, Study notes of Software Engineering

Toggle sidebar
bg1
1
Strategy Pattern
Strategy Pattern
ParulParul PatelPatel
Josh GoodrichJosh Goodrich
Justin Justin PlotkinPlotkin
Jonas Jonas WeiWei
Purpose of Strategy Pattern:
Purpose of Strategy Pattern:
Encapsulate different algorithms
Encapsulate different algorithms
Strategy lets the
Strategy lets the alogorithm
alogorithm vary
vary
independantly
independantly from the clients that use it.
from the clients that use it.
The Strategy pattern defines an abstract
The Strategy pattern defines an abstract
policy for performing an algorithm At the
policy for performing an algorithm At the
same time it provides multiple
same time it provides multiple
mechanisms that can comply with that
mechanisms that can comply with that
policy, thus letting mechanism vary
policy, thus letting mechanism vary
independently from the policy required.
independently from the policy required.
pf3
pf4

Partial preview of the text

Download Strategy Pattern: Encapsulating Variable Algorithms and Avoiding Conditional Statements and more Study notes Software Engineering in PDF only on Docsity!

Strategy Pattern Strategy Pattern

ParulParul PatelPatel Josh GoodrichJosh Goodrich JustinJustin PlotkinPlotkin JonasJonas WeiWei

Purpose of Strategy Pattern: Purpose of Strategy Pattern:

Encapsulate different algorithms Encapsulate different algorithms

Strategy lets the alogorithm Strategy lets thealogorithm varyvary independantly from the clients that use it.independantlyfrom the clients that use it.

The Strategy pattern defines an abstract The Strategy pattern defines an abstract policy for performing an algorithm At thepolicy for performing an algorithm At the same time it provides multiplesame time it provides multiple mechanisms that can comply with thatmechanisms that can comply with that policy, thus letting mechanism varypolicy, thus letting mechanism vary independently from the policy required.independently from the policy required.

Use When: Use When:

You have many related classed that differ You have many related classed that differ only in their behavior.only in their behavior.

You need different variants of an You need different variants of an algorithm.algorithm.

An algorithm uses data that clients An algorithm uses data that clients shouldn't know about.shouldn't know about.

It helps to avoid exposing complex, It helps to avoid exposing complex, algorithm-algorithm-specific data structures.specific data structures.

When you have several conditional When you have several conditional statements in the program.statements in the program.

Strategy Pattern Example 1Strategy Pattern Example 1

Drawbacks: Drawbacks:

The application must be aware of all the The application must be aware of all the strategies to select the right one for thestrategies to select the right one for the right situation.right situation.

Increased number of object Increased number of object

Context and the Strategy classes normally Context and the Strategy classes normally

communicate through the interfacecommunicate through the interface