当前位置: 移动技术网 > IT编程>开发语言>Java > Java结构型设计模式中的适配器模式与桥接模式解析

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

适配器模式

定义
适配器模式(英语: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. 抽象和实现部分都可以独立的扩展,而不相互影响。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网