Design Pattern : Why You Would Probably Need One

Photo by Susan Holt Simpson on Unsplash

“What’s the big fuss? I wrote my own code and i knew exactly what to fix if something is broken”

Some of us may think of the statement above when being challenged on why they write such kind of code, in fact it is true that the one who wrote the code supposed to know what is wrong, but mostly in practice, developers work in teams, which means that you work together with other people to build the app, you have to also consider on how your code could impact the app’s readability & maintainability so that any other team member could easily help you when something went wrong, i’m quite sure you don’t want to make the entire team searching for that one particular bug you caused in the massive haystack of the app. 😜

“Er..Surely no! well then, what can i do about it? How to improve my code quality?”

Alright then, the very first thing you need is of course strengthen your basic knowledge of the language you’re coding with, after that you can use a powerful tool called design pattern to accompany you on your coding journey.

WHAT IS DESIGN PATTERN

Quoted from SourceMaking, a design pattern is defined as below

“A general repeatable solution to a commonly occurring problem in software design. A design pattern isn’t a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.”

Photo by Magdalena Kula Manchee on Unsplash

“………Wait..what?”

In a simpler words, design pattern is a guide, or a concept ( often visualized with diagrams ) that could assist you when code and teach you how to think and build a clear, loosely coupling code.

“The question is, does it work? YES it is working but does it efficient enough? “

Probably not, because the logging method uses the same class, which in this case the Logger class, if there is 10 menus or modules in the app, then the developer should have at least 10 Logger objects, which is not the best practices ( although it works ) since they are all doing the same thing and surely 10 objects is consuming more space in memory than a single object, then what can we do about it?

  • Ensure a class has only one instance, and provide a global point of access to it.
  • Encapsulated “just-in-time initialization” or “initialization on first use”.
  1. We made a class named Logger
  2. We then create a companion object of the class. In Kotlin, if you want to write a function or any member of the class that can be called without having the instance of the class then you can write the same as a member of a companion object inside the class. So, by declaring the companion object, you can access the members of the class by class name only (which is without explicitly creating the instance of the class). You can read more about companion object here ( Companion Object )
  3. Inside the companion object, we declare an attribute named instance, which will be our singleton object
  4. Starting on line 6, we made a getInstance getter, where it would return an instance of our Logger class, but firstly it checked whether our instance attribute is already instantiated or not, if it is null ( have not been instantiated ), then the instance get assigned a new object of our Logger class module, the assignment will only happen on the first time the getter is called, because the next time the getter is called, it will just return the already instantiated object before.

--

--

A World-Class center for innovative ICT enabling the vigorous pursuit of outstanding BINA NUSANTARA services.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arya Surya

Mostly writes about frontend, including but not limited to iOS and web development. Go follow @agustinustheoo for other stuff