Assignment 3

To implement the observer pattern you need to extend the observable class and implement the observer interface (you can use java’s or design your own).  The observers, obviously, observe the observables.  In my program I made organisms (which includes predators and prey) as both observers and observables, as they would be in real life.  Next, you need the observables to contain a method that notifies the observers based on some change.  My organism class notifies its observers based on position changes. 

 Now the observers need an update method that will respond to the information “pushed” by the notify observers method.    In my program the predator and the prey each have different reactions to being notified that their observable has changed position.  The predator chases the prey and the prey runs away.  If the predator is notified that the prey is within a certain distance, the predator calls feedBahavior and the prey’s state is set to dead – this represents the predator initiating the action of killing and eating the prey.

 Once the notifyObservers() and update() methods are correctly set up, all that remains is to add observers.  The easiest way to do this in my program was simply to base it on proximity – when two organisms are within a certain distance of each other (before this they move about randomly), they become mutual observers and observables.  At this point, the chase begins.  If they get far enough away, the chase ends.

At the moment the predator catches the prey over and over again each cycle.  I will have to change the parameters to make the prey faster or it will never be able to reproduce.


Leave a Reply

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

You are commenting using your 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: