Assignment 4

I decided to use the decorator pattern to increase the speed of the prey organism every time it is captured.  This is done by using the abstract class “SpeedDecorator”, which is extended by the classes IncreaseSpeed and DecreaseSpeed.  I would like to be able to use these decorators on both predators and prey, but making the class SpeedDecorator extend organism didn’t allow me to apply them to specific instantiations of predators or prey.  For that reason I made SpeedDecorator extend prey.  If there is a way, please let me know.

I think I will end up using the decorator class for many changes of the organisms.  Since I want them to evolve, I will need an effective ways of changing their parameters at runtime.  The current program uses the observer pattern to make the predator chase the prey and the prey run from the predator.  After the predator catches the prey, I use the speed decorator to increase the speed of the next instantiation of the prey.  Eventually the prey becomes too fast for the predator to catch.

To use the decorator pattern, start with an abstract class.  This is the abstract component and the methods are abstract.  Then make a concrete class that extends this component with concrete methods.  This is the class that is going to be decorated, which means we will add new behavior to it with decorators.  The decorator class itself will extend the component and is also an abstract class.  This class must have an instance variable that holds a reference to the component it extends.  Then we must create concrete decorators that extend the abstract decorator.  These must have an instance variable for the class that they decorate.  These decorators are what will extend the state of the component. 

Now we can use these decorators on our concrete components.  Since these concrete components are wrapped by decorators, the decorators can use the instance variables to get all the information from the objects they decorate and use their own methods to decorate it.

In my code, the decorators hold reference variables to prey objects and for that reason have access to the prey’s speed variables.  Then the decorators can add to the speed variable every time they are called.  I think I will eventually make it so that organisms in a healthy state get bonuses to their speed, vision, or defense.


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 )

Google photo

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

Connecting to %s

%d bloggers like this: