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设计模式结构型模式
结构型模式: – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结 构,用来解决更大的问题 分类: • 适配器模式.代理模式.桥接模式. 装饰模式.组合模式.外观模式.享元模式 结构型模式 ...
随机推荐
- [HTML5] Text Alternatives
Most of times, we need 'alt' to the images, so it can tell the screen reader what is this image abou ...
- Hibernate Tools-代码生成
建立好数据库后,点击一个键,代码就生成了,这实在是份十分满意的事情.前面有介绍Hibernate Tools就能够生成代码,那么我们怎么利用它来生成代码呢. 以下就把具体步骤贴出来,相同,有图有真相. ...
- xargs用例一个
ls -a *.doc|awk -F. '{print $1}' |xargs -I {} java -jar ~/soft/jodconverter-2.2.2/lib/jodconverter-c ...
- 机器学习 LR中的参数迭代公式推导——极大似然和梯度下降
Logistic本质上是一个基于条件概率的判别模型(DiscriminativeModel). 函数图像为: 通过sigma函数计算出最终结果,以0.5为分界线,最终结果大于0.5则属于正类(类别值为 ...
- 编译最新版webrtc源码和编译好的整个项目10多个G【分享】
编译最新版webrtc源码和编译好的整个项目10多个G[分享] 参考https://webrtc.org/native-code/development/编译最新版webrtc源码: Git clon ...
- Java-API-POI:POI百科
ylbtech-Java-API:POI百科 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1. ...
- UIPickerView的自定义视图
UIPickerView允许开发者对列表项进行任意定制 开发者只要实现UIPickerViewDelegate协议中的-pickerView:viewForRow:forComponent: reus ...
- springmvc生成注册验证码
通过SPRing MVC为系统添加验证码 1:布局登陆页面,用户名,密码,填写验证码的文本框,及验证码的图片及点击换图 <%@ taglib prefix="c" uri=& ...
- 一个登录页面的spring 逻辑过程
1.首先用户访问login.jsp 2.用户在登录页面输入用户名/密码,提交表单到服务器,Spring根据配置调用LoginController控制器响应登录请求(关键) 3.LoginControl ...
- JQuery学习系列篇(二)
1.事件切换函数 hover([over],out); over鼠标移动到元素上要触发的函数,out鼠标移出元素要触发的函数. 2.togger 如果元素是可见的,切换为隐藏的:如果元素是隐藏的,切换 ...