温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实例解析Java设计模式编程中的适配器模式使用

发布时间:2020-09-26 10:20:19 来源:脚本之家 阅读:154 作者:wbb 栏目:编程语言

适配器模式的主要作用是在新接口和老接口之间进行适配,通过将一个类的接口转换成客户期望的另一个接口,让原本不兼容的接口可以合作无间,本文以实例解析Java设计模式编程中的适配器模式使用,需要的朋友可以参考下

平时我们会经常碰到这样的情况,有了两个现成的类,它们之间没有什么联系,但是我们现在既想用其中一个类的方法,同时也想用另外一个类的方法。有一个解决方法是,修改它们各自的接口,但是这是我们最不愿意看到的。这个时候Adapter模式就会派上用场了。

p > Adapter模式也叫适配器模式,是由GoF提出的23种设计模式的一种。Adapter模式是构造型模式之一,通过Adapter模式,可以改变已有类(或外部类)的接口形式。

适配器 模式 有三种方式,一种是对象适配器,一种是类适配器, 一种是接口适配器

以下举例说明:

1.类适配器

实例解析Java设计模式编程中的适配器模式使用

public class DrawRectangle {//画方 
 public void drawRectangle(String msg) { 
  System.out.println("draw Rectangle: " + msg); 
 } 
} 
public interface IDrawCircle {//画圆的接口 
 void drawCircle(); 
} 
/** 
 * 类适配器 使用对象继承的方式,是静态的定义方式 
 * @author stone 
 * 
 */
public class DrawAdapter4Class extends DrawRectangle implements IDrawCircle {//既能画方又能画圆 
 @Override
 public void drawCircle() { 
  System.out.println("DrawAdapter4Class: drawCircle"); 
 }   
}

2.对象适配器

/** 
 * 对象适配器: 使用对象组合的方式,是动态组合的方式。 
 * 既能画方又能画圆 
 * @author stone 
 * DrawAdapter是适配器,DrawRectangle属于adapter,是被适配者,适配器将被适配者和适配目标(DrawCircle)进行适配 
 * 
 */
public class DrawAdapter4Object implements IDrawCircle {//既能画方又能画圆  
 private DrawRectangle drawRectangle; 
 public DrawAdapter4Object(DrawRectangle drawRectangle) { 
  this.drawRectangle = drawRectangle; 
 }  
 @Override
 public void drawCircle() { 
  System.out.println("DrawAdapter4Object: drawcircle"); 
 }   
 public void drawRectangle(String msg) { 
  drawRectangle.drawRectangle(msg); 
 }   
} 

3.接口适配器

/* 
 * 接口适配器:接口中有抽象方法,我们只想实现其中的几个,不想全部实现, 
 * 所以提供一个默认空实现,然后继承自它,重写实现我们想实现的方法 
 */
public interface IDraw { 
 void drawCircle(); 
 void drawRectangle(); 
} 
/* 
 * 接口适配器 的默认实现 
 */
public class DefaultDrawAdapter implements IDraw {//画方 画圆 皆为空实现 
 @Override
 public void drawCircle() { 
 } 
 @Override
 public void drawRectangle() { 
public class Test { 
 public static void main(String[] args) { 
  //对象适配器 
  DrawAdapter4Object objAdapter = new DrawAdapter4Object(new DrawRectangle()); 
  objAdapter.drawCircle(); 
  objAdapter.drawRectangle(" in DrawAdapter4Object"); 
  System.out.println("--------------");    
  //类适配器 
  DrawAdapter4Class clzAdapter = new DrawAdapter4Class(); 
  clzAdapter.drawCircle(); 
  clzAdapter.drawRectangle("in DrawAdapter4Class");  
  System.out.println("--------------"); 
  //接口适配器 
  MyDrawAdapter myDrawAdapter = new MyDrawAdapter(); 
  myDrawAdapter.drawCircle(); 
  myDrawAdapter.drawRectangle(); 
 } 
 static class MyDrawAdapter extends DefaultDrawAdapter { 
  @Override
  public void drawCircle() { 
   System.out.println("drawCircle in MyDrawAdapter"); 
  } 
   
 } 
}

打印

DrawAdapter4Object: drawcircle 
draw Rectangle: in DrawAdapter4Object 
-------------- 
DrawAdapter4Class: drawCircle 
draw Rectangle: in DrawAdapter4Class 
-------------- 
drawCircle in MyDrawAdapter 

适配器模式的三个特点:

1 适配器对象实现原有接口

2 适配器对象组合一个实现新接口的对象(这个对象也可以不实现一个接口,只是一个单纯的对象)

3 对适配器原有接口方法的调用被委托给新接口的实例的特定方法

有人认为讲解设计模式的例子都太简单,看着感觉是那么回事,但是要是真想在项目开发中使用,还真是应用不到。其实我们不必在项目中刻意使用设计模式,而是应该从实际的设计问题出发,看哪个模式能解决我们的问题,就使用哪个模式。不要为了使用模式而使用模式,那样就舍本逐末了,一般情况下,只要遵循一定的设计原则就可以了,设计模式也是根据这些原则被总结出来的,熟悉了这些原则,模式自然而然就有了。

希望本篇文章对您有所帮助

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI