生成器模式(Builder)


function extend(subclass, superclass) {
var F = function () {
};
F.prototype = superclass.prototype;
subclass.prototype = new F();
subclass.prototype.constructor = subclass;
subclass.super = superclass.prototype;

if (superclass.prototype.constructor === Object.prototype.constructor) {
superclass.prototype.constructor = superclass;
}
}

function mixin(targetObj, obj, deep) {
if (Object.prototype.toString.call(obj) !== '[object Object]') {
return;
}
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
if (deep === true) {
targetObj[i] = targetObj[i] || {};
mixin(targetObj[i], obj[i], deep);
} else {
targetObj[i] = obj[i];
}
}
}
}

<script></script>
生成器模式

定义:
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

本质:
分离整体构建算法和部件构造。
生成器模式的重心在于分离整体构建算法和部件构造,而分步骤构建对象不过是整体构建算法的一个简单实现,或者说是一个附带产物。

要实现同样的构建过程可以创建不同的表现,那么一个自然的思路就是先把构建过程独立出来,在生成器模式中把它称为指导者,由它来指导装配过程,但是不负责每步具体的实现。当然,光有指导者是不够的,必须要有能具体实现每步的对象,在生成器模式中称这些实现对象为生成器。
这样一来,指导者就是可以重用的构建过程,而生成器是可以被切换的具体实现。
Read more "生成器模式(Builder)"

不错的javascript工厂案例(Abstract Factory)

文章来自:http://www.dofactory.com/javascript-abstract-factory-pattern.aspx

Definition

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Frequency of use (in JavaScript):    high   medium

 

Summary

An Abstract Factory creates objects that are related by a common theme. In object-oriented programming a Factory is an object that creates other objects. An Abstract Factory has abstracted out a theme which is shared by the newly created objects.

Read more "不错的javascript工厂案例(Abstract Factory)"