The Bitter Coder Tutorials, Binsor Style: Part XII, Decorators

Wed, Oct 1, 2008 One-minute read

Here is our Order and OrderItem classes:

Let’s wire up the Binsor:

We put the GST calculator first, so it’s the default implementation, and run the program:

Cost to deliver Order 1: 123.7500 Cost to deliver Order 2: 10.0

So, the US one hasn’t changed, as expected.  Finally, let’s add a shipping calculator:

Cost to deliver Order 1: 211.2500 Cost to deliver Order 2: 260.0

As Alex points out, we are calculating our GST before shipping, which is kinda stoopid.  The Decorator Pattern saves our bacon here, as we can just swap things around (oh, and we change some parameters too):

Run it one more time:

Cost to deliver Order 1: 211.2500 Cost to deliver Order 2: 260.0

So, that’s that.  Alex has some great comments in his post about other stuff you can do with the Decorator pattern, so I highly recommend you read his all the way through.

Next time we’ll do this stuff without decorators….