Another WPF C# .NET project but for this one, the focus isn’t on WPF but rather the use of design patterns.
I had learned about them at University but only ever found myself applying a few of them. Now obviously you should only use a design pattern when the occasion would benefit from it but still, I didn’t like the fact that useful design patterns were fading from my memory because you never know, I might need them one day!
So I revised the common ones – specifically the ones in the Head First Design Patterns book. No offence to the “Gang of Four” but their design patterns book isn’t all too exciting to read as it’s not meant to be but it does mean that it can be a little more difficult to follow at times.
Going through the Head First book I couldn’t help but really enjoy the decorator pattern and it gave me an idea of a small simple project that uses it. The usual examples of decorator usage are a Starbucks-like cafe or Subway-like restaurant where you can really customize your order but I figured I’d go with a Pizzeria where you can choose a base pizza and add any number of toppings you want…yes…this is something that all Pizzerias do (well, at least most of them) but still, it’s the decorator design pattern in use!
One problem that the decorator design pattern introduces is that there’s a large number of small classes which means that some central class (where a lot of instantiation occurs) will be coupled to a large number of classes so I also decided to employ another design pattern – the factory method pattern. So instantiation is encapsulated elsewhere and my Pizzeria class only needs to know that it’s something of base type Pizza or PizzaDecorator abstract classes, it doesn’t need to know anything about any of the concrete classes.
As with all my projects, I am no artist and so please judge my work on its functionality as opposed to looks! Because for this project…my inability to draw is really showing! as you’ll see with the amazing pictures soon. Yes, the ham is just a pink diamond. Yes, it took me about 10 mins to draw it all. No! it wasn’t done in ms paint…it was done in paint.NET!
The whole decorating and factory-based instantiation is done from a class library dll so this can be used in any other application too, as shown in one of the pictures (the WPF application is just the front end). From the class library, you can create pizzas, decorate them with as many extras as you want in any order and you’ll be able to get a string description of the entire decorated object as well as its full cost by just calling two simple functions: GetDescription() and GetCost().
Also, as you add decorations to your pizza, you can also choose to remove them if you wish, in any order and the decorated pizza will be reconstructed properly. I think this is a nice addition as “undo” actions are typically a pain to do with the decorator pattern, specifically undo-actions that can occur in any order — one of the patterns disadvantages.
The initial starting screen. Notice how the extras are not selectable yet as you need to choose a base pizza first. The new order button resets the app back to its initial state and the order button finalizes it all, as long as you’ve selected at least a base pizza (in which case you’ll get a warning!)
Once you’ve selected the base pizza, the undo-able list on the side starts to fill and the total cost starts being calculated
After selecting a base pizza you can start to add extras. Whereas you can have only 1 base pizza, you can have as many extras as you want
However, I think this is too much bacon so let’s try clicking on the X button to some of the bacon extras in the list
Better! notice how in this pic and the previous ones that when we added (or removed) extras, the total cost was all re-calculated correctly, but just note – the app in the background is only seeing a single instance of an abstract base type and nothing more.
Clicking on order unfortunately doesn’t actually order a pizza for us, it does however utilize the GetDescription() method that each decorator instance can cascade down to the base pizza instance. There’s only one item, a decorated one, yet we get the full description and price of all the decorations and base pizza added together.
The WPF application is actually just the front end, it uses a class library I made which can be used in any .NET application type, shown here is the code working in a console application
One improvement I would make is that currently you can only order 1 pizza at a time. In a more realistic application you would be able to order and customize many pizzas that are added to a “basket”. However, this is just a case of making the functionality of this app into some inner functionality of another app that repeatedly uses this app’s methods and data structures, alongside a list structure for the “basket”. So it wouldn’t be too difficult but it also wouldn’t be showing off design patterns which was the intent of this project.
If you wish to have the source code for the project, please email me via the address listed on my C.V.