桥接模式示例
我花了一些时间在 wikipedia 的桥接模式示例上,但是,我仍然不明白什么这个桥梁模式试图解释吗?
interface DrawingAPI { public void drawCircle(double x, double y, double radius); } /** "ConcreteImplementor" 1/2 */ class DrawingAPI1 implements DrawingAPI { public void drawCircle(double x, double y, double radius) { System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius); } } /** "ConcreteImplementor" 2/2 */ class DrawingAPI2 implements DrawingAPI { public void drawCircle(double x, double y, double radius) { System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius); } } /** "Abstraction" */ interface Shape { public void draw(); // low-level public void resizeByPercentage(double pct); // high-level } /** "Refined Abstraction" */ class CircleShape implements Shape { private double x, y, radius; private DrawingAPI drawingAPI; public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) { this.x = x; this.y = y; this.radius = radius; this.drawingAPI = drawingAPI; } // low-level i.e. Implementation specific public void draw() { drawingAPI.drawCircle(x, y, radius); } // high-level i.e. Abstraction specific public void resizeByPercentage(double pct) { radius *= pct; } } /** "Client" */ class Main { public static void main(String[] args) { Shape[] shapes = new Shape[2]; shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1()); shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2()); for (Shape shape : shapes) { shape.resizeByPercentage(2.5); shape.draw(); } } }
子类 CircleShape 构造函数需要 4 个参数,在其 draw() 方法中,前 3 个参数被传递给第 4 个参数,该参数可以是 DrawingAPI 的任何子类。 那么这是否意味着使用桥接模式可以增加灵活性呢? 这个例子还能告诉我们更多的事情吗?
谢谢!!!!
I had spend some time on this Bridge pattern example from wikipedia, however, i still do not understand what is this bridge pattern trying to explain.
interface DrawingAPI { public void drawCircle(double x, double y, double radius); } /** "ConcreteImplementor" 1/2 */ class DrawingAPI1 implements DrawingAPI { public void drawCircle(double x, double y, double radius) { System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius); } } /** "ConcreteImplementor" 2/2 */ class DrawingAPI2 implements DrawingAPI { public void drawCircle(double x, double y, double radius) { System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius); } } /** "Abstraction" */ interface Shape { public void draw(); // low-level public void resizeByPercentage(double pct); // high-level } /** "Refined Abstraction" */ class CircleShape implements Shape { private double x, y, radius; private DrawingAPI drawingAPI; public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) { this.x = x; this.y = y; this.radius = radius; this.drawingAPI = drawingAPI; } // low-level i.e. Implementation specific public void draw() { drawingAPI.drawCircle(x, y, radius); } // high-level i.e. Abstraction specific public void resizeByPercentage(double pct) { radius *= pct; } } /** "Client" */ class Main { public static void main(String[] args) { Shape[] shapes = new Shape[2]; shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1()); shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2()); for (Shape shape : shapes) { shape.resizeByPercentage(2.5); shape.draw(); } } }
The subclass CircleShape constructor takes 4 args, in its draw() method, the first 3 args are passed to the 4th arg which can be any subclass from DrawingAPI.
So does this mean that using bridge pattern can increase flexibility?
and are there more things this example can tell us?
Thanks!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个更具体的例子会更清楚地说明为什么这是有用的。假设DrawingAPI1封装了你的图形驱动程序,而DrawingAPI2为你的打印机驱动程序做了同样的事情。 DrawingAPI 是图形系统的通用 API。它允许您在显示器上绘制一个 CircleShape 并使用相同的代码将其打印在一张纸上,您只需传入不同的 DrawingAPI 实现即可。但是,如果将 DrawingAPI 传递到 Shape.draw() 而不是将其传递到构造函数中,则会更加灵活,因为这样您就可以对显示器和打印机使用相同的对象图。
A more concrete example why this is useful will make it clearer. Suppose DrawingAPI1 encapsulates your graphics driver, while DrawingAPI2 does the same thing for your printer driver. Then DrawingAPI is the generic API for your graphics system. It lets you draw a CircleShape to your monitor and print it on a piece of paper using the same code, you only have to pass in the different DrawingAPI implementations. However, if you pass DrawingAPI into Shape.draw() instead of passing it into the constructor it would be more flexible because then you can use the same object graph for the monitor and the printer.
这不仅仅是灵活性。 桥接模式将抽象与实现解耦,并且两者可能有所不同。
由于
CircleShape
使用组合来包含DrawingAPI
而无需继承,因此您可以将DrawingAPI
API 替换为DrawingAPI< 的多个实现之一。 /code>
Shape
和CircleShape
可以独立更改,而不依赖于DrawingAPI
。您可以在下面的 SE 帖子中找到有关桥接模式的更多详细信息,其中解释了不同的示例:
桥接模式是否将抽象与实现解耦?
It's not only the flexibility. The Bridge pattern decouples abstraction from implementation and both can vary differently.
Since
CircleShape
uses composition to containDrawingAPI
with-out inheritance, you can replaceDrawingAPI
API with any one of multiple implementations ofDrawingAPI
Shape
andCircleShape
can change independently without dependency onDrawingAPI
.You can find more details about Bridge pattern in below SE post, which explains different example:
Does the Bridge Pattern decouples an abstraction from implementation?