• javascript设计模式2


    接口:利 固化一部分代码 弊 丧失js的灵活性

    在JavaScript中模仿接口

    /*
    interface Composite{
        function add(child);
        function remove(child);
        function getChild(index);
    }
    interface FormItem{
        function save();
    }
    */
    var CompositeForm=function(id,method,action){
        ...
    };
    CompositeForm.prototype.add = function(child) {
        ...
    };
    CompositeForm.prototype.remove = function(child) {
        ...
    };
    CompositeForm.prototype.getChild = function(index) {
        ...
    };
    CompositeForm.prototype.save = function() {
        ...
    };

    改进一下,用属性检查

    /*
    interface Composite{
        function add(child);
        function remove(child);
        function getChild(index);
    }
    interface FormItem{
        function save();
    }
    */
    var CompositeForm=function(id,method,action){
        this.implementsInterfaces=['Composite','FormItem'];
        ...
    };
    ...
    function addForm(formInstance){
        if (!implements(formInstance,'Composite','FormItem')) {
            throw new Error("Object does not implement a required interface.");
        }
        ...
    }
    function implements(Object){
        for(var i=1;i<arguments.length;i++){
            var interfaceName=arguments[i];
            var interfaceFound=false;
            for(var j=0;j<Object.implementsInterfaces.length;j++){
                if (Object.implementsInterfaces[j]==interfaceName) {
                    interfaceFound=true;
                    break;
                };
            }
            if (!interfaceFound) {
                return false;
            };
        }
        return true;
    }

    用鸭式辨型模仿接口

    var Composite =new Interface('Composite',['add','remove','getChild']);
    var FormItem=new Interface('FormItem',['save']);
    var CompositeForm=function(id,method,action){
        ...
    };
    ...
    function addForm(formInstance){
        ensureImplements(formInstance,Composite,FormItem);
        ...
    }

    结合第一种和第三种

    var Composite =new Interface('Composite',['add','remove','getChild']);
    var FormItem=new Interface('FormItem',['save']);
    var CompositeForm=function(id,method,action){
        ...
    };
    ...
    function addForm(formInstance){
        Interface.ensureImplements(formInstance,Composite,FormItem);
        ...
    }

    Interface类的定义

    var Interface=function(name,method){
        if(arguments.length!=2){
            throw new Error("需要2个参数");
        }
        this.name=name;
        this.method=[];
        for(var i=0,len=methods.length;i<len;i++){
            if(typeof methods[i]=='string'){
                throw new Error("需要是一个字符串");
            }
            this.methods.push(methods[i]);
        }
    };
    Interface.ensureImplements=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个参数,并是Interface的实例");
            }
            for(var j=0,methodsLen=interface.methods.length;j<methodsLen;j++){
                var method=interface.methods[j];
                if (!object[method]||typeof object[method]!=='function') {
                    throw new Error('方法未找到');
                };
            }
        }
    };
  • 相关阅读:
    char array 与string
    汗= =
    看来要学 Asp.Net 了
    安装 Xamarin for Visual Studio
    Xamarin for Visual Studio 3.11.658 Alpha 版 破解补丁
    用 Xamarin for VS 创建 aar 文件的绑定
    Android中当前墙纸Wallpaper存放的位置
    Android中动态改变控件的大小的一种方法
    程序员学习能力提升三要素(读书笔记)
    让Android软键盘默认进入英文键盘
  • 原文地址:https://www.cnblogs.com/sdgjytu/p/4201472.html
Copyright © 2020-2023  润新知