• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java结构型设计模式中的适配器模式与桥接模式解析

java 搞代码 4年前 (2022-01-05) 23次浏览 已收录 0个评论

这篇文章主要介绍了Java结构型设计模式中的适配器模式与桥接模式,结构型设计模式是从程序的结构上解决模块之间的耦合问题,需要的朋友可以参考下

适配器模式

定义
适配器模式(英语:adapter pattern)有时候也称包装样式或者包装。将一个类的接口转接成用户所期待的。一个适配使得因接口不兼容而不能在一起工作的类工作在一起。

有两类适配器模式:
1. 对象适配器模式 – 对象适配器通过关联满足用户期待接口,还降低了代码间的不良耦合。在工作中推荐使用“对象适配”。
2. 类适配器模式 – 这种适配器模式下,适配器继承自已实现的类(一般多重继承),java中没有多重继承,所以这里不做介绍。

实现

1. Target – 定义Client需要使用的方法。
2. Adapter – 继承或者实现Target,适配Adaptee的方法到Target。
3. Adaptee – 定义一个已经存在的方法。
4. Client – 调用Target中的方法。

 public class Adaptee { public void specificRequest(){ System.out.println("Hello, I am from Adaptee!"); } } public interface Target { public void request(); } public class Adapter implements Target { Adaptee adaptee; public Adapter(){ adaptee = new Adaptee(); } public void request(){ adaptee.specificRequest(); } } public class Client { public static void main(String[] args) { Target target = new Adapter(); target.request(); } } 

要实现类适配器模式,我们需要Adapter继承Adaptee。

适用场景
1. 你想使用一个旧类,而它的接口不符合你的需求,那么可以使用Adapter类来作为中介类。
2. 你想创建一个可以通用的类,该类可以调用一些不相关的类的接口来供你使用。

桥接模式

动机
有些时候一个抽象应该有不同的实现,比如,保存数据时有两种方式,一种是文件方式,一种是数据库方式,通常的做法是继承保存数据的类,然后实现不同的保存方式。这样做的问题就是难于修改和扩展保存方式,运行时无法切换保存方式。

定义
桥接模式是软件设计模式中最复杂的模式之一,它将事物的抽象部分与它的实现部分分离,使它们都可以独立地变化。

如“圆形”、“三角形”归于抽象的“形状”之下,而“画圆”、“画三角”归于实现行为的“画图”类之下,然后由“形状”调用“画图”。

1. Abstraction – 定义抽象方法。
2. AbstractionImpl – 使用实现接口来实现抽象方法。
3. Implementor – 为具体实现行为定义接口。
4. ConcreteImplementor1, ConcreteImplementor2 – 实现Implementor接口。

 /** "Implementor" */ 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; } } /** <strong style="color:transparent">来源gaodai#ma#com搞@@代~&码网</strong>"Client" */ class BridgePattern { 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(); } } } 

实例
1. 动机里面提到的数据保存。
2. 图形的绘制框架。类似上面代码中的实现。

适用场景
1. 你不希望抽象和实现有固定的关系,希望可以在运行时修改实现的方式。
2. 抽象和实现部分都可以独立的扩展,而不相互影响。

以上就是Java结构型设计模式中的适配器模式与桥接模式解析的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java结构型设计模式中的适配器模式与桥接模式解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址