Last week I had the opportunity to give a course entitled “Introduction to Object Oriented Programming and UML with Java.” I was in front of 10 students coming from very different backgrounds, aged from 20 to 40.
If you’re reading this, big up to you guys! Free coffee & Mario Kart did it all.
A big challenge was to find consistent examples to explain design patterns with simple words that everyone could understand.
Believe it or not, pizzas are much more than food.
This article will guide you through some very useful design patterns widely implemented, explained with pizzas and one-file snippets.
Design Pattern Observer
Running a pizza business isn’t easy and one of the difficulties that come with managing food is ensuring that your food is still fresh for customers.
In real life, pizzas won’t tell you they’re out of date.
In programming they will.
Margherita is an
Observable : Something another object can observe.
Chef is an
Observer : An object that can observe another.
Margherita can have multiple Observer using
setChanged() will let
notifyObservers() happen. Not using this function will make
notifyObservers() have no effect at all.
notifyObservers() can take a parameter often used to describe what kind of change happened.
update() is called with two parameters. The modified
Observable instance and the optional details parameter from
Also called : Listener / Handler.
Design Pattern Decorator
Running a pizza business involves managing different kinds of pizza like Margherita, Marinara, etc.
Still, most companies let you make your own custom pizza.
This is how it works both in real life and in programming.
Pizza is an object we want to add attributes / methods to, dynamically at run-time.
There is a base class
Pizza that holds another
Pizza given in the constructor with parameters.
There are child classes extending Pizza :
WithGarlic | WithOnions | WithPepperonis . These classes are instantiated using the constructor with parameters.
Those child classes do only one thing: overriding the
cost() function to get the base cost and adding an extra cost on top of it.
The important thing is that : As a
Pizza can hold another
Pizza that holds another
Pizza and so on, the total cost is computed in a cascade style using
Usually there are 3 kinds of implementation of the design pattern Decorator :
- Using inheritance and composition, just like I did. Semantically, telling that a pizza contains another pizza is unbelievable, yet we’re in programming and this kind of thing happens very often.
- Using interfaces : More flexible as Java doesn’t support multiple inheritances, yet it supports implementing multiple interfaces. Since Java 8, interfaces can hold default methods easing this process. Still do I believe that implementation should be done by actual objects. Also to note that interfaces can’t hold instance members.
- Using inheritance, composition and abstract classes : Like I did, making
Pizzaabstract. It would ensure that no one could write
Pizza p = new Pizza(new Pizza())which would be a total nonsense as there can only be one very base.
Each kind of implementation has pros and cons, but I think the one I provided is definitely the most straightforward and easiest to understand.
Looking at how online tutorials are designed, I think there are too much complex boilerplate code a complete beginner would struggle with.
Design Pattern Decorator lets you dynamically add attributes and functionalities to an object at run-time, without using big if-else statements and without changing the base class behavior. A Java use-case is
IO with InputStream | BufferedInputStream | FileInputStream, etc
I hope this article has been valuable to you and helped you to understand those simple and frequently used design patterns.
Pizzas turn out to be very useful at explaining programming stuff, just like concrete examples help understanding conceptual notions.
I had a very nice time explaining those two design patterns during my class, illustrated with ice creams and game characters.
Wondering how to run my snippets ? Use this Online Java Compiler
Thanks for reading
My articles take root from your claps and encouragements.
I’m making my best at maintaining a 1-article-per-week basis, and if you weren’t that many readers reaching out to tell me what was great / awful, I wouldn’t be there.
This is my latest story:
Feel free to reach out at email@example.com.
Fullstack Developer, Trainer & Entrepreneur.
Learning stuff, sharing knowledge and building on top of great ideas are my top priorities.