Ir al contenido principal

Entradas

Mostrando entradas de abril, 2012

Javascript patterns 2 (Singleton)

We continue with the design pattern more commonly used in Javascript. Singleton design pattern is used to ensure that only one instance of the object is created. Singleton Pattern var SingletonObject = {     Model : "Ferrari",     Color: "Red",     ToString: function () {         return "Model: " + this .Model + ", Color: " + this .Color;     } }

Javascript Patterns 1 (Constructors)

One of the first design pattern  you must know is the "Constructor pattern", which lets you create objects/classes in Javascript. Classes dont exist in JavaScript, but we can "simulate" them with functions. Basic Constructor function Car(model, color) {     this.Model = model;     this.Color = color;     this.ToString = function () {         return "Model: " + this .Model + ", Color: " + this .Color;     } } Constructor using Prototypes function Car(model, color) {     this.Model = model;     this.Color = color; } Car.prototype.ToString = function () {     return "Model: " + this .Model + ", Color: " + this .Color; }

Javascript Design Patterns

I want to show you some of the Design Patterns more commonly used in Javascript. Design pattern can be divide into categories and i will show you three: Creational, Structural and Behavioral. Creational Patterns Abstract Factory Builder Prototype Singleton Structural Patterns Adapter Bridge Composite Decorator Facade Flyweight Proxy Behavioral Patterns Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor I will comment some of this design patterns with an example later in another post.