First exploration of Design Patterns.
Design Patterns are solutions to recurring software design problems. Here we provide an introduction to Design Patterns, their history, and their benefits.
Design patterns are solutions to programming problems you find again and again in real-world application development. The design involves a description or a solution template for solving a problem that can be applied in different scenarios. Patterns are formalized best practices that developers can use in their own applications.
The GoF patterns are generally considered the foundation for all other patterns. They were published in 1995 in a seminal book called "Design Patterns, Elements of Reusable Object-Oriented Software". Four authors were involved -- Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides –hence the name Gang-of-Four, or GoF for short. This publication was instrumental in introducing the concept of Design Patterns to the field of software engineering.
Actually, the notion of design patterns originated in a different discipline, namely architecture. Christopher Alexander, a well-known architect first developed the concept of a pattern language in which he categorized architectural design elements that are both beautiful and practical. His design elements include exact methods for constructing practical, safe, and attractive designs at any scale (from individual rooms to city blocks). The Gang of Four borrowed this concept of reusable design elements and applied it to world of software design.
So, design patterns are solutions to common software design problems. Let's review the benefits of using patterns:
Perhaps you are wondering, when does a particular software design solution qualify as a pattern? This is not always an easy question to answer. The GoF state that design patterns are "descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context". Essentially it is a solution to a problem in a context, that is, each pattern focuses on a general, but scoped problem area or issue. This is the classical view on patterns. As you learn more about JavaScript patterns you will see that the JavaScript community has a more pragmatic perspective towards design patterns.
Even so, there are always four essential elements to a pattern:
These elements, together with the GoF pattern definition, provide a reasonable starting point of what constitutes a design pattern. The diagram below depicts this graphically. The pre-pattern stage represents the problem and its context which is messy and unstructured. The post-pattern stage shows the pattern solution which is clean and well structured. The design has a name and there are consequences associated with using this pattern.
In their book, the GoF are very detailed in their pattern definitions. They use a formal 12-point system that categorizes and describes each pattern; this includes: Name, Intent, Motivation, Applicability, Participants, Structure, Consequences, Implementation, Known Uses, and several more. This certainly was useful and relevant at that time because patterns were a new concept and it added clarity and structure to each of the 23 patterns they listed.
However, today you will not find a single pattern author that follows this system. One reason is that the concept of a design pattern has become more fluid and is used in broader contexts. Also, it is sometimes hard to pinpoint a single pattern author because patterns evolve in online communities with many participants. A new design pattern is rarely nailed in a single design session in which each of the 12 points can be defined.
In fact, the JavaScript community is a great example of how pattern development has evolved. It started off as a rather formal discipline in which a 'software authority' (mostly book authors) put their stamp of approval on a series of patterns. Today, the pattern movement takes a far more agile and pragmatic approach in which numerous community members participate resulting in highly practical software design solutions that are of immediate use to the practicing web app developer. The nature of Design Patterns in JavaScript is discussed next.