Homework 4

Homework 4 consists of taking a software program and applying two design patterns to its design to enable new functionality.

The Program: SpriteWorld

When you run the original Java program, a window opens and three sprites appear; two begin to animate, the third is under user control. One of the sprites is highlighted; clicking the tab key causes the highlight to switch among the sprites. You can download the source code of the original program from the Sample Code page on this website.

The Changes

Our clients have supplied a fourth sprite that they would like to have included in Sprite World. However, this new sprite, known as RedSprite, does not follow the interface provided by the Sprite interface that is the basis for SpriteWorld. Our clients are rather proud of the code they wrote for that class, so they forbid us to change it in any way. You will need to use the Adapter pattern to incorporate RedSprite into SpriteWorld.

Our clients also think that it is weird that the visual highlight of the sprites carries no semantic meaning. They have asked us to change the program such that the highlighted sprite is under user control. You still switch between sprites by clicking the tab key but now you control the sprite that is highlighted. When asked what a sprite should do when it loses the highlight, our clients said that "they should go back to what they were doing before they were highlighted". Our clients provided a mock-up of how the desired program should behave when complete.


  1. Draw a UML class diagram of the original system. (10 points)
  2. Apply the adapter pattern to incorporate the red sprite into the system. Note: You are not allowed to modify the RedSprite.java class in any way. (10 points)
  3. Apply the strategy pattern to allow user control of the highlighted sprite. See the hints below. (20 points)
  4. Draw a UML class diagram of the final system (10 points).
  5. Create an archive that contains the source code of the final system, your two UML diagrams and a README that describes the state of the final system, the amount of functionality you provide and anything else that you think we should know about the system.

Extra Credit

There are several ways to earn extra credit on this assignment.

  1. Add a new sprite to the system beyond the RedSprite. You'll need to supply the images and the movement/animation logic for this new sprite. (5 points)
  2. Define a completly new movement behavior and make it be the BlueSprite's default movement behavior. In the original system, BlueSprite was under user control. In the mock-up created by our clients, the BlueSprite defaulted to the NorthSouth movement behavior. Give it a new default behavior. (5 points)
  3. Find an opportunity to apply one more design pattern to this system. Be sure to document which design pattern you added in your README and be sure its components are visible in your final UML diagram. (10 points.)


  • When applying the strategy pattern, you will likely be creating a class or interface that is called MoveBehavior. This interface (or class) needs to define a method that looks like this: public void move(Canvas c, Sprite s). You will discover that you will need to pass a pointer to the Sprite when extracting the move methods from the original set of sprites into the new concrete move behaviors.
  • In the original system, the BlueSprite implemented the KeyListener interface and the Test program submitted the BlueSprite to the Canvas's addKeyListener() method. Now that all sprites (except the RedSprite), can come under the control of the user, it no longer makes sense to have the KeyListener methods located in BlueSprite. You should instead consider moving that logic to the Canvas object, as it is the object responsible for handling which sprite is highlighted at a given time.
  • The focus of this assignment is on the ability to apply the design patterns. You should not muck with the details of the javax.swing code. For the most part, you will be refactoring the original system as you restructure it to fit the design patterns as opposed to making any major changes to the way it interacts with Swing.

Language Issues

If you do not want to complete this assignment in Java, then you must attempt to recreate the original system in your language of choice. Reuse the images that come with the original system. They are transparent .png files and all modern programming frameworks should have an Image class that will work with them. When recreating the RedSprite, the important thing is that your class have the same interface as the original RedSprite and that it should NOT depend on the Sprite class in any way. You will then need to create an adapter so that your version of the RedSprite class is incorporated into your final system via the adapter design pattern.

Due Dates

Homework 4 is due by 11:59 PM on Friday, February 25, 2011. This is a hard deadline, as I will release a solution to Homework 4 on Saturday, February 26, 2011 for students to study in prepartion for the midterm on Tuesday, March 1st. No late assignments accepted!

You are encouraged to work in teams on this assignment; it is not required, just encouraged. Get started soon!

© Kenneth M. Anderson, 2011