Posts

Showing posts with the label Singleton

ES5 Common Design Patterns Examples - Part 1

// Abstract Factory /* ({}).createInstance() (function (a, b, c) { this.sum = a + b + c; }).createInstance([1, 2, 3]) */ Object.defineProperty( // (C) WebReflection - Mit Style License Object.prototype, "createInstance", { value: (function (create) { return function createInstance(args) { var self = this, isFunction = typeof self == "function", obj = create(isFunction ? self.prototype : self) ; isFunction && args != null && self.apply(obj, args); return obj; }; }(Object.create)) } ); // Abstract Builder /* var person = Object.builder({ setup: function (name) { this.create(); this.instance.name = name; } }); person.setup("WebReflection"); alert(person.instance.name); person.create(); person.instance.name = "Andrea"; alert(person.instance.name); */ Object.defineProperty( // (C) WebReflection - Mit Style License Function.prototype, "builder", { ...

PHP 5.3 Singleton - Fast And Abstract

In this same blog I talked different times about Singleton Pattern , in latter link " poorly " implemented in PHP 5. I say poorly, because being Singleton a pattern, there are many ways to implement it and via PHP 5.3 things are more interesting. There are several ways to define a Singleton class and to extend it, being able to automatically create another one that will follow that pattern. Here is my implementation, which is extremely fast, logic, and simple as well. <?php // Singleton :: The WebReflection Way namespace pattern; // this is just a pattern ... // so no new Singleton is allowed // thanks ot abstract definition abstract class Singleton { // note, no static $INSTANCE declaration // this makes next declaration a must have // for any extended class // protected static $INSTANCE; // @constructor final private function __construct() { // if called twice .... if(isset(static::$INSTANCE)) // throws an Exception ...

A completely revisited Singleton and Factory design pattern for PHP and JavaScript

Singleton From Wikipedia In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern by some people, who feel that it is often used as a euphemism for global variable Basically, the last point is true. Whatever we think about Singleton, we cannot say that we do not use this pattern to have the same instance, or object, in every place of our application. The most common case scenario, is usually a database object, or a global queue, used as a stack, or something similar, like a template engine instance or a DOM / XML node. The worst thing ever, is that with languages like PHP and JavaScript, the Singleton pattern is really hard to implement correctly. Why bother with a class? In a lot ...