# 桥接设计模式

• 继承

• 桥接

### 实现

• 图形抽象类

``````let Shape = function(color) {
this.color = color;
};
Shape.prototype.setColor = function(color) {
this.color = color;
};
Shape.prototype.draw = function() {
new  Error();
}``````
• 图形类

``````let Rectangle = function (color) {
Shape.call(this, color);
};
extend(Rectangle, Shape);
Rectangle.prototype.draw = function () {
color.bepaint("长方形");
};

let Square = function (color) {
Shape.call(this, color);
};
extend(Square, Shape);
Square.prototype.draw = function () {
color.bepaint("正方形");
}

let Circle = function (color) {
Shape.call(this, color);
};
extend(Circle, Shape);
Circle.prototype.draw = function () {
color.bepaint("圆型");
};``````
• 颜色抽象类

``````let Color = function() {
};
Shape.prototype.bepaint = function() {
new  Error();
};``````
• 颜色类

``````let Red = function () {
Color.call(this);
};
extend(Red, Color);
Red.prototype.bepaint = function(shape) {
console.log("白色的" + shape);
};

let Green = function () {
Color.call(this);
};
extend(Green, Color);
Green.prototype.bepaint = function(shape) {
console.log("绿色的" + shape);
};

let Blue = function () {
Color.call(this);
};
extend(Blue, Color);
Blue.prototype.bepaint = function(shape) {
console.log("蓝色的" + shape);
};``````
• 使用

``````let red = new Red();

//正方形
let square = new Square();
//红色的正方形
square.setColor(red);
square.draw();

//长方形
let rectange = new Rectangle();
//红色长方形
rectange.setColor(red);
rectange.draw();``````

原文作者：设计模式
原文地址: https://segmentfault.com/a/1190000013317094
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。