Javascript乱弹设计模式系列 - 观察者模式(Observer)
2010-09-14 13:35:51 来源:WEB开发网这里是改进的程序示范,包括重载函数的构造,这里也暂时贴出来下:
改进的代码
// Interface.js
function Interface(name, methods)
{
if(arguments.length != 2) {
throw new Error("接口构造函数含" + arguments.length + "个参数, 但需要2个参数.");
}
this.name = name;
this.methods = [];
if(methods.length < 1) {
throw new Error("第二个参数为空数组.");
}
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i][0] !== 'string') {
throw new Error("接口构造函数第一个参数必须为字符串类型.");
}
for(var j = 1; j < methods[i].length; j++) {
if(methods[i][j] && typeof methods[i][j] !== 'number') {
throw new Error("接口构造函数第二个参数以上必须为整数类型.");
}
}
this.methods.push(methods[i]);
}
};
Interface.registerImplements = function(object) {
if(arguments.length < 2) {
throw new Error("接口的实现必须包含至少2个参数.");
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("从第2个以上的参数必须为接口实例.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method1 = interface.methods[j][0];
var arr1 = interface.methods[j].slice(1).sort(compareNumber);
for(var k = 0; k < object.methodArr.length; k++) {
var method2 = object.methodArr[k][0];
if(method1 === method2)
{
var arr2 = object.methodArr[k].slice(1).sort();
if(ComareArray(arr1,arr2))
{
break;
}
else
{
throw new Error("接口的实现对象不能执行" + interface.name + "的接口方法" + method1 + ",因为它找不到或者不匹配.");
}
}
}
}
}
};
function compareNumber(num1, num2) {
var iNum1 = parseInt(num1);
var iNum2 = parseInt(num2);
if(iNum1 < iNum2) return -1;
else if(iNum1 > iNum2) return 1;
else return 0;
}
function ComareArray(arr1, arr2) {
if(arr1.length!=arr2.length)
{
return false;
}
for(var i = 0; i < arr1.length; i++) {
if(arr1[i]!==arr2[i])
{
return false;
}
}
return true;
}
Function.prototype.getParameters = function() {
var str = this.toString();
var paramString = str.slice(str.indexOf('(') + 1, str.indexOf(')')).replace(/s*/g,''); //取得参数字符串
try
{
return (paramString.length == 0 ? [] : paramString.split(','));
}
catch(err)
{
throw new Error("函数不合法!");
}
}
// demo.js
function Overload(method)
{
this.methods = [];
for(var i = 1; i <= arguments.length - 1; i++)
{
methods.push(arguments[i].length);
}
OverloadNumber.methodArr.push([arguments[0]].concat(methods));
OverloadNumber.argumentArr.push(arguments);
return function()
{
for(var i = 0; i < OverloadNumber.methodArr.length; i++)
{
if(OverloadNumber.methodArr[i][0] == method)
{
var index = OverloadNumber.methodArr[i].slice(1).indexOf(arguments.length);
if(index != -1)
{
return OverloadNumber.argumentArr[i][index+1].apply(this,arguments);
}
}
}
throw new Error("参数不匹配!");
}
}
var INumber = new Interface("INumber", [["Add",1,0,2,3],["Sub",1,2]]); //其中1,0,2,3之类属于重载函数参数个数,可以不按先后顺序
function OverloadNumber() {
Interface.registerImplements(OverloadNumber, INumber);
}
OverloadNumber.methodArr = [];
OverloadNumber.argumentArr = [];
OverloadNumber.prototype = {
Add : Overload( //算术加
"Add",
function(a,b) {
return a + b;
},
function(a) {
return ++a;
},
function(a,b,c) {
return a+b+c;
},
function() {
return -1;
}
),
Sub : Overload( //算术减
"Sub",
function(a) {
return --a;
},
function(a,b) {
return a - b;
}
)
}
Tags:Javascript 乱弹 设计模式
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接