CSE 115 - Fall 2006 - Banner
   CSE 115 - Fall 2006 - Introduction to Computer Science for Majors I
CSE 115 - Fall 2006 - Left Navigation CSE 115 - Fall 2006 - Lab 7

Lab 7

Notice

This lab is a modified version of a lab originally developed at Rice University.


Introduction

We want to study the behavior of fish in an aquarium for some biological research. When we create a fish, we want it to exhibit a particular type of behavior as it goes around the screen. We want to observe the fish behaviors to see what will happen when we get lots of fish in the tank. We also want a way for a fish to exhibit a combination of more than one behavior. Maybe as it swims in a circle, it also needs to emit a strange radioactive glow. We'll concentrate on behaviors that are similar to this, and some that you might not expect fish to have!


Working From Home? Click here for any special directions


New Concepts Covered

The following are the new concepts covered in this lab.
  • Delegation
  • Objects as Behaviors (Strategy)
  • Null Object
  • Composite (Revisited)


Assignment Specification

You will write an Applet with two menus that will allow the user to create different kinds of fish in their fish bowl.

From the first menu, the user should be able to select from a menu the following types of fish:

* Regular - a fish that swims across the screen.
* Chameleon - a fish that changes colors randomly as it swims about the screen.
* Dizzy - a fish that spins while it swims.
* Random Swim - a fish that moves in a random way each time it is ready to move.
* Surprise - an opportunity for extra credit (should you choose to accept it).

The second menu allows the user to select any combination of Chameleon, Dizzy, or Random Swim and create a fish with that behavior.

When a fish is created, it should be created with a random color and move according to the behavior selected for it. Note that the cse115.utilities.Random class and its randomColor method could be extremely useful here.


Helpful Hints

Before you start programming, look over the notes from the lectures. Very special (not-so) secret tip: review the lectures on Interfaces, Polymorphism and Design Patterns before beginning this assignment. If you don't understand something that is covered in that lecture, see your TA or instructor during their office hours.


Preparatory Work

In order to be able to carry out the tasks required during your lab session, you must come prepared. Do the following before coming to your lab session:

Reading

Make sure you have read up to chapter 5 in the text.


Lab tasks

At your lab session your teaching assistant will briefly discuss how to carry out each of the tasks below. She or he will also be available to answer questions you might have. You must carry out each of the following tasks.

Get the project skeleton & disconnect from the repository

Get the Lab7 project skeleton from the LabSkeletons repository and disconnect it from the repository.

 

Lab Design

Once again you are required to design your solution to the lab. Create a file called Lab7.dia in your project and submit it with your jar file.

The assignment specification gives you a hint as to what classes you'll need. Recall the first step is to ask, what has already been written for me?

Once again, remember to design and code iteratively.


Objects as Behavior

Many people make the mistake of thinking that objects can only represent data. However, objects are used to model concepts both tangible and intangible. It is important to remember that we can just as easily represent processes with objects as we can simple data. Once we free our minds from the self-imposed prison, we can begin programming at the correct level.

Some words of inspiration for our journey from Stephen Wong:

To quote from Gamma, Helm, Johnson and Vlissides' (known in the industry as "The Gang of Four") "bible", Design Patterns, Elements of Reusable Object-Oriented Software, design patterns

...are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.

A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design.  The design pattern identifies the participating classes and instances, their roles and collaborations, and the distribution of responsibilities.

Design patterns are the biggest breakthrough in object-oriented design in the last twenty years and arguably the hottest area of modern OO research. 

But design patterns are hardly just just a theoretical endeavor.  Design patterns have their roots in industry, in the ways that programmers "in the trenches" have found through much trial and error to solve common design problems.   Design patterns take these concrete solutions and abstract their behaviors and structure so that we can use them to understand and solve a wide variety of problems.

At the heart of a design pattern is often a UML diagram that shows the classes involved in the pattern.

The Strategy Design Pattern

The strategy design pattern  is a way of abstracting behavior and capturing that behavior in an object.   An embodiment of the classic tenent of separating the invariant and variant behaviors of a system, it enables one to break one's design into two parts: 

  1. The "context" part of the design deals with the behavior at an abstract level.  That is, it doesn't care exactly what the behavior does, only that it does it.  The behavior is thus referred to as the "strategy" for accomplishing a task.   The context is the "invariant" part of the design in that its behaviors do not change from situation to situation.   Thus it can be encapsulated into a single object.
  2. The "strategy" part of the design captures the "variant" nature of the design where the particular actions taken when accomplishing a task change from situation to situation, perhaps dynamically as the program is run.   The strategy section consists of an abstract strategy class and a series of concrete strategy subclasses.   Each subclass represents are possible specific action that could be taken when the strategy is executed by the context.    The variant nature of the design has been abstracted and captured in a tree hierachy.

The context deals with the strategies at the abstract strategy level.   The abstract class contains an abstract method that that the context believes that it is executing.   In actuality, at any given moment, the context is holding one of the concrete subclasses.   Thus when the strategy's method is called, the resultant behavior is that of the concrete strategy being held.

The strategy pattern is one of the most fundamental and common design patterns and forms the basis for many other patterns.  One is well advised to know it well.

The biggest benefits of the strategy pattern are

  1. A clean delineation between the variant  and invariant components.
  2. The ability to dynamically change the behavior of a system without  explicitly changing classes.

Classic scenarios where the strategy design is used are to change the language used in message windows depending on what country the program is being run in.   Or changing the behavior of a button depending on what options had been previously selected.  Netscape plug-ins are an example of a strategy pattern.


Strategy Fish

Let's look at what all fish have in common - they can demonstrate a certain behavior. Each behavior described above are unique from the others, but in all cases, each fish can exhibit any one or multiple of the behaviors. This idea is encapsulated in the fishbowl.strategy.Fish class. It has the ability to set it's current behavior. You can set the behavior of a fish to be anything that implements the fishbowl.strategy.IFishBehavior interface.

Let's start with a simple fish, the Regular Fish. This fish is simply a fish that has no particularly interesting behavior. It simply swims around the screen. Now, fishbowl.strategy.Fish already have the ability to swim around the screen built into them, but they rely on their IFishBehavior to tell them additional information about what they are supposed to it. Simply creating a fishbowl.strategy.Fish and ignoring it's IFishBehavior will cause a runtime error in your program. Try it out for yourself and see.

If you do not set the IFishBehavior of the Fish and simply create it, a NullPointerException occurs. Recall that a null pointer exception occurs when you are trying to call a method (or methods) on a reference that does not refer to anything. Our fish's behavior is not referencing anything. However, what kind of value should we give the behavior of the Regular Fish. What type of additional behavior would a regular fish have?

Well actually a regular fish would simply swim around like any normal fish and do nothing special. But this doing nothing is just as valid as the other types of fish behaviors, so we'd better provide a class for it. Create a do nothing class that implements the IFishBehavior interface. It's method (from the interface) should have an empty definition -- the true definition of doing nothing. The class you just created is an example of the Null Object design pattern. While it was omitted from the Gang of Four book, it is incredibly important and powerful. It allows us to give to represent nothingness in our system in the same way we are representing doing something.

This is probably a good time to test things out. Once you know this works, you can write your other behavior classes and hook them up in your class that is used by the Single Fish menu.

Some notes about Behaviors

Here are some helpful hints so you can create the other behaviors for your fish.

  • Cameleon Fish - Uses that pesky cse115.utilities.Random class' randomColor() method every time it updates.
  • Dizzy Fish rotate around while they are swimming. Note the setRotation() method that is available on the fish.
  • Random Swimming fish move a random distance (either forwards or backwards) each time they move.
  • Surprise fish is an opportunity for you to earn extra credit by using your imagination and creating a fish that has behavior that is nothing like what we have previously described for you. The TAs will have some ideas in recitation for you that you can implement or you can come up with an idea completely on your own. Please note that simply a combination of fish behaviors will not be satisfactory for this extra credit. You must come up with a behavior that is distinct from ones we've described in this lab.

Composing Behaviors

We've done composite already in Lab 6, but here we are going to do it so that a fish can take on a combination of two of the behaviors from our menu. The composite in this case will know two objects of type fishbowl.strategy.IFishBehavior and it will implement that interface as well. It's update() method simply calls the method of both behaviors it knows. Getting this class working is the key to making the Multi Fish menus work. The TAs will discuss in recitation how to leverage this pattern to make the menu work fully.


Classes' Public Interfaces

We have provided documentation for the fishBowl.strategy package and the cse115.utilities package in Javadoc format.


What you hand in

Once you are ready to submit, export your solution as a jar file, just as you did in previous labs, but name your jar file Lab7.jar

Use the electronic submission program that corresponds to your recitation. For further directions on the submission program, please refer back to the earlier labs.


Due Dates

Due 11:59:59pm the day before your recitation meets the week of October 30th, 2006.

 

CSE 115 - Fall 2006 - Footer

 

 
Last modified: Fri Oct 20 07:35:21 2006