Strategy Pattern Recipe

1.)  Seperate sections of your code into 2 parts: the things that vary and those that don’t.

2.)  Place the parts that don’t vary into abstract classes.  These will become blueprints for  your classes that will envolve more detail and varied behavior.

3.)  From your abstract classes you should now extend that behavior into other classes that contain your chunks of code that vary.  In other words, you should place your chunks of code that vary into their own classes and extend your abstract class into these classes.

4.)  From here you should be able to utilize interfaces to add individualized functionality to your subclasses that were built from your abstract class.  In other words,  make some interfaces for your customized methods that you want your subclasses of your abstract class to be able to utilize.

5.)  Create some new classes that will implement your interfaces.  In other words, define your methods that your interface laid out the blueprint for.

6.)  Now that you have created your interfaces and defined methods for those interfaces in other subclasses of the interfaces you can create instance variables of those interfaces in your original abstract class.  This will allow all your subclasses of your abstract class to access their own individualized methods at run time.

7.) Before compiling your program, the last thing you have to do is go back into your subclasses of your abstract classes and instantiate your instance variables that you created in the previous step.  In other words, create constructors for your instance variables of your interfaces in your subclasses of your abstract class.  This will specifically tell your subclasses what  behaviors they should assume when their methods are called.

Advertisements

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 )

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: