DEV Community

Design patterns. How do you select yours?

ImTheDeveloper on October 08, 2017

Iโ€™ve recently found myself spending time reading up on the various design patterns out there, primarily JavaScript based but they can be applied to...
Collapse
 
kspeakman profile image
Kasey Speakman

I wouldn't get too obsessed over patterns. They can be handy to know once you can recognize the problems they solve. But in my experience it's harder to work the other way, starting with the patterns. The danger in starting with patterns is making abstractions which don't end up fitting your problem. Focus on solving the problem first. You'll start to develop an intuition on when the patterns apply.

Collapse
 
slackerzz profile image
Lorenzo

You don't choose design patterns, they choose you!
Honestly, dp are just best practices, at beginning of your career you usually are worried about them, later on you use them without thinking this is a state, this is a decorator... They become tools inside your toolbox.

Collapse
 
tomowens profile image
Thomas J Owens

A few passages from the Gang of Four Design Patterns book stand out.

From the Preface:

Design patterns capture solutions that have developed and evolved over time. Hence they aren't the designs people tend to generate initially. They reflect untold redesign and recoding as developers have struggled for greater reuse and flexibility in their software.

From Chapter 1 (Introduction):

Each design pattern systematically names, explains, and evaluates an important and recurring design...Our goal is to capture design experience in a form that people can use effectively.

Knowing the appropriate design patterns for the context in which you are working is a good thing. But patterns are, in my opinion, first and foremost a communication tool. You can have a good design without knowingly using any patterns. However, understanding patterns and information about things like the intention, motivation, and participants and their responsibilities allow you to talk about and understand other people's designs, which can help you improve your designs.

At the end of the day, though, focus on good design principles, not trying to use patterns. As you read about other people's designs along with the problems they were trying to solve and the tradeoffs they made, you'll come across various "patterns".

Collapse
 
dariojavierrick profile image
Dario Javier Rick

Hi. I believe that it is more reliable to think about what not to do, rather than thinking in patterns itself. Your second question is a common example of "golden hammer" or "silver bullet" anti pattern. Don't assume that your favorite solution could resolve all the problems.

Maybe a pattern could fit in your proyect or maybe not, but you should not force them. Pherhaps you have a simple project with 2 or 3 classes, and forcing a "abstract factory" or "decorator" result in a overpowered architecture (and confusing, by the way). Sometimes keep it simple is the best.

Now, if you need to start a project with a complex architecture, with many clases and too much interconnection between objects, maybe you should think a bit more and check wich pattern could fit in some situation, but always using the "divide and conquer" strategy first. Always is simplier to resolve many easy problems than a very difficult one. Then, the patterns appear naturally.

Not knowing design patterns is not bad at all, but they could help you a lot. They gives you generic solutions for many common problems

Collapse
 
ben profile image
Ben Halpern

I agree with this line of thinking. The design patterns also sort of find their way to you when you need them most. I personally am not well-versed on design patterns in general, mostly reliant on the pattens I've acquired along the way when I need them, either formally or through natural independent invention.

Collapse
 
bgadrian profile image
Adrian B.G.

When I was Lead and had to design a project from scrath (OOP), I printed cheatsheets and glue them to a nearby wall.

When we reached the implementation phase we can quickly search if a problem we have can be solve by one or a mix of known design patterns.

Collapse
 
imthedeveloper profile image
ImTheDeveloper • Edited

I've actually been looking at doing this. Not just for design patterns but for JavaScript in general. I read a lot and there's only so much information I can take in. I often know there is a concept out there or need a slight prompt. I believe cheat sheets are a great way to jog your memory. I found them especially useful back when I was learning new languages.

I think in this case there is a distinction between the patterns used within modules and then there's the patterns used to organise my projects & modules. In the cheat sheet it terms these are structural vs creational which is a great initial distinction to make!

Your post has actually got me on the hunt for some other cheat sheets github.com/addyosmani/essential-js...

Collapse
 
bgadrian profile image
Adrian B.G.

Yes there are many patterns and you only use them when starting a new project / system / module, impossible to remember them all.

Especially in JS where you can jog between multiple programming paradigms, our brain can be overloaded so cheatsheets are here to rescue us. From DRY/SOLID principles to JS module patterns it's good to make some wall posters from them.

Thread Thread
 
imthedeveloper profile image
ImTheDeveloper

Good point around the beginning of the project. I'm just starting out building a telegram bot which will use a lot of custom commands for authenticated users.

I've decided instead of doing my usual "write code - suffer pain - refactor" mantra I will plan out elements of the architecture.

As an example, since every message would be running through my application I expect a middleware pattern would be useful for logging, checking for authentication levels, spotting messages that contain commands and attaching a custom handler.

Once the message is picked up by the handler I could use a plugin based architecture as there are many commands that I would treat as custom hooks to be fired in response to the routed command.

Even having a rough implementation view at this level will help to structure my project. I can then begin delving into the creation patterns such as an InstantMessage class which creates a structure I can then utilise in my concrete classes which hold the detail of the commands.

Thread Thread
 
bgadrian profile image
Adrian B.G.

Offtopic - I made some bots in the last 2 years and I came across this generator that does something simple and beautiful

  • each command is a file (module)
  • each command receive the message & client, returns a message or error
  • each command describe itself
Thread Thread
 
imthedeveloper profile image
ImTheDeveloper

Thanks for this sounds like it will give me a nice framework to review and steal with pride.

Collapse
 
johwacode profile image
joh

I think there are very different types patterns that you could use in various situations.
For starters there are dp that are nearly always a good idea such as facades or adapters.
Then there are patterns that fit a special model-structure as containers, fabrics etc, you'll recognize them when they fit your project.
Then there are patterns you just are aware of because you just used them for any recent project, maybe per accident, maybe someone told you to, maybe you read about them - if they were helpfully you'll try to reuse them, hopefully they're fitting again.
At last there are dozens of patterns you're not even aware of. While experimenting you could try to do stuff with them, but you usually don't try to pick one and make your project fit to it.