本文实例讲述了javascript设计模式 – 代理模式原理与用法。分享给大家供大家参考,具体如下:介绍:代理使我们很常见的一众模式,proxy,nginx都称之为代理,代理是什么意思呢?代理模式在客户端和目标对象之间加入一个新的代理对象,代理对象起到一个中介作用,去掉客户不能看到的内容和服务,或者增添客户需要的额外服务。介绍:定义:给某一个对象提供一个代理,并由代理对象控制对原对象的引用。代理模式是一种对象结构型模式。定义:场景:我们还是以画图形为例,我将所有的绘图动作包装到Shape类中,使用代理模式来部分开放功能给客户。场景:示例:示例:
var Shape = function(color){

console.log('创建了一个对象');

this.color = color;

this.x;

this.y;

this.radius;


this.setAttr = function(x, y, radius){

this.x = x;

this.y = y;

this.radius = radius;

}

this.drawCircle = function(){

console.log('画圆: 颜色:' + this.color + ' x:' + this.x + ' y:' + this.y + ' radius:' + this.radius)

}

this.drawSquare = function(){

console.log('画方: 颜色:' + this.color + ' x:' + this.x + ' y:' + this.y )

}

this.drawTriangle = function(){

console.log('画三角: 颜色:' + this.color + ' x:' + this.x + ' y:' + this.y )

}
}

var proxyShape = function(color, x, y, radius){

this.color = color;

this.x = x;

this.y = y;

this.radius = radius;

this.shape = null;

this.drawSquare = function(){

if(this.shape === null){

this.shape = new Shape(this.color);

this.shape.setAttr(this.x, this.y, this.radius);

}

this.shape.drawSquare();

}
}

var square = new proxyShape('red', 10, 10);
square.drawSquare();
square.drawSquare();
// 创建了一个对象
// 画方: 颜色:red x:10 y:10
// 画方: 颜色:red x:10 y:10


var Shape = function(color){

console.log('创建了一个对象');

this.color = color;

this.x;

this.y;

this.radius;


this.setAttr = function(x, y, radius){

this.x = x;

this.y = y;

this.radius = radius;

}

this.drawCircle = function(){

console.log('画圆: 颜色:' + this.color + ' x:' + this.x + ' y:' + this.y + ' radius:' + this.radius)

}

this.drawSquare = function(){

console.log('画方: 颜色:' + this.color + ' x:' + this.x + ' y:' + this.y )

}

this.drawTriangle = function(){

console.log('画三角: 颜色:' + this.color + ' x:' + this.x + ' y:' + this.y )

}
}

var proxyShape = function(color, x, y, radius){

this.color = color;

this.x = x;

this.y = y;

this.radius = radius;

this.shape = null;

this.drawSquare = function(){

if(this.shape === null){

this.shape = new Shape(this.color);

this.shape.setAttr(this.x, this.y, this.radius);

}

this.shape.drawSquare();

}
}

var square = new proxyShape('red', 10, 10);
square.drawSquare();
square.drawSquare();
// 创建了一个对象
// 画方: 颜色:red x:10 y:10
// 画方: 颜色:red x:10 y:10

你可以在proxyShape中增加一些日志,权限等任务。因为代理类的存在,新增的任务不会影响到Shape类。代理模式为对象的简介访问提供了解决方案,可以对对象的访问进行控制。代理模式总结:代理模式总结:优点:
* 代理模式可以协调调用者和被调用这,一定程度降低了系统耦合度。优点:
缺点:
* 由于增加了代理对象,因此有些类型的代理模式可能会造成请求的处理速度变慢。
* 实现代理模式需要额外的工作,有些代理模式的实现非常复杂。缺点:适用场景:
* 当客户端需要访问远程主机中的对象时,可以使用远程代理。
* 当需要用一个消耗资源较少的对象来代表资源消耗较多的对象,可以使用虚拟代理
* 当需要控制一个对象的访问,为不同用户提供不同级别的访问权限时可以使用保护代理适用场景:感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》javascript面向对象入门教程JavaScript错误与调试技巧总结JavaScript数据结构与算法技巧总结JavaScript遍历算法与技巧总结JavaScript数学运算用法总结希望本文所述对大家JavaScript程序设计有所帮助。