适配器模式主要是通过适配器来实现接口的统一,如要实现国内手机在国外充电,则需要在不同的国家采用不同的适配器来进行兼容!

  一、示例展示:

  以下例子主要通过给笔记本电脑添加类似手机打电话和发短信的功能来详细演示适配器模式的应用!

  对象适配器:

  1. 创建抽象类:Handphone

public abstract class Handphone
{
public abstract void Dail();
public abstract void SendMessage();
}

  2. 创建抽象类:Laptop

public abstract class Laptop
{
public abstract void Working();
}

  3. 创建具体类:AppleLaptop

public class AppleLaptop : Laptop
{
public override void Working()
{
Console.WriteLine("Working using laptop!");
}
}

  4. 创建适配器类:

public class AppleLatopAdapter : Handphone
{
//Keep the reference of Laptop
Laptop laptop;
public AppleLatopAdapter(Laptop laptop)
{
this.laptop = laptop;
}
public void Working()
{
laptop.Working();
} public override void SendMessage()
{
Console.WriteLine("My apple laptop can send message now!");
} public override void Dail()
{
Console.WriteLine("My apple laptop can dail now!");
}
}

  5. 客户端调用:

class Program
{
static void Main(string[] args)
{
AppleLaptop laptop = new AppleLaptop();
Handphone hpAdapter = new AppleLatopAdapter(laptop); laptop.Working(); hpAdapter.Dail();
hpAdapter.SendMessage();
Console.ReadLine();
}
}

  类适配器:

  1. 创建接口:Handphone

public interface Handphone
{
void Dail();
void SendMessage();
}

  2. 创建抽象类:Laptop

public abstract class Laptop
{
public abstract void Working();
}

  3. 创建适配器:LaptopAdapter

public class LatopAdapter : Laptop, Handphone
{
public void SendMessage()
{
Console.WriteLine("My apple laptop can send message now!");
} public void Dail()
{
Console.WriteLine("My apple laptop can dail now!");
} public override void Working()
{
Console.WriteLine("My apple laptop is working now!");
}
}

  4. 客户端调用:

class Program
{
static void Main(string[] args)
{
Handphone ltAdapter = new LatopAdapter(); ltAdapter.Dail();
ltAdapter.SendMessage();
Console.ReadLine();
}
}

  二、适配器模式设计理念:

  适配器模式主要通过添加额外的适配器类,通过对抽象类对扩展接口Handphone中的方法进行实现,同时又保留原类Laptop的方法来实现功能的扩展!

  三、角色及关系:

  

设计模式学习总结(七)适配器模式(Adapter)的更多相关文章

  1. 设计模式(五)适配器模式Adapter(结构型)

      设计模式(五)适配器模式Adapter(结构型) 1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相 ...

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

    适配器模式(Adapter) 考虑一个记录日志的应用,由于用户对日志记录的要求很高,使得开发人员不能简单地采用一些已有的日志工具或日志框架来满足用户的要求,而需要按照用户的要求重新开发新的日志管理系统 ...

  3. 设计模式之十三:适配器模式(Adapter)

    适配器模式: 将一个类的接口转换成另外一个期望的类的接口.适配器同意接口互不兼容的类一起工作. Convert the interface of a class into another interf ...

  4. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  5. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  6. 【设计模式】适配器模式 Adapter Pattern

    适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...

  7. 8.3 GOF设计模式二: 适配器模式 Adapter

    GOF设计模式二: 适配器模式 Adapter  为中国市场生产的电器,到了美国,需要有一个转接器才能使用墙上的插座,这个转接 器的功能.原理?复习单实例模式  SingleTon的三个关键点  ...

  8. JAVA设计模式——第 8 章 适配器模式【Adapter Pattern】(转)

    好,请安静,后排聊天的同学别吵醒前排睡觉的同学了,大家要相互理解嘛.今天讲适配器模式,这个模式也很简单,你笔记本上的那个拖在外面的黑盒子就是个适配器,一般你在中国能用,在日本也能用,虽然两个国家的的电 ...

  9. Java设计模式(9)适配器模式(Adapter模式)

    适配器模式定义:将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份. 为何使用适配器模式 我们经常碰到要将两个没有关系的类组合在一起使用 ...

  10. 设计模式系列之适配器模式(Adapter Pattern)——不兼容结构的协调

    模式概述 模式定义 模式结构图 模式伪代码 类适配器,双向适配器,缺省适配器 类适配器 双向适配器 缺省适配器 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 主要缺点 适 ...

随机推荐

  1. Appium移动端自动化测试之元素定位(三)

    1.name定位 driver.find_element_by_id(') driver.find_element_by_id(') driver.find_element_by_name('登录') ...

  2. php二维数组修改键名

    最近遇到一个问题,是关于json数据提交的时候,总是报出[object object]的错误,查了晚上需要资料,大部分的说法是json数据格式不规范导致的错误.一般建议说将dataType类型注释掉. ...

  3. idea无法下载源码(Cannot download sources)

    有网上说,在命令行项目根目录下,执行如下命令下载: mvn dependency:resolve -Dclassifier=sources 我试着没有效果. 但是在本地仓库中,找到要下载的目录,删除以 ...

  4. Discrete cosine transform(离散余弦转换)

    A discrete cosine transform (DCT) expresses a finite sequence of data points in terms of a sum of co ...

  5. poj3080 Blue Jeans(暴枚+kmp)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  6. Maven整理笔记の初步窥探

    第一部分:引言 Maven是一款跨平台的项目管理工具,作为Apache组织的一个开源项目,主要服务于基于java平台的项目构建.依赖管理和项目信息管理. 项目构建:编译.运行单元测试.生成文档.打包和 ...

  7. delphi 创建DLL文件 及其调用和注意事项

    首先创建一个DLL文件,项目自带的代码为: library ProjectPnr; { Important note about DLL memory management: ShareMem mus ...

  8. .net Stream篇(六)

    BufferedStream 目录: 简单介绍一下BufferedStream 如何理解缓冲区? BufferedStream的优势 从BufferedStream 中学习装饰模式 如何理解装饰模式 ...

  9. html5 video使用autoplay属性时,声音混乱

    html5 video使用autoplay属性时,声音混乱 页面代码 Index.html <html xmlns="http://www.w3.org/1999/xhtml" ...

  10. VS vs2012制作安装包

    VS  vs2012制作安装包 一.参考地址: http://www.3fwork.com/b100/000196MYM014103/