JavaScript Design Patterns: The Revealing Template Pattern and the Singleton Pattern

26/06/2021

Design patterns are patterns that can be applied in software development. For something to be considered a standard it must meet three basic criteria: it must be a proven solution, it is easily reusable and it can be used in many different systems. The revealing model pattern and the Singleton Pattern that we'll talk about today is no exception to this rule.

The Revealing Model Pattern

introduction

In the last post we talked about the template pattern. This pattern is linked to the revealing pattern pattern, as Christian Heilmann's (creator of the revealing pattern pattern) creates this pattern to solve pattern pattern problems. According to him, his frustration stems from having to repeat the object's name when he wants to call a public method. Also, having to change the literal notation of his object to change something between public and private bothered him.

The solution he found to solve the problem was to make all functions and variables private, and return an anonymous object with a pointer to the functions he would like to be public.

  • Exemplo:

var myRevealingModule = (function () {

var privateVar = "Ben Cherry",

publicVar = "Hey there!";

function privateFunction() {

console.log( "Name:" + privateVar );

}

function publicSetName( strName ) {

privateVar = strName;

}

function publicGetName() {

privateFunction();

}

// Reveal public pointers to // private functions and properties

return {

setName: publicSetName,

greeting: publicVar,

getName: publicGetName

};

})();

myRevealingModule.setName( "Paul Kinlan" ); - Código encontrado no livro "

Learning JavaScript Design Patterns

A book by Addy Osmani

"


in the example above, you can see more clearly how the pattern can be used in practice.

Benefits

  • This pattern allows your scripts to be more consistent;
  • It allows for easier visualization of what can be accessed publicly and what cannot, changing small details from the standard of conventional models.

Disadvantages

  • if a private role refers to a public role, that public role cannot be overridden if an update is required.
  • The standard does not apply to public members, only to roles. Public objects that refer to private variables are also subject to the last problem, ie, public objects may not be replaced if an update is needed.
  • This module can be more fragile than the original template pattern thanks to the last two topics.

Singleton pattern

Introdução

This pattern is well known, in javascript its function is to restrict the instantiation of a class to a single object.

Classically, the Singleton pattern can be implemented by creating a class with a method that creates a new instance of the class if one does not exist. In the case of an already existing instance, it simply returns a reference to that object.

In javascript, it provides a single access point for functions.

  • Example:

var mySingleton = (function () {

// Instance stores a reference to the Singleton

var instance;

function init() {

// Singleton // Private methods and variables

function privateMethod(){

console.log( "I am private" );

}

var privateVariable = "Im also private";

var privateRandomNumber = Math.random();

return {

// Public methods and variables

publicMethod: function () {

console.log( "The public can see me!" );

},

publicProperty: "I am also public",

getRandomNumber: function() {

return privateRandomNumber;

}

};

};

return {

// Get the Singleton instance if one exists // or create one if it doesn't getInstance: function () {

if ( !instance ) {

instance = init();

}

return instance;

}

};

})();

var singleA = mySingleton.getInstance();

console.log(singleA.getRandomNumber());

Código encontrado no livro "

Learning JavaScript Design Patterns

A book by Addy Osmani

"


Singletons differ from static classes (or objects) in that we can delay their initialization, usually because they require some information that may not be available during initialization time. They don't provide a way for code that isn't aware of a previous reference to them to retrieve them easily. This is because it is not the object or "class" that is returned by a Singleton, it is a structure.

Vantagem

  • This pattern is useful when a single object needs to have its actions coordinated throughout the system.
  • It guarantees that a class (in the case of javascript represented by a function) has only one instance

Disadvantages

  • Singletons can be more difficult to test due to issues ranging from hidden dependencies, difficulty creating multiple instances, and so on.

If this article was helpful to you, consider subscribing to our newsletter. It takes less than a minute and you'll be kept up to date with articles like this and more. We just need your email: