Introduction to Software Design Patterns and why they matter.

Fatma Ali
2 min readDec 29, 2017

I’m a fairly new web programmer and after a couple of months of writing code for 8 hours a day, I started asking myself :

“Is this all there is to it? can I get better?”.

I have always had a thirst for more and one day, when a senior developer reviewed my code and advised me to replace some hacky lines with the Factory Design Pattern, my programming style changed forever.

So what is a software design pattern?

A design pattern is a general repeatable solution to a commonly occurring problem in software design. It is simply a description for how to solve a problem that can be used in many different situations.

Most problems we face in design, someone somewhere has faced it and has a solution for it in form of a design pattern. I chose to study design patterns because they help me write efficient, scalable and extremely maintainable code. And I sure as hell won’t re-invent the wheel :-p

However, not all agree with using design patterns in solving everyday problems. Some have argued that design patterns are overkill and they are not very much different from other abstractions.

This post serves as an introduction to a series of posts that I will be writing with regards to the different design patterns I learn and use everyday. I will be using TypeScript (a child of Java and JavaScript) to show the code examples. So why not jump right into the first one?

The Singleton Pattern

By far the simplest pattern of all yet the most powerful when it comes to maintaining a single instance of a class a

Problem:

Sometimes,we need only a single instance of a class across the whole application, especially if objects containing configurations or objects responsible for managing resources. Yet we also need a global access point for it. So how do we achieve this using the Singleton pattern?

Solution:

singleton.ts

client.ts

And there you have it! That’s the Singleton Pattern. After doing further research, some people suggested using namespaces in TypeScript as an alternative way of implementing the Singleton. So as a Bonus, here is a re-implementation of the singleton class above using namespaces:

singleton.ts

client.ts

And that’s it. We just implemented our first Pattern :-)

--

--

Fatma Ali

Software Engineer, passionate about code, food & art. Bollywood dancer sometimes💃 Find me on GitHub https://github.com/fatmali