javascript design patterns (proxy mode)

Object-oriented design principles, also known as single responsibility principle, for the five basic principles of object-one (SOLID). It provides for a class should be only one reason for changes. The so-called duty refers to the reasons like change. If a class has more than one motive is changed, then this class will have more than one role. The single responsibility principle refers to a class or module should have one and only one reason to change.

The key proxy mode is convenient when customers direct access to an object or a body does not meet the need to provide a substitute object to control access to this object, the client actually accesses the stand-in objects. After stand-in objects to requests some processing, then the request to the body object.

Agent can be used for: pre-loaded images

var setImage = (function(){
    var imgNode = document.createElement('img');
    document.body.appendChild(imgNode);
    return {
        setImgSrc:function(src){
            imgNode.src = src;
        }
    }
})();
//代理函数
var proxyFn = (function(){
    var img = new Image;
    img.onload = function(){
        setImage .setImgSrc(this.src);
    }
    return{
        setImgSrc:function(src){
            setImage .setImgSrc('loading.gif');
            img.src = src;
        }
    }
})();

proxyFn .setImgSrc('test.jpg');

 

Guess you like

Origin www.cnblogs.com/xingxingclassroom/p/11359176.html