the strategy pattern csc 211: design patterns. reading quiz
TRANSCRIPT
![Page 1: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/1.jpg)
The Strategy Pattern
CSC 211: Design Patterns
![Page 2: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/2.jpg)
Reading Quiz
![Page 3: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/3.jpg)
Video Game Example• Aliens vs. Humans
• Both have adjustable life points
• Both take damage
• Both can recover life points
• Let’s design a class…
• Some functionality is shared, so inheriting from a common superclass makes sense
• We’ll differentiate between the two classes in their subclasses
![Page 4: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/4.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Class name
Class variables
Classmethods
![Page 5: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/5.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : int
takeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
<<create>> = constructor
What does Human inherit from LifeForm?
What does Human add to extend its superclass?
extends
![Page 6: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/6.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
Alien
<<create>> Alien()
So we’re all set … at least for handling life points, right?
![Page 7: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/7.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
Alien
<<create>> Alien()
Design change!Characters allowed to recover life points over time
![Page 8: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/8.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
recover(i : int) : void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
Alien
<<create>> Alien()
New Problem:Now Humans have a double advantage (armor + recovery)
![Page 9: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/9.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
Alien
<<create>> Alien()recover(i : int) : void
Another Design Change:Some aliens should be harder than othersThey should recover at different rates, not all at once
![Page 10: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/10.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
AlienmaxLifePoints : int<<create>> Alien()
recoverNone(i : int) : voidrecoverFraction(percent : double) void
recoverLinear(i : int) : void
Possible Solution:Multiple methods
Problems with this?
![Page 11: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/11.jpg)
Design Principle #1
You will spend more time maintaining your code than you will on the initial design.
Plan for change!
Identify the aspects of your application that vary and separate
them from what stays the same
![Page 12: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/12.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
AlienmaxLifePoints : int<<create>> Alien()
recoverNone(i : int) : voidrecoverFraction(percent : double) void
recoverLinear(i : int) : void
Create an interface for the things that vary
![Page 13: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/13.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
AlienmaxLifePoints : int<<create>> Alien()
Create an interface for the things that vary
RecoveryBehavior
calculateRecovery(current : int,max : int) : int
<<interface>>
![Page 14: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/14.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
AlienmaxLifePoints : int<<create>> Alien()
Create an interface for the things that varyCreate behavior classes that implement the interface
RecoveryBehavior
calculateRecovery(current : int,max : int) : int
<<interface>>
![Page 15: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/15.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
AlienmaxLifePoints : int<<create>> Alien()
<<interface>>RecoveryBehavior
calculateRecovery(current : int,max : int) : int
RecoveryLinear
step : int<<create>> RecoveryLinear(rStep : int)
calculateRecovery(current : int, max : int) : int
implements
RecoveryNone
<<create>> RecoveryNone()calculateRecovery(current : int, max : int) : int
RecoveryFractional
percent : double<<create>> RecoveryFractional(rPercent : double)
calculateRecovery(current : int, max : int) : int
![Page 16: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/16.jpg)
Design Principle #2
Exploit polymorphism in useful ways.
You can only inherit from one superclass
Program to an interface, not an implementation
![Page 17: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/17.jpg)
LifeForm
currentLifePoints : int
getLifePoints() : inttakeHit(i : int): void
Human
armorPoints : int
<<create>> Human(life : int,armor : int)setArmorPoints(armor : int) : void
getArmorPoints() : int
AlienrecoveryBehavior : RecoveryBehavior
maxLifePoints : int<<create>> Alien()
recover() : voidsetCurrentLifePoints(i : int) : void
<<interface>>RecoveryBehavior
calculateRecovery(current : int,max : int) : int
RecoveryLinear
step : int
<<create>> RecoveryLinear(rStep : int)calculateRecovery(current : int, max : int) : int
RecoveryFractional
percent : double
<<create>> RecoveryFractional(rPercent : double)calculateRecovery(current : int, max : int) : int
RecoveryNone
<<create>> RecoveryNone()calculateRecovery(current : int, max : int) : int
Has-A
![Page 18: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/18.jpg)
Design Principle #3
Avoid inheriting behavior
Favors flexibility
Favor composition over inheritance
![Page 19: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/19.jpg)
The Strategy Pattern
SuperClass
Behavior myBehavior
methodA()methodB()
SubClassA
methodA() <<override>>
<<interface>>Behavior
behaviorMethod()
Behavior_v1behaviorMethod()
SubClassB Behavior_v2behaviorMethod()
IS-A
HAS-A
Implements(also IS-A)
![Page 20: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/20.jpg)
The Strategy Pattern
Definition
The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes
them interchangeable. Strategy lets the algorithm vary independently from the clients
that use it.
![Page 21: The Strategy Pattern CSC 211: Design Patterns. Reading Quiz](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649cf05503460f949be9c2/html5/thumbnails/21.jpg)
Lab 2• Due Monday
• Submit your code and tests electronically
• Header the at top of every file you write/*** Description of this class* @author Your Full Name* Your section number*/
• ProjectName_LastName• Zip you eclipse project, name your zip file
» LastName_Lab#