平时我们会常常碰到这种情况,有了两个现成的类,它们之间没有什么联系。可是我们如今既想用当中一个类的方法。同一时候也想用另外一个类的方法。有一个解决方法是。改动它们各自的接口。可是这是我们最不愿意看到的。这个时候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. HDU 4268 Alice and Bob(贪心+Multiset的应用)

     题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...

  2. 菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t

    菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn ...

  3. hdu5592/BestCoder Round #65 树状数组寻找第K大

    ZYB's Premutation    Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一个排列PP,但他只记得PP中每个前缀区间的逆 ...

  4. User_Login_Register_Shopping 1.0

    #!/usr/bin/env python# -*- coding: utf-8 -*-# @File : user_login_register_shopping.py# @Author: Anth ...

  5. TFS源代码管理工具:

    源代码管理: 先获取最新版本,再签入.如发现错误,可以点击--源代码管理--获取特定版本撤回修改 1.签入:(要备注,测试通过后签入) 敏捷开发:(小步快跑):小部分功能开发完成测试通过后就签入 全部 ...

  6. Oprofile分析(android oprofile性能分析)

    一.内核支持: make menuconfig 1.评测菜单中启用 Oprofile ,在 .config 文件中设置?CONFIG_PROFILING=y?和?CONFIG_OPROFILE=y 2 ...

  7. SpringBoot(八) Spring和消息队列RabbitMQ

    概述 1.大多数应用中,可以通过消息服务中间件来提升系统异步能力和拓展解耦能力. 2.消息服务中的两个重要概念:消息代理(Message broker)和目的地(destination) 当消息发送者 ...

  8. CSS实现栅格布局

    CSS实现栅格布局 设置容器container: .grid-container { width: 100%; max-width: 1200px; } 清除浮动: .row:before, .row ...

  9. IE8不支持响应式设计解决方法

    下载并引入 respond.js 即可 为了针对IE8应用这段脚本,需要针对IE8的条件注释 <!--[if lt IE 9]> --- <! [endif]--> 为了不让并 ...

  10. 像素点的Hessian矩阵

    最近开始学习图像处理相关知识,碰到对像素点求黑塞矩阵查了资料才搞懂. 给定一个图像f(x,y)上的一点(x,y).其黑塞矩阵如下: 因为导数的公式是f'(x)=(f(x+dx)-f(x))/dx在数字 ...