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

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

下面举例说明:

类适配器 类图

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");
} }

对象适配器类图:

/**
* 对象适配器: 使用对象组合的方式,是动态组合的方式。
* 既能画方又能画圆
* @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);
} }

接口适配器

类图

/*
* 接口适配器:接口中有抽象方法,我们仅仅想实现当中的几个。不想所有实现,
* 所以提供一个默认空实现,然后继承自它,重写实现我们想实现的方法
*/
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

Java 实现适配器(Adapter)模式的更多相关文章

  1. java演示适配器(adapter)模式

    为什么要使用模式: 模式是一种做事的一种方法,也即实现某个目标的途径,或者技术. adapter模式的宗旨就是,保留现有类所提供的服务,向客户提供接口,以满足客户的需求. 类适配器:客户端定义了接口并 ...

  2. 设计模式--适配器(Adapter)模式

    今天学习另一个设计模式,适配器(Adapter)模式,这是一个共同方向,但有特殊要求,就应用到此设计模式.写到这里,想起很久以前,有写过一篇<ASP.NET的适配器设计模式(Adapter)&g ...

  3. 【原】模式之-适配器Adapter模式

    适配器Adapter模式 适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 模式所涉及的角色有 ...

  4. 适配器(Adapter)模式

    适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的一些其他名称:变压器模式.转换器模式.包装(Wrapper)模式.适 ...

  5. 设计模式C++描述----06.适配器(Adapter)模式

    一. 定义 适配器模式将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. Adapter 模式的两种类别:类模式和对象模式. 二. 举例说明 实际中 ...

  6. 2、适配器 adapter 模式 加个"适配器" 以便于复用 结构型设计模式

    1.什么是适配器模式? 适配器如同一个常见的变压器,也如同电脑的变压器和插线板之间的电源连接线,他们虽然都是3相的,但是电脑后面的插孔却不能直接插到插线板上. 如果想让额定工作电压是直流12伏特的笔记 ...

  7. 漫谈设计模式(一):代理(Proxy)模式与适配器(Adapter)模式对比

    1.前言 为什么要将代理模式与适配器模式放在一起来说呢?因为它们有许多的共同点,当然也有一些不同的地方.首先两者都是属于结构型模式.结构型模型是这样定义的: 结构型模式涉及到如何组合类和类以获得更大的 ...

  8. Adapter(适配器)模式

    1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相关库的发展和进化.  例子1:iphone4,你即可以 ...

  9. java设计模式结构型模式

    结构型模式: – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结 构,用来解决更大的问题 分类: • 适配器模式.代理模式.桥接模式. 装饰模式.组合模式.外观模式.享元模式 结构型模式 ...

随机推荐

  1. [GraphQL] Fetch Server Data and Client-side State in One Query using React Apollo + GraphQL

    In this lesson we look at how the Apollo @client directive can be used to fetch client-side state al ...

  2. 简单易学的机器学习算法——神经网络之BP神经网络

    一.BP神经网络的概念     BP神经网络是一种多层的前馈神经网络,其基本的特点是:信号是前向传播的,而误差是反向传播的.详细来说.对于例如以下的仅仅含一个隐层的神经网络模型: watermark/ ...

  3. centos编译ffmpeg x264

    1.安装汇编编译器(一般系统自带吧).假设没有依照以下的命令安装吧 yum install yasm 2.使用最新x264源代码编译(仅仅支持编码)    在x264官网下载最新的代码http://w ...

  4. No unique bean of type [net.shougongfang.action.paymoney.AlipayPayMoneyReturnObj] is defined: Unsat

    0 你把@Service放到实现类上吧.这个问题好像不止一个人在问啦 2013年10月25日 10:34 shidan66  30  0 1 1 加入评论 00 1,@service放到实现上  2. ...

  5. maven冲突管理及依赖管理实践

    1.“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突 Maven采用“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突,即如果一个项目最终 ...

  6. Java-SpringCloud:目录

    ylbtech-Java-SpringCloud:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:htt ...

  7. tomcat开启https服务

    一.创建证书 证书是单点登录认证系统中很重要的一把钥匙,客户端于服务器的交互安全靠的就是证书:本教程由于是演示所以就自己用JDK自带的keytool工具生成证书:如果以后真正在产品环境中使用肯定要去证 ...

  8. Win32 CRT and MFC 清单文件.manifest配制

    Demo.exe.manifest <?xml version="1.0" encoding="UTF-8" standalone="yes&q ...

  9. Python 之 入门须知

    1.Python2.0不支持中文,3.0支持 2.版本问题

  10. python pip fatal error in launcher unable to create process using

    用pip安装一个包,不知道为啥,就报了这个错误:python pip fatal error in launcher unable to create process using “”   百度了一下 ...