Question: Exercise - Behavioral Patterns For this first exercise you ll be focusing on a set of classes that represent pets. The main classes and their
Exercise Behavioral Patterns
For this first exercise youll be
focusing on a set of classes
that represent pets. The main
classes and their members can
be represented in a UML class
diagram shown
In this exercise, youll be
extending and then refactoring
the codebase to explore how
behavioral patterns can
simplify the process of adding
new functionality, and can
remove the need for duplicate
code
Exercise a The Strategy Pattern
Add a new Pet class CuddlyToy that requires new algorithms for
the growth, feeding, hunger, and crying
Consider how one might use subclasssuperclass relationships to
avoid duplicate code
Implement an abstract GrowthStrategy that provides method
signatures for growthrelated algorithms
Implement the three concrete implementations of
GrowthStrategy encountered so far
Modify the existing Pet classes to use the newly created strategy
classesStage Add a new Pet class
Youve been asked to add a new Pet, CuddlyToy, for
players that for example have allergies or just dont
want the effort of looking after a reallife creature. The
requirements for CuddlyToy are as follows:
A CuddlyToy should not grow, they are ADULTSIZE at
instantiation
A CuddlyToy does not eat, and should not get hungry
A CuddlyToy squeaks, its cry is generated by a plastic
squeaker
Stage Design subclasssuperclass
relationships to avoid duplicated code
Its clear that many of our Pets have
quite different algorithms for growth.
Some, like Goats and Cats grow
steadily, increasing by a fixed amount
over a constant time interval. Others,
like MagicDragons, increase by a fixed
amount but at irregular intervals
their growth stagnates for a while and
then they undergo a growth spurtStage Introduce a GrowthStrategy
A Strategy pattern defines a encapsulates a family of
interchangeable algorithms here, our
interchangeable algorithms describe different patterns
of growth.
The first step in refactoring to a Strategy will be to
create an abstract class GrowthStrategy.
ACTION Create a new GrowthStrategy class, with
abstract method signatures for canGrow and Grow
Stage Implement concrete growth
strategies
You now need to create concrete implementations of your
GrowthStrategy class, each representing a different growth
algorithm. So far, weve encountered three growth algorithms:
Steady growth Grows by a fixed amount every time the grow
method is called.
Random growth Grows by a fixed amount some random subset
of times that the grow method is called.
No growth Does not grow, even when the grow method is
called.
ACTION Create three subclass implementations of
GrowthStrategy, one for each of the growth algorithms
encountered so far.
Stage Modify the codebase to use
our GrowthStrategy
Now we have a selection of implemented GrowthStrategy
classes, we need to modify the Pet subclass to utilise these new
classes. To do this, well add an attribute growthStrategy of
type GrowthStrategy to the Pet class. Well also need to add a
set method for the new attribute, and modify the existing
canGrow and grow method in Pet and its subclasses to
make calls to the new strategies.
ACTION Make the remaining code changes needed to have Pet
and its subclasses use GrowthStrategy. This should now mean
that there is no specialcase grow implementation in
MagicDragon and CuddlyToy
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
