Java 实现适配器(Adapter)模式
平时我们会常常碰到这种情况,有了两个现成的类,它们之间没有什么联系。可是我们如今既想用当中一个类的方法。同一时候也想用另外一个类的方法。有一个解决方法是。改动它们各自的接口。可是这是我们最不愿意看到的。这个时候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)模式的更多相关文章
- java演示适配器(adapter)模式
为什么要使用模式: 模式是一种做事的一种方法,也即实现某个目标的途径,或者技术. adapter模式的宗旨就是,保留现有类所提供的服务,向客户提供接口,以满足客户的需求. 类适配器:客户端定义了接口并 ...
- 设计模式--适配器(Adapter)模式
今天学习另一个设计模式,适配器(Adapter)模式,这是一个共同方向,但有特殊要求,就应用到此设计模式.写到这里,想起很久以前,有写过一篇<ASP.NET的适配器设计模式(Adapter)&g ...
- 【原】模式之-适配器Adapter模式
适配器Adapter模式 适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 模式所涉及的角色有 ...
- 适配器(Adapter)模式
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的一些其他名称:变压器模式.转换器模式.包装(Wrapper)模式.适 ...
- 设计模式C++描述----06.适配器(Adapter)模式
一. 定义 适配器模式将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. Adapter 模式的两种类别:类模式和对象模式. 二. 举例说明 实际中 ...
- 2、适配器 adapter 模式 加个"适配器" 以便于复用 结构型设计模式
1.什么是适配器模式? 适配器如同一个常见的变压器,也如同电脑的变压器和插线板之间的电源连接线,他们虽然都是3相的,但是电脑后面的插孔却不能直接插到插线板上. 如果想让额定工作电压是直流12伏特的笔记 ...
- 漫谈设计模式(一):代理(Proxy)模式与适配器(Adapter)模式对比
1.前言 为什么要将代理模式与适配器模式放在一起来说呢?因为它们有许多的共同点,当然也有一些不同的地方.首先两者都是属于结构型模式.结构型模型是这样定义的: 结构型模式涉及到如何组合类和类以获得更大的 ...
- Adapter(适配器)模式
1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相关库的发展和进化. 例子1:iphone4,你即可以 ...
- java设计模式结构型模式
结构型模式: – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结 构,用来解决更大的问题 分类: • 适配器模式.代理模式.桥接模式. 装饰模式.组合模式.外观模式.享元模式 结构型模式 ...
随机推荐
- 一点一点学架构(四)—Spring.NET错误Cannot Resolve Type……
背景 在搭建完项目框架之后,当我利用单元測试来測一条线时.出现了下面错误: Cannot resolve type[--]for object with name 'ButtonBll' define ...
- angularjs1-4 事件指令
<div ng-app="myApp"> <div ng-controller="firstController"> <div n ...
- How do I UPDATE from a SELECT in SQL Server?
方法1 https://stackoverflow.com/questions/2334712/how-do-i-update-from-a-select-in-sql-server UPDATE T ...
- 基于macOS+VMware的GNS3内VM上公网
笔者经常需要做网络实验,GNS3就是笔者最喜欢用的模拟器,为了便于实验,需要能从macos上直接ssh登陆模拟出来的vm,并且vm需要上公网.经过研究,已解决此问题,并以此分享出来 tag: maco ...
- Windows远程桌面和360
Windows的远程桌面输错了一次密码, 然后就怎么都连接不上了, 查了半天发现 傻缺360会默认屏蔽Windows的远程桌面和数据库连接..... 大家没事都卸载了360吧
- Linux中安装redis的phpredis扩展
下载phpredis扩展 http://pecl.php.net/package/redis wget http://pecl.php.net/get/redis-4.0.2.tgz 需要环境配置文件 ...
- 3Ds Max实例教程-制作女战士全过程
3Ds Max制作“女战神” 作者:Diego Rodríguez 使用软件:3Ds Max,Photoshop 3Ds Max下载:http://wm.makeding.com/iclk/?zone ...
- ZBrush中Tool工具的保存
ZBrush软件的界面及操作方法与其他的三维软件完全不同,很多初学者常常会觉得有些困难,接下来我们就讲解一下ZBrush®最为基础的操作-Tool工具的保存. 首先要明白什么是Tool工具?我们创建的 ...
- Pyhton学习——Day47
# 转载:http://www.cnblogs.com/yuanchenqi/articles/6357507.html# 外键:一种约束条件,与主键对应# 主表:被绑定的表:字表# 外键约束:# - ...
- Pyhton学习——Day28
#上下文协议:文件操作时使用with执行# with open('a.txt','w',encoding='utf-8') as f1:# with语句,为了让一个对象兼容with语句,必须在这个对象 ...