本文实例讲述了javascript设计模式 – 桥接模式原理与应用。分享给大家供大家参考,具体如下:介绍:如果软件系统中某个类存在两个或多个独立变化的维度,可以通过桥接模式将这些维度分离出来,使两者可以独立扩展,让系统更符合单一职责原则。介绍:定义:将抽象部分与其实现部分分离,使他们都可以独立的变化。它是一种对象结构型模式,又称为柄体模式或接口模式。定义:场景:我们做一个简单的画圆,将圆的半径和颜色这两个维度进行分离,使每一个维度都可以单独扩展,很多同学都说这么简单一个需求我5行代码就实现了。为什么写这么啰嗦一套。
我们这里分享的是设计思想,当你的系统足够复杂时需要用什么样的方式进行优化。作为示例,也只是用最小的例子把道理讲明白,不是说所有类似的地方就必须这么写。什么样的场景需要什么样的模式,需不需要用模式需要你自己去考量。场景:示例:示例:
var CircularColor = {};
CircularColor.redCircular = function(){

this.getColor = function(){

return 'red';

}
}
CircularColor.greenCircular = function(){

this.getColor = function(){

return 'green';

}
}

var CircularRadius = {};
CircularRadius.small = function(){

this.x = this.y = 0;

this.radius = 5;

this.color = null;

this.setColor = function(circularColor){

this.color = circularColor.getColor();

}

this.draw = function(){

console.log('画一个小圆!颜色:' + this.color + ' 原点坐标:x:' + this.x + ' y:' + this.y + ' 半径:' + this.radius);

}
}
CircularRadius.big = function(){

this.x = this.y = 0;

this.radius = 20;

this.color = null;

this.setColor = function(circularColor){

this.color = circularColor.getColor();

}

this.draw = function(){

console.log('画一个大圆!颜色:' + this.color + ' 原点坐标:x:' + this.x + ' y:' + this.y + ' 半径:' + this.radius);

}
}

var color = new CircularColor.redCircular();
var radius = new CircularRadius.big();
radius.setColor(color);
radius.draw();//画一个大圆!颜色:red 原点坐标:x:0 y:0 半径:20


var CircularColor = {};
CircularColor.redCircular = function(){

this.getColor = function(){

return 'red';

}
}
CircularColor.greenCircular = function(){

this.getColor = function(){

return 'green';

}
}

var CircularRadius = {};
CircularRadius.small = function(){

this.x = this.y = 0;

this.radius = 5;

this.color = null;

this.setColor = function(circularColor){

this.color = circularColor.getColor();

}

this.draw = function(){

console.log('画一个小圆!颜色:' + this.color + ' 原点坐标:x:' + this.x + ' y:' + this.y + ' 半径:' + this.radius);

}
}
CircularRadius.big = function(){

this.x = this.y = 0;

this.radius = 20;

this.color = null;

this.setColor = function(circularColor){

this.color = circularColor.getColor();

}

this.draw = function(){

console.log('画一个大圆!颜色:' + this.color + ' 原点坐标:x:' + this.x + ' y:' + this.y + ' 半径:' + this.radius);

}
}

var color = new CircularColor.redCircular();
var radius = new CircularRadius.big();
radius.setColor(color);
radius.draw();//画一个大圆!颜色:red 原点坐标:x:0 y:0 半径:20

这节需要说一个点,桥接模式在java的介绍里,独立之后的维度是在抽象层会建立关联关系,js没有抽象层,所以两个独立维度通过一个setColor方法建立关联关系。桥接模式总结:桥接模式总结:优点:优点:* 桥接模式提高了系统的可扩展性,在两个变化维度中任意扩展一个维度,都不需要修改原有系统,符合开关原则。
* 多数情况下,桥接模式可以取代多层集成方案。
* 分离接口及其实现部分,使得实现可以沿着各自的维度来变化。缺点:缺点:* 桥接模式的使用会增加系统的理解与设计难度。
* 桥接模式要求正确识别出系统中两个独立变化的维度,因此其使用范围具有一定的局限性。适用场景:适用场景:* 一个类存在两个或多个独立变化的维度,且这些维度都需要独立进行扩展。感兴趣的朋友可以使用在线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程序设计有所帮助。