But first, what is a design pattern?
Do I need to know design pattern?
Before we get into to the meat and potato of this article, I know what you’re thinking. “C’mon Atsushi ¯\_(ツ)_/¯, I don’t NEED to learn any design patterns. My code works! and great shirt btw” Thanks, and you are not wrong. I know your code is utterly beautiful as of now.
But think of it like this. Imagine that you are trying to build a house. You could build it by putting together woods based on how you think a house should look like, and I know you are smart so it would probably work. However, you might find it hard to change part of the house after a while since many things might be depended on each other unintentionally.
Instead, let’s say your architect friend Joe comes along to help you build the house. Joe is a nice guy, so he draws a diagram on how the house should be build based on his experience. Once the house is build, you would find it easy to manipulate part of the house since you have understanding of how changing one part affect others. After enough time, you can change the house to be what ever you want it to be with less hustle!
The planning diagram that Joe made is exactly what design pattern is! It gives you a roadmap to build a robust software from other programmers experiences that makes your code easy to manipulate and scale for everyone!
- Design new applications quickly and efficiently
- Follow the solution proven right by programmers before us
- Understand the structure of other programs
- Communicate expressively with other programmers.
To sum it up it is almost like a ….
Which pattern should I learn?
Awesome! Learning Design pattern sounds like a great deal eh? But now the questions is, which one should you learn???
There are lots of design patterns out there. Important thing to keep in mind is that there is no design pattern is one size fits all. With that being said, there are some patterns that are applicable to many cases.
Please allow me to introduce - Module Design Pattern!
Why are private variables and methods good? These StackOverflow answers gives a great explanation than I ever could!
To summarize above answers:
- Having private variables protect them from being manipulated outside of its class unintentionally.
- Private methods keep the functions that will only be used inside the class. Hence, hide what the world out of the class doesn’t need to see.
- Less clutter the global namespace
Diving Into The Code
Enough talking! Let’s dive into the code. Let’s say we’re making a program that keeps track of how many people are in the movie theater!
Pretty cool right?
As you can see, “count” variable is kept inside pplCounterModule class, so the outside world can’t access it or manipulate it. Because you know that the only way to manipulate the variable is through its public methods, it’ll be super easy to debug!
The Revealing Module Pattern
Now that we’re on a roll, let’s give it a go at another pattern - The Revealing Module Pattern
As you might have been able to tell from the name, it is very similar to Module Design Pattern except one small part - everything is defined privately!
Notice the difference?
This one gives couple extra benefits:
- Cleaner code
- The public methods name can be simple, since privately defined methods can have descriptive function names instead.
- Easier to maintain which method is public or not
Aaaand that’s a wrap! You can pat yourself on the back. Go tell your friends that you now know TWO design patterns!
There are many, many other cool patterns out there.
Till next time, chao!