Decorator Pattern Recipe

1.) First of all, its important to understand that the decorator pattern is a very permutation oreiented pattern.  The decorator pattern attatches additional responsibilities to an object dynamically.  So the first thing you must do when implementing the decorator pattern is figure out which of your objects are concrete (static non-changing classes) that you want to decorate(add functionality or features to) with any number of classes or permutations there of to those concrete classes.  For instance you might have a car program where you have an abstract car class that gets inherited by hatch_back, sport_car, muscle_car, and convertable classes.  Then there are all sorts of features that anyone of these car subclasses can posses such as white_wall_tires, 20_inch_rims, tinted_windows, candy_paint_job etc or any combination of the features.  Those features are what you will be decorating your concrete  hatch_back, sport_car, muscle_car, and convertable classes with.

2.)  The next thing to do is build an abstract decorator class that inherits from the same superclass that your concrete classes do.  To follow the car program example your decorator class would inherit from the car superclass.

3.) While still with in your abstract decorator class you must think about which functions that your concrete classes use to add on more functionality and you must reimplement an abstract version of those functions in your abstract decorator class. Again to follow the car program example you might want to reimplement the function that draws all your features onto your car.  This will allow your subclasses of your abstract decorator class to add more functionality to that method.

4.)  The next thing to do is build some classes that inherit from your decorator class and add functionality to your concrete classes.  To follow the car program example you would want to build your white_wall_tires, 20_inch_rims, tinted_windows, candy_paint_job feature classes and have them inherit from the decorator class.

5.) While still within your subclasses you must alter whatever method that adds functionality to your concrete classes so that it actually adds on the functionality.  Again to follow the car program example you would want to implement and alter the draw method so that it adds the whitewall tires, tinted windows etc.

6.) While still within your decorator subclasses you must make an instance of your orginal abstract class or interface that everything inherits from and make your constructor of your decorator subclasses accept a parameter of an instance of that object.  The reason for this is that you will need to call whatever method that you are altering in your decorator classes frin within that very same method in your decorator class.  To follow the car example you would need to make an instance of your orginal abstract car class within your decorator subclasses (white_wall_tires, 20_inch_rims, tinted_windows, candy_paint_job ) and create a parameter of type car within your constructor for that class.  Within your constructor you will want to copy over your car argument into your local instance of the car object.  This will give you a handle on the live car object at runtime.  Then within your draw method in your decorator subclasses you will want to call your draw method from the instance of your car superclass.

7.) Thats basically the whole structure of the decorator pattern.  The next step is to actually use it in your driver class.  To use it all you have to do is create an instance of your original superclass construct it with one of your concrete classes and then construct that same object with any number or combination of your decorator classes.  This works because both the decorator classes and the concrete classes are all inherited from the same super class.  To follow the car example you would want to do something like this:

car car1 = new hatch_back(();

car1 = new white_wall_tires(car1);

car1 = new tinted_windows(car1);

car1 = new 20_inch_rims(car1);

So now u can add any combination of features with just a constructor in your driver.  That is the whole concept of the decorator pattern.

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: