SE2811
Software Component Design

This is an old version of this course, from Winter 2014-2015. A newer version is avaliable here.

The objective of this lab is to refactor the Angry Bees simulation such that both the Game and GameCharacter classes encapsulate behaviors using the Strategy Pattern, and incorporate the Simple Factory Idiom into the Bee and Flower classes to create those characters.

Assignment

Download the TagBehavior.java and MoveBehavior.java files containing the interfaces that define the MoveBehavior and TagBehavior strategies. Since these are just interfaces, you'll have to provide the implementation of the concrete strategies in concrete classes. You will need to create 4 classes that implement the MoveBehavior interface; these are

  • RandomMoveBehavior - as demonstrated in lecture, this class implements the random move planning behavior originally implemented in the Bee class.
  • StationaryMoveBehavior - this is a "null" behavior that Flowers use.
  • GreedyMoveBehavior - this is the behavior which causes the character using this behavior to move to the Flower with the highest number of points.
  • AlphaMoveBehavior - this is the behavior which causes the character to move to the flower with at least 4 points that is first in the alphabet.

You will need to write 2 classes that implement the TagBehavior interface; these are:

  • StandardTagBehavior - this is the behavior originally implemented in the Game class.
  • AdvancedTagBehavior - this is a new behavior that should implement an algorithm similar to StandardTagBehavior, but also:
    • when a Bee tags another Bee, 20% of the time this game should "steal" half the points from the tagged Bee and award them to the tagging Bee (use a random number generator to do this efficiently), and do nothing the other 80% of the time. (This requirement updated for clarity -- no change in the actual requirement.)
    • when a Bee tags a Flower whose name is "nightshade", 50% of the time the game should change the behavior of the tagging Bee so that it subsequently uses RandomMoveBehavior and award the Bee 0 points (although the Flower's score should decrease to 1 point as usual). The other 50% of the time, no behavior change should be made, but the Bee should still be awarded 0 points along with the Flower's score reducing to 1 point.
    • when a Bee tags a Flower whose name is "daisy", the game should award points as usual, but also change the behavior of the tagging Bee so that it subsequently uses AlphaMoveBehavior.
    • when a Bee tags a Flower whose name is "rose", award points as usual and change the behavior of the tagging Bee so that it subsequently uses GreedyMoveBehavior.

The class diagram representing this application's structure after the lab is as follows (the different colors serve only to differentiate strategy interfaces from strategy implementations):

To create Bees in this version of the game, note that you must modify the Bee class to implement a factory method1 named createBee(), which handles the creation of the Bee itself as well as a specified behavior (supplied to createBee() as an enum). Similarly, to create Flowers, you must modify the Flower class to implement a createFlower() factory method.

In the Game class, you must modify Game to use the TagBehavior interface, create an instance of StandardTagBehavior in the main() method, and set the tagBehavior attribute to initially reference that instance. The Game.tag() method thus initially uses StandardTagBehavior whenever that method is called from a GameCharacter's finishMove() method. You must modify the Game class's runGame() method to switch to use AdvancedTagBehavior when the remaining time is 80% of the original game time. No special factory method is needed to create or switch these behaviors.

Acknowledgement

This lab developed by Dr. Mark Hornick.

The Fine Print

1Watch out! This Factory Method is part of the Simple Factory Idiom, not the Factory Method Pattern