V
V
Vadim Remin2015-09-12 22:10:58
JavaScript
Vadim Remin, 2015-09-12 22:10:58

Can you give an example of a JavaScript task that requires the use of a pattern?

Good afternoon,
I'm studying patterns, but it's hard to come up with without much experience in what real task they can be used. Of course, the most common patterns are of interest (Singleton, Factories, Decorator, etc.).
Please give an example of setting a real problem. You can indicate in parentheses which of the patterns you need to apply to solve it.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin Kitmanov, 2015-09-12
@k12th

Patterns are not about problem solving. This is about solving a problem in a typical and understandable by another programmer method. Patterns solve problems not like “make buttons”, but like “make it so that in a year you don’t want to kill yourself against the wall and don’t have to redo everything from scratch”.

P
Pavel Volintsev, 2015-09-13
@copist

Design patterns and programming patterns are descriptions of the best solutions for common problems. Many thousands of programmers have already solved such typical tasks and most of them managed to achieve good results in approximately the same way.
The task sometimes means global ones:
how to make the second programmer understand my code (for example, MVC / MVVM - code separation into logic and presentation, Twig / Jade templating, frameworks)
how to make us code quickly (frameworks, code division front/back, Twig/Jade templating, data access via ORM/ADO, LESS/SCSS/Stylus preprocessors)
how to make it so that we can easily include a third-party module or our code can be connected to something (plugins, modules)
Medium-scale
how to make it so that there are as few calls to the server as possible (cache, lazy init, client-side validation, rich internet application)
how to handle long requests (queue, asynchronous processing, comet, websocket, long polling)
what to do, if the interface is "frozen" (show spinner, display "sorry :(")
how to make a mega-form with 50 fields (make a step-by-step wizard)
how to show something very important (popup, modal window)
small
how to make the interface responsive on user behavior (events)
how to make the code run periodically (timers, intervals)
how to make my JS scripts connect as needed and only once (requirejs)
how to make it so that I have the same task started only if the previous one has already ended (for example, do not send an AJAX request if the previous one has not completed; do not animate the same DOM object twice)
In general, it is impossible to say that each of them is solved by a combination of singletons . Or factories. There are a lot of small ones, just a blockage of small fragments of code, blocks of logic, in each of which something was done according to a template.
Here on Javascript examples with an explanation habrahabr.ru/post/132472

V
Vitaliy Orlov, 2015-09-12
@orlov0562

It is difficult to set such a task in the context in which you want to see it. Patterns are a "standard" and often effective solution to some urgent task. By "standard" I mean that other programmers understand what you're talking about just by hearing the name of the pattern. However, any pattern can be replaced with arbitrary code that solves the same problem, but such code will be harder for other people to understand, and in most cases, harder to maintain.
Well, offhand, consider this editor for comments and answers. He has buttons. Here is the task: to make a single interface for working with buttons. Of course, you can code a lot here, or you can make a factory.
In general, there are no such tasks that are solved exclusively by patterns.

O
Oleg Gamega, 2015-09-13
@gadfi

you have the wrong idea about patterns.
even without reading a single book about them, sooner or later you will come to them with your mind (not everything, of course)
their main task, oddly enough, does not relate to programming in the sense of coding)) rather, first of all, they are needed so that developers understand each other friend.
the very solution to a particular problem was invented before the gang of four and more than once))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question