How to remember design patterns

Profile picture of Nainia Youness Hacker Noon

Nainia Youness

I am a computer engineer and would like to share my thoughts on what I have learned so far.

I just read the book “Design Patterns: Elements Of Reusable Object-Oriented Software” by the gang of four. And although the code was difficult to understand, it really is a reference book that I cannot recommend highly enough. That’s why I would like to share what I took away from it as well as how it helped me improve my coding skills and most importantly what I did to remember most of the designs I saw.

Why do we need design templates?

Well for many of you there is an obvious answer to that. But for newbies like me, coding was all about sitting down and getting things done whenever you had a problem to solve or an app to build. I figured only inexperienced programmers would take the time to sit down and plan each step (senior developers; know-it-all!).

But what drove me to design models and then to this wonderful book, first and foremost were the big projects that needed to be flexible and that were meant to last for the long haul. It was then that I realized that this naive way of approaching programming was not helping me because I would end up with a big pile of mess every time that was impossible to understand (even for me), to extend or maintain.

How to remember design patterns

First of all, I suggest you learn the SOLID principles before you dive into the design patterns, as they are the basis of it, and focus more on the important principles that are used frequently.

For those of you reading this particular book or any other relating to this area, you will know that there are many! Each of these principles addresses a particular problem. Some of them are even interchangeable and others work together. And it can be difficult for beginners to remember and use them when needed.

Originally I tried to remember the book definition of each one, and while some of them really capture the meaning like the singleton model “Make sure a class n have only one instance and provide a global access point to it. “. Others, I find, are just not as expressive as you might expect.

But due to the importance of this, I had to find a way to keep them available in my toolbox at all times. What I did was relate them to everyday life situations and actions. Here is a list of some of the design patterns in the book, their definition, and what made me remember them.

PS: It’s just so that the intention of each design template seems more intuitive, the implementation has to be understood. And you can modify them to suit a more relevant situation that you come across frequently.

1-Creative design template

  • Abstract factory design template

“Provide an interface to create families of linked or dependent objects without specifying their concrete classes. “

If you buy a keyboard, chances are you will buy a mouse with it. Therefore, e-commerce websites may offer to buy both at the same time. I say they are using an abstract factory. Forcing you to purchase related items together.

“Separate the construction of a complex object from its representation so that the same construction process can create different representations. “

If the process of building an office or any piece of furniture is difficult and time consuming, I say the company should create a builder to simplify the process. By giving you a user-friendly manual with step-by-step instructions to follow. Or he could even send an employee (builder) to build from scratch.

“Define an interface to create an object, but let the subclasses decide which class to instantiate. The Factory method allows a class to defer instantiation to subclasses. “

The name itself gives enough information, it is enough to regard it as a real factory.

“Make sure a class has only one instance and provide a global access point to it.” “

Call a singleton an object that you configure or activate only once, such as setting your game’s graphics settings. And then you or anyone can use it directly without having to waste time in the future.

2-Structural design models

“Convert the interface from a class to another interface that customers expect. Adapter allows classes to work together that otherwise could not function due to incompatible interfaces. “

We’ve all used real adapters at some point when we couldn’t directly connect two devices together.

“Compose objects in trees to represent entire partial hierarchies. Composite allows customers to treat individual objects and object compositions uniformly. “

You wouldn’t talk to your boss the same way you would talk to your co-worker because of the hierarchy. But it would be easier if you could send them the same email without having to change anything. If only you could use the composite pattern.

  • Decorator design template

“Dynamically attach additional responsibilities to an object. Decorators provide a flexible alternative to subclassing to expand functionality.

Normally decorating something will add no functionality to it. But think of it as a superhero costume that gives more power to the person wearing it (laser beams from the eyes, super strength, etc.)

“Provide a substitute or placeholder for another object to control access to it.” “

When you are a child, your proxy is by definition someone who has some sort of power over you like your parents. They control how and when people talk to you, under what circumstances, and they can basically make decisions for you. But all of this is essential to protect yourself.

3-Behavioral design models

    “Wrap a query as an object, allowing you to set up clients with different queries, queue or log queries, and support rollback operations.” “

Lately, many technological devices like mice or smart lights offer you a customizable button. This gives customers more flexibility since the order is not directly attached to the button.

“To provide a way to access the elements of an aggregated object sequentially without exposing its underlying representation.” “

When using your favorite social network, you keep scrolling down and up through the list of posts. You are browsing the list not knowing what algorithm is used to browse the posts or what exact type of data you are scrolling through. You scroll through the window regardless of what is happening in the background. This is what the iterator model is for.

“Define a family of algorithms, encapsulate each of them and make them interchangeable. The strategy allows the algorithm to vary regardless of the clients that use it. “

When you buy a pencil, you usually don’t care about its brand as long as it works and is cheap. This means that they are interchangeable for you and when you go to the market you are looking for a pencil, not a specific brand. This is the model of strategy.

“Define a one-to-many dependency between objects so that when an object changes state, all of its dependencies are notified and updated automatically.” “

Let’s say you’re expecting an important email from the manager. Once you receive it, you will need to immediately make the required changes to the codebase. You can send them an email saying “Are you going to send it now?” »Every 5 minutes. But they could and probably will be annoyed by it. The most natural way is to move on with your life and once the email is sent you will receive a notification and you can start working immediately. This is called the observer model.


Here it is. Hope this helped you. If so, feel free to do the same for the rest of the design templates.

Profile picture of Nainia Youness Hacker Noon
through Nainia Youness @younessna.I am a computer engineer and would like to share my thoughts on what I have learned so far.

Read my stories

Key words

Source link

Abdul J. Gaspar