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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

lixiang0522   2021-03-20 我要评论
想了解Java结构型设计模式中的适配器模式与桥接模式解析的相关内容吗,lixiang0522在本文为您仔细讲解Java的适配器和桥接模式的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,适配器模式,桥接模式,设计模式,下面大家一起来学习吧。

适配器模式

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

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

实现

20162384930660.png (475×274)

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. 你想创建一个可以通用的类,该类可以调用一些不相关的类的接口来供你使用。


桥接模式

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

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

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

20162384958680.png (629×299)

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; 
  } 
} 
  
/** "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. 抽象和实现部分都可以独立的扩展,而不相互影响。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们