Posts

Showing posts with the label Factory

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", { ...

Good Old And Common JavaScript Errors

... I won't write any introduction, but I'll let you comment, OK? for loops (plus ++i) for(i = 0; i // global i defined for(var i = 0; i // cache the length if it won't change during the loop // associate once only under certain conditions to speed up for(var i = 0; i // there is absolutely nothing wrong in above loop Few people told me something like: " doode, if you use ++i at the end you need to write i " ... basically the for loop has been rarely understood, let me try again: // directly from C language, 1972 for( // optional inline declaration, coma accepted for more vars ; // optional condition to verify // if undefined, it loops until a break is encountered // this condition is performed BEFORE the first loop // if false, the loop will never be executed ; // optional POST operation, it will never be executed // if the condition is false, "", 0, or null // it is executed in any case AFTER the loop, if any ){}...

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 ...

PHP or JavaScript implicit Factory method design pattern

Update Above technique could be used to create an implicit Singleton as well. class Demo { // your unbelievable stuff } function Demo($some, $arg){ static $instance; return isset($instance) ? $instance : ($instance = new Demo($some, $arg)); } Demo(1,2)->doStuff(); Demo(1,2) === Demo(1,2); // true from Wikipedia The factory method pattern is an object-oriented design pattern ... More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects. I have talked about JavaScript possibility different times in my prototypal inheritance documentation, and in other posts of this blog. The summary is that thanks to perfect this behaviour , that for some unknown reason someone would like to modify in JS2 , making them ambiguous when you are using a constructor as a function and not, for example, as private method, we can create intelligent constructors that does not require the usage of new keyword - in a Pythonic way: // Java...