Can you please wrap the gift? It is for my grandma.

JAVING
Javarevisited
Published in
2 min readJun 15, 2020
Instructions on how to wrap a gift for grandma

Have you ever been in the situation where you had to buy a gift for someone’s birthday but you completely forgot until the very last moment? If this has happened to you, then you know how it feels. It does hurt, specially if that someone is somebody you love so much, like your grandma. It’s not a good feeling, but this things happen.

We live, we learn!

How would you solve it? Maybe you have just some time to quickly run to the corner shop and get something simple and small. You know grandma likes simple things.

I bet that’s what you would a probably done (you cheeky bastard!). Unfortunately, you know that It won’t work, everyone will notice that you forgot to buy a gift and you just bought the first crap you found on your way to grandmas birthday party. Everyone would notice unless you are a wrapping expert.

You can probably guess where I am heading with this story, right?
Correct! Today I will talk about the decorator design pattern.

You asphyxiated the kitten from earlier so you decided to get grandma an Iron. This time you decided to ask for a professional to help you wrapping it.

Professional wrappers are experts are decorating and making things look great. They do it so good because they keep practising the same pattern again and again.

The “Decorator” design pattern.
Imagine, you want to include some additional functionality to a method but you don’t want to have impact in what it is already doing. What you can do, is create a decorator class, that will wrap the original call to that method, and make all the clients call the decorator instead.

Step 1 — Extract an interface that exposes the function you want to decorate.

Step 2 — Create the decorator

Step 3 — Make the client use the decorator

Final note:
In this explanation I shown the decorator design pattern using Java inheritance. But it is also possible to do it using composition. Whenever you can, remember that you should prefer composition over inheritance. But more on that on another story.

I hope your grandma liked the gift. 🎁👵❤️🐈

If you liked this story, please give us some 👏and don’t forget to follow Javing

Originally published at http://javing.blogspot.com/2013/10/can-you-please-wrap-gift-it-is-for-my.html

--

--

JAVING
Javarevisited

The present continuous form of “to program in Java”.