DEV Community

Oliver Mensah
Oliver Mensah

Posted on • Edited on

Design Patterns for Developers using JavaScript - Part One

Most developers using JavaScript strives to write code that is readable, maintainable, and reusable because writing "bad" code in JavaScript is easy. The problem arises from the fact that JavaScript we use today is not the JavaScript that was written back in 1995 by Brendan Eich. Originally, JavaScript was written for interactive web pages but today, JavaScript has grown, and hence it is used on many platforms like servers, mobile, desktop, and many more. Since it is now used on other platforms aside from the web pages, if much care is not taking, developers will start writing JavaScript which is messy and becomes really difficult to read and maintain.

When it comes to writing elegant, maintainable, and reusable codes, design patterns are what is needed under the tool belt of developers to help structure and organize JavaScript codes. Each pattern consists of many properties. By the end of part one of this series, you should be able to understand various design patterns and how you can use them to write elegant code.

The concept of Design Patterns comes from an architect named Christopher Alexander. Back in 1977, he wrote a book called A Pattern Language about the common patterns that he came across while building buildings and towns and cities. Alexanders' concept of design patterns let the world of software enter into the world of Gand of Four, Erich, Richard, Ralph, and John, where they turned his concepts into common patterns for software development in a book called Design Patterns and mostly known as the Gang of Four. According to the book, there are some common problems that developers faced while writing software and then what pattern can help them fix that. For example; creating a service layer to operate on data where there is a separation of the data manipulation from controllers, The Module Pattern can help in tackling this problem. Also, say we have a complicated object, Facade Pattern can be used to abstract the complicated object and make it simpler. What then is the design pattern?

Design Patterns are proven concepts that provide a description or template for how to solve a problem that can be used in many different situations. So why do we even care about all these design patterns? As programmers, we encounter problems. Once the problem has not been solved, such a person then comes out with a solution to that problem. In case someone faces a similar problem, why solve a problem again? The person then makes use of what has been solved before to solve his problem. Also, we have different developers from different areas like those from functional or object-oriented programming languages, design patterns gives a common vocabulary for them to communicate well as a collective thing.

Let's Get Started?
Now that we have seen how easily developers can write bad JavaScript code as well as the common solutions that can be used when writing software. Design Patterns exist in many forms. We have patterns that deal with the creation of new instances of an object. Those patterns can be classified into Creational Design Pattern. Also, as Creational deals with creating objects, Structural Design Pattern deals with the makeup of the actual objects themselves. Finally, we have a Behavioral pattern which deals more with how objects relate to each other and how they operate, as opposed to the structure or getting a new instance of something

NB: Part two will focus only on Creational Design Patterns.

Top comments (1)

Collapse
 
alexwaweru profile image
Alex Njoroge waweru • Edited

This pretty good Oliver. I would love to add that design patterns help developers isolate the "vectors of change" in their systems. Developers find the most important thing that changes in their systems and put it away. Once developers find the vector of change in their system, they have the focal point around which to structure their design. For example if your most important thing is to limit the number of instances of the class to one, the singleton pattern is the most elegant solution for you.