[原创]设计模型探索Q-bridge parttern之桥归桥路归路
桥接模式
将抽象部份与它的实现部份分离,使它们都可独立地变化。
桥接模式是最复杂的模式之一,它把事物对象和其具体行为、具体特征分离开来,使它们可以各自独立的变化。事物对象仅是一个抽象的概念。如“圆形”、“三角形”归于抽象的“形状”之下,而“画圆”、“画三角”归于实现行为的“画图”类之下,然后由“形状”调用“画图”。
桥接模式的组成部份:
1.抽象对象
2.对象实现
3.细化抽象
4.细化对象实现
桥接模式各组成部份之间的关系:
例子
1.抽象对象
2.抽象对象实现
3.细化抽象
4.细化抽象实例
1).
2).
5.测试
效果:
将抽象部份与它的实现部份分离,使它们都可独立地变化。
桥接模式是最复杂的模式之一,它把事物对象和其具体行为、具体特征分离开来,使它们可以各自独立的变化。事物对象仅是一个抽象的概念。如“圆形”、“三角形”归于抽象的“形状”之下,而“画圆”、“画三角”归于实现行为的“画图”类之下,然后由“形状”调用“画图”。
桥接模式的组成部份:
1.抽象对象
2.对象实现
3.细化抽象
4.细化对象实现
桥接模式各组成部份之间的关系:
例子
1.抽象对象
package
bridge;
/** */ /**
* <ul>
* <li>Title:[Shape]</li>
* <li>Description: [图形接口]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public interface Shape
{
/** *//**
* <ul>
* <li>Description:[画法]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public void draw();
/** *//**
* <ul>
* <li>Description:[百分比放缩]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param pct
*/
public void resizeByPercentage(double pct);
}
/** */ /**
* <ul>
* <li>Title:[Shape]</li>
* <li>Description: [图形接口]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public interface Shape
{
/** *//**
* <ul>
* <li>Description:[画法]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public void draw();
/** *//**
* <ul>
* <li>Description:[百分比放缩]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param pct
*/
public void resizeByPercentage(double pct);
}
2.抽象对象实现
package
bridge;
/** */ /**
* <ul>
* <li>Title:[CircleShape]</li>
* <li>Description: [圆图形]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class CircleShape implements Shape
{
/** *//** 图形位置X */
private double x;
/** *//** 图形位置Y */
private double y;
/** *//** 图形单径 */
private double radius;
/** *//** 画接口 */
private DrawAPI drawAPI;
/** *//**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param x
* @param y
* @param radius
* @param drawAPI
*/
public CircleShape(double x, double y, double radius, DrawAPI drawAPI)
{
this.x = x;
this.y = y;
this.radius = radius;
this.drawAPI = drawAPI;
}
/** *//**
* <ul>
* <li>Description:[画图]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public void draw()
{
drawAPI.drawCircle(x, y, radius);
}
/** *//**
* <ul>
* <li>Description:[百分比放缩]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param pct
*/
public void resizeByPercentage(double pct)
{
radius *= pct;
}
}
/** */ /**
* <ul>
* <li>Title:[CircleShape]</li>
* <li>Description: [圆图形]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class CircleShape implements Shape
{
/** *//** 图形位置X */
private double x;
/** *//** 图形位置Y */
private double y;
/** *//** 图形单径 */
private double radius;
/** *//** 画接口 */
private DrawAPI drawAPI;
/** *//**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param x
* @param y
* @param radius
* @param drawAPI
*/
public CircleShape(double x, double y, double radius, DrawAPI drawAPI)
{
this.x = x;
this.y = y;
this.radius = radius;
this.drawAPI = drawAPI;
}
/** *//**
* <ul>
* <li>Description:[画图]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public void draw()
{
drawAPI.drawCircle(x, y, radius);
}
/** *//**
* <ul>
* <li>Description:[百分比放缩]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param pct
*/
public void resizeByPercentage(double pct)
{
radius *= pct;
}
}
3.细化抽象
package
bridge;
/** */ /**
* <ul>
* <li>Title:[DrawAPI]</li>
* <li>Description: [画图API]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public interface DrawAPI
{
/** *//**
* <ul>
* <li>Description:[画圆]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param x
* @param y
* @param radius
*/
public void drawCircle(double x, double y, double radius);
}
/** */ /**
* <ul>
* <li>Title:[DrawAPI]</li>
* <li>Description: [画图API]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public interface DrawAPI
{
/** *//**
* <ul>
* <li>Description:[画圆]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param x
* @param y
* @param radius
*/
public void drawCircle(double x, double y, double radius);
}
4.细化抽象实例
1).
package
bridge;
/** */ /**
* <ul>
* <li>Title:[ConcretDrawA]</li>
* <li>Description: [具体画法]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class ConcretDrawA implements DrawAPI
{
/**//* (non-Javadoc)
* @see bridge.DrawAPI#drawCircle(double, double, double)
*/
public void drawCircle(double x, double y, double radius)
{
System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
}
}
/** */ /**
* <ul>
* <li>Title:[ConcretDrawA]</li>
* <li>Description: [具体画法]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class ConcretDrawA implements DrawAPI
{
/**//* (non-Javadoc)
* @see bridge.DrawAPI#drawCircle(double, double, double)
*/
public void drawCircle(double x, double y, double radius)
{
System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
}
}
2).
package
bridge;
/** */ /**
* <ul>
* <li>Title:[ConcretDrawB]</li>
* <li>Description: [具体画法2]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class ConcretDrawB implements DrawAPI
{
/**//* (non-Javadoc)
* @see bridge.DrawAPI#drawCircle(double, double, double)
*/
public void drawCircle(double x, double y, double radius)
{
System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
}
}
/** */ /**
* <ul>
* <li>Title:[ConcretDrawB]</li>
* <li>Description: [具体画法2]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class ConcretDrawB implements DrawAPI
{
/**//* (non-Javadoc)
* @see bridge.DrawAPI#drawCircle(double, double, double)
*/
public void drawCircle(double x, double y, double radius)
{
System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
}
}
5.测试
package
bridge;
public class Test
{
public static void main(String[] args)
{
Shape[] shapes = new Shape[2];
shapes[0] = new CircleShape(1, 2, 3, new ConcretDrawA());
shapes[1] = new CircleShape(5, 7, 11, new ConcretDrawB());
for (Shape shape : shapes)
{
shape.resizeByPercentage(2.5);
shape.draw();
}
}
}
public class Test
{
public static void main(String[] args)
{
Shape[] shapes = new Shape[2];
shapes[0] = new CircleShape(1, 2, 3, new ConcretDrawA());
shapes[1] = new CircleShape(5, 7, 11, new ConcretDrawB());
for (Shape shape : shapes)
{
shape.resizeByPercentage(2.5);
shape.draw();
}
}
}
1.Bridge模式使用“对象间的组合关系”解耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化。
2.所谓抽象和实现沿着各自维度的变化,即“子类化”它们,得到各个子类之后,便可以任意它们,从而获得不同平台上的不同型号。
3.Bridge模式有时候类似于多继承方案,但是多继承方案往往违背了类的单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多继承方案更好的解决方法。
4.Bridge模式的应用一般在“两个非常强的变化维度”,有时候即使有两个变化的维度,但是某个方向的变化维度并不剧烈——换言之两个变化不会导致纵横交错的结果,并不一定要使用Bridge模式。
适用性:
1.如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的联系。
2.设计要求实现化角色的任何改变不应当影响客户端,或者说实现化角色的改变对客户端是完全透明的。
3.一个构件有多于一个的抽象化角色和实现化角色,系统需要它们之间进行动态耦合。
4.虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者。
总结
Bridge 模式是一个非常有用的模式,也非常复杂,它很好的符合了开放 - 封闭原则和优先使用对象,而不是继承这两个面向对象原则。