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

本篇文章主要通过实例对适配器模式进行了详解,需要的朋友可以参考下

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

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

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

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

以下举例说明:

1.类适配器

8054e94307c522932b6f09775f5438c0.png

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 对适配器原有接口方法的调用被委托给新接口的实例的特定方法

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值