1. The constructor Pattern
Modules are an integral piece of any robust application's architecture and typically help in keeping the unit of code for a project cleanly separated and organized
There are several option for implementing modules. These include:
- Object literal notation
- The module Pattern
- AMD modules
- CommonJS module
- ECMAScript Harmony modules
Object literals :
The modules Pattern:
Let's begin looking at an implementation of the Module pattern by created a module that is self-contained.
One thing that the revealing module can do is avoiding repeat the name of the main object when we want to call one public method from another or access public variables.
The Singleton pattern is thus known because it restricts instantiation of a class to single object. Singletons differ from static classes as we can delay their initialization. generally because they require some information that may not be available during initialization time. For code that is unaware of a previous reference to them, they do not provide a method for easy retrieval. Let's have a look of the structure of singleton:
The observer is a design pattern in which an object maintains a list of objects depending on it observers, automatically notifying them of any changes to state.
- Maintains a list of observers, facilities adding or removing observers
- Provides an update interface for object that need to be notified of a subject’s change of state
- Broadcasts notifications to Observers on changes of state, stores the state of ConcreteObservers
- Stores a reference to the ConcreteSubject, implements an update interface for the observer to ensure state is consistent with subjects
When a subject needs to notify observers about something interesting happening, it broadcasts a notification to the observers ( including specific data related to the topic of the notification)
If it appears a system has too many direct relationships between components. it may be time to have a central point of the control that components communicate through instead. The mediator pattern promotes loose coupling by ensuring that instead of components referring to each other explicitly.
Factory can provide a generic interface for creating objects, where we can specify the type of factory object we wish to create. see the diagram below.
Mixins are classes that offer functionality that can be easily inherited by a sub-class or group of sub-classes for the purpose of the function reuse.
In this case, superhero is capable of overriding any inherited values with values specific to its object.