zoukankan      html  css  js  c++  java
  • javascript设计模式7

    链式调用

    (function(){
        function _$(els){
            //...
        }
        _$.prototype={
            each:function(fn){
                for(var i=0,len=this.elements.length;i<len;++i){
                    fn.call(this,this.elements[i]);
                }
                return this;
            },
            setStyle:function(prop,val){
                this.each(function(el){
                    el.style[prop]=val;
                });
                return this;
            },
            show:function(){
                var that=this;
                this.each(function(el){
                    that.setStyle('display','block');
                });
                return this;
            },
            addEvent:function(type,fn){
                var add=function(el){
                    if(window.addEventListener){
                        el.addEventListener(type,fn,false);
                    }
                    else if(window.attachEvent){
                        el.attachEvent('on'+type,fn);
                    }
                };
                this.each(function(el){
                    add(el);
                });
                return this;
            }
        };
        window.$=function(){
            return new _$(arguments);
        };
    })();

    从支持链式调用的方法获取数据

    window.API=window.API||function(){
        var name='Hello World';
        this.setName=function(newName){
            name=newName;
            return this;
        };
        this.getName=function(callback){
            callback.call(this,name);
            return this;
        };
    };
    var o2=new API;
    o2.getName(console.log).setName('Meow').getName(console.log);

    简单工厂

    var BicycleShop=function(){};
    BicycleShop.prototype={
        sellBicycle:function(model){
            var bicycle;
            switch(model){
                case'The Speedster':
                bicycle=new Speedster();
                break;
                case'The Lowrider':
                bicycle=new Lowrider();
                break;
                case'The Comfort Cruiser':
                default:
                bicycle=new ComfortCruiser();
            }
            interface.ensureImplements(bicycle,Bicycle);
            bicycle.assemble();
            bicycle.wash();
            return bicycle;
        }
    
    };

    把成员对象的创建工作交给外部对象

    var BicycleFactory={
        createBicycle:function(model){
            var bicycle;
            switch(model){
                case'The Speedster':
                bicycle=new Speedster();
                break;
                case'The Lowrider':
                bicycle=new Lowrider();
                break;
                case'The Flatlander':
                bicycle=new Flatlander();
                break;
                case'The Comfort Cruiser':
                default:
                bicycle=new ComfortCruiser();
            }
            Interface.ensureImplements(bicycle,Bicycle);
        }
    };

    工厂模式(不是使用另外一个类或对象创建,而是使用子类创建)

    var BicycleShop=function(){};
    BicycleShop.prototype={
        sellBicycle:function(model){
            var bicycle=this.createBicycle(model);
            bicycle.assemble();
            bicycle.wash();
            return bicycle;
        },
        createBicycle:function(model){
            throw new Error('...')//抽象类,声明这个方法,但不实现这个方法,直接调用会报错
        }
    };
    var AcmeBicycleShop=function(){};
    extend(AcmeBicycleShop,BicycleShop);
    AcmeBicycleShop.prototype.createBicycle=function(model){
        var bicycle;
        switch(model){
            case'The Speedster':
            bicycle=new AcmeSpeedster();
            break;
            case'The Lowrider':
            bicycle=new AcmeLowrider();
            break;
            case'The Comfort Cruiser':
            default:
            bicycle=new AcmeComfortCruiser();
        }
        Interface.ensureImplements(bicycle,Bicycle);
        return bicycle;
    };
    var GeneralProductsBicycleShop=function(){};
    extend(GeneralProductsBicycleShop,BicycleShop);
    GeneralProductsBicycleShop.prototype.createBicycle=function(model){
        var bicycle;
        switch(model){
            case'The Speedster':
            bicycle=new GeneralProductsSpeedster();
            break;
            case'The Lowrider':
            bicycle=new GeneralProductsLowrider();
            break;
            case'The Comfort Cruiser':
            default:
            bicycle=new GeneralProductsComfortCruiser();
        }
        Interface.ensureImplements(bicycle,Bicycle);
        return bicycle;
    };
    //需要时任意调用即可
    //var bobCruisers=new GeneralProductsBicycleShop();
    //var yourSecondNewBike=bobCruisers.sellBicycle('The Lowrider');
  • 相关阅读:
    ArcGIS for Android地图控件的5大常见操作
    adb开启不了解决方案
    Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤
    解决 Your project contains error(s),please fix them before running your application问题
    二路归并算法实现
    字符串全排列
    python连接MySQL
    .net常考面试题
    win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面
    int.Parse()与int.TryParse()
  • 原文地址:https://www.cnblogs.com/sdgjytu/p/4227550.html
Copyright © 2011-2022 走看看