javascript 写职责链

 我认为职责链最大的目的在于解决对一个对象的加工过程问题、并且如何通过filter在什么时机截止操作流程

 

 

/**
 * by JackChen 2016-3-14 15.16.53
 * 职责链
 * 看马士兵老师的java 设计模式,后自己尝试写的。
 * 
 * 演示,实现了两部分内容:
 * 一个,是职责链元素的添加,和执行(按顺序)
 * 一个,是职责链过程的打断,
 *
 * 通过切面(也可以说javascript的装饰模式)修改我们的serverMethod
 * 从而加入filter改变调用结果
 */

//////////////////////////////////////////////////////////////////////
///  集合filter
var FilterChain = function() {
    var self = this;

    self.chain = new Array();
    self.name = "Chain";
    self.index = 0;
    self.error = false;
};
FilterChain.prototype = {};
FilterChain.prototype.constructor = FilterChain;

//循环调用自己的子元素
FilterChain.prototype.doFilterForward = function(input) {
    var self = this;
        console.log("f " + self.name);
    if (!self.error) {
        for (var i = 0; i < self.chain.length; i++) {
            if (!self.chain[i].doFilterForward(input)) {
                self.error = true;
                return false;
            };
        };
        return true;
    };
    return false;
};

//循环调用自己的子元素
FilterChain.prototype.doFilterBackward = function(output) {
    var self = this;
        console.log("b " + self.name);
    if (!self.error) {
        for (var i = self.chain.length - 1; i >= 0; i--) {
            if (!self.chain[i].doFilterBackward(output)) {
                self.error = true;
                return false;
            };
        };
        return true;
    }
    return false;
};

//添加子元素
FilterChain.prototype.addFilter = function(filter) {
    console.log(filter.name);
    this.chain.push(filter);
};

///////////////////////////////////////////////////////////////////////
/// 普通叶子元素
var FilterLeaf = function() {
    var self = this;

    self.name = "Leaf";
};
FilterLeaf.prototype = {};
FilterLeaf.prototype.constructor = FilterLeaf;

//在数据加上自己
FilterLeaf.prototype.doFilterForward = function(input) {
    var self = this;
    console.log("f " + self.name);
    input.data += "<" + self.name + ">";
    return true;
};

//在数据加上自己
FilterLeaf.prototype.doFilterBackward = function(output) {
    var self = this;
    console.log("b " + self.name);
    output.data += "</" + self.name + ">";
    return true;
};

/////////////////////////////////////////////////////////////////////
/// 长度处理filter
var FilterLength = function() {
    var self = this;
    self.max_length = 1000;
    self.name = "Length";
};
FilterLength.prototype = {};
FilterLength.prototype.constructor = FilterLength;

//判断数据超长则终止
FilterLength.prototype.doFilterForward = function(input) {
    var self = this;
    console.log("f " + self.name);
    if (input.data.length <= self.max_length) {
        input.data += "<!--" + self.name + "-->";
        return true;
    }
    else {
        return false;
    };
};

FilterLength.prototype.doFilterBackward = function(output) {
    var self = this;
    console.log("b " + self.name);
        output.data += "<!--" + self.name + "-->";
        return true;
};



//////////////////////////////////////////////////////////////
/// 测试数据
/// 

//输入数据
var origin = {
    data: "aaaaaaaa1aaaa",
};
//输出数据
var result = {
    data: "",
};

//原生处理函数
var serverMethod = function(input, output) {
    output.data = "Server recv :[ " + input.data + " ]";
}

//创建我们的filter数据
var masterChain = new FilterChain();

//截断用filter
var lengthFilter = new FilterLength();
lengthFilter.max_length = 1000;//检测长度
masterChain.addFilter(lengthFilter);

//普通元素
var omiga = new FilterLeaf();
omiga.name = "omiga";
masterChain.addFilter(omiga);

//集合元素
var alpha0 = new FilterLeaf();
alpha0.name = "alpha0";
var beta0 = new FilterLeaf();
beta0.name = "beta0";
var fatherChain0 = new FilterChain();
fatherChain0.name = "fatherChain0";
fatherChain0.addFilter(alpha0);
fatherChain0.addFilter(beta0);
masterChain.addFilter(fatherChain0);

//集合元素
var alpha1 = new FilterLeaf();
alpha1.name = "alpha1";
var beta1 = new FilterLeaf();
beta1.name = "beta1";
var fatherChain1 = new FilterChain();
fatherChain1.name = "fatherChain1";
fatherChain1.addFilter(alpha1);
fatherChain1.addFilter(beta1);
masterChain.addFilter(fatherChain1);

//普通元素
var gama = new FilterLeaf();
gama.name = "gama";
masterChain.addFilter(gama);



//重写处理函数
var old_serrserverMethod = serverMethod;
serverMethod = function(input, output) {
    if (!masterChain.doFilterForward(input)) {
        return;
    };
    old_serrserverMethod(input, output);
    if (masterChain.doFilterBackward(output)) {
        return;
    };
}

//实际调用
serverMethod(origin, result);

//测试输出
console.log("origin :" + origin.data);
console.log("result :" + result.data);

 

posted @ 2016-03-14 15:32  $JackChen  阅读(299)  评论(0编辑  收藏  举报