Home > Assignments > Problems implementing the Observer Pattern

Problems implementing the Observer Pattern

At first it was unclear as to where I should apply the Observer Pattern, but after deciding on how I was going to implement the various objects in my game it became more clear. I first created a class hierarchy for my spaceships, then I decided that I wanted my ships to be able to observe other ships, but when I began to implement the Observer Pattern I ran into the single largest pitfall of the Observer Pattern: your class must extend Observable. This was a problem as my ships were already extending classes. So I decided I needed to redefine how I implemented my spaceships in the game anyways, and went ahead and removed the Observer Pattern as well as redesign my ship hierarchy. Now, I have a base class Ship that extends Observable and implements Observer. Problem fixed. The relation between the Ships utilizes the Observer Pattern by allowing the Ships to communicate between each other their specific location. When you two Ships are about to engage in combat, they observe one another, and as the combat goes on the Ships drift around in space, so their locations change, and if the opposing Ship is locked on (observing), then they receive an update of that Ships location. Ships may choose to attempt an escape, where they disengage from combat and fly away. The opposing Ship may choose to follow them. Once again, the locations of the ships change and the observing Ships get updated on this change.

My second implementation was between the class Radar and the class Asteroid. Every Ship in the game will have a Radar, which will performScan() and pickup any nearby Asteroids. The movement of the Asteroids varies; some are stationary while others float through space. As a Ship locks on to an Asteroid, it may shoot at it to destroy it, or try to avoid it. Since the location of the Asteroids may change, the Ship will be updated on this change. This seems to provide a nifty life-like feature: your Ship detects an Asteroid field ahead moving your direction; do you try and blast your way through, or attempt to outmaneuver them?

Unfortunately, the code that I turn in does not do very much. Yes, it exemplifies the Observer Pattern but it is not practical code. I have to correctly design my Ship hierarchies and implement them before I can properly use the Radar/Asteroid relationship, as well as the Ship/Ship relationship.

Advertisements
Categories: Assignments Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: