美文网首页大数据
设计模式之适配器模式

设计模式之适配器模式

作者: Airprince | 来源:发表于2018-08-12 19:57 被阅读35次

设计模式之适配器模式

1.综述

在计算机编程中,适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。

- 中文名

  适配器模式

- 英文名

  adapterpattern

- 基本信息

  共有两类适配器模式

- 客    户

  需要调用我们的代码的对象

- 宗    旨

  保留现有类所提供的服务

在设计模式中,适配器模式(英语:adapter pattern)有时候也称包装样式或者包装(wrapper)。将一个类的接口转接成用户所期待的。一个适配使得因接口不兼容而不能在一起工作的类能在一起工作,做法是将类自己的接口包裹在一个已存在的类中。

2. 类适配器模式结构

2.1 结构图

2.1.1 类适配器结构图

2.1.2 对象适配器结构图

2.2 参与者

  适配器模式参与者:

  ◊ Target:Client所使用的目标接口,可以是接口或抽象类。由于C#/Java不支持多类继承,故把Target定义为接口。

  ◊ Adaptee:需要适配的类接口。

  ◊ Adapter:适配器,负责Adaptee的接口与Target接口进行适配。

  ◊ Client:与符合Target接口的对象协调的类。

  在适配器模式中,类Adapter实现适配器的功能,它在Client于Adaptee之间加入Adapter,这样Client把请求发给接口为Target的类Adapter,再由Adapter调用Adaptee,从而实现Client调用Adaptee。

3. 适配器模式结构实现

3.1 类适配器结构实现

  ITarget.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ClassAdapter

{

    publicinterface ITarget

    {

        voidRequest();

    }

}

  Adaptee.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ClassAdapter

{

    publicclass Adaptee

    {

       public void SpecificRequest()

        {

           Console.WriteLine("Called SpecificRequest()");

        }

    }

}

  Adapter.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ClassAdapter

{

    publicclass Adapter : Adaptee, ITarget

    {

       public void Request()

        {

           this.SpecificRequest();

        }

    }

}

  Client.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ClassAdapter

{

    publicclass Client

    {

       static void Main(string[] args)

        {

           ITarget t = new Adapter();

           t.Request();

        }

    }

}

复制代码

  运行输出:

Called SpecificRequest()

请按任意键继续. . .

3.2 对象适配器结构实现

  Client需要调用Request方法,而Adaptee并没有该方法,为了使Client能够使用Adaptee类,需要提供一个类Adapter。这个类包含了一个Adaptee的实例,将Client与Adaptee衔接起来。

  ITarget.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ObjectAdapter

{

    publicinterface ITarget

    {

        voidRequest();

    }

}

  Target.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ObjectAdapter

{

    publicclass Target : ITarget

    {

       public virtual void Request()

        {

           Console.WriteLine("Called Target Request()");

        }

    }

}

  Adaptee.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ObjectAdapter

{

    publicclass Adaptee

    {

       public void SpecificRequest()

        {

           Console.WriteLine("Called SpecificRequest()");

        }

    }

}

  Adapter.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ObjectAdapter

{

    publicclass Adapter : Target

    {

       private Adaptee _adaptee = new Adaptee();

       public override void Request()

        {

           _adaptee.SpecificRequest();

        }

    }

}

Client.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Structural.ObjectAdapter

{

    publicclass Client

    {

       static void Main(string[] args)

        {

           ITarget t = new Adapter();

           t.Request();

        }

    }

}

4. 适配器模式实践应用

  以手机充电的电源适配器为例,用适配器模式的解决方案。

4.1 类适配器结构实现

  ITarget.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ClassAdapter

{

    publicinterface ITarget

    {

        voidGetPower();

    }

}

  Power.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ClassAdapter

{

    publicclass Power

    {

       public void GetPower220V()

        {

           Console.WriteLine("从电源中得到220V的电压");

        }

    }

}

  Adapter.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ClassAdapter

{

    publicclass Adapter : Power, ITarget

    {

       public void GetPower()

        {

            this.GetPower220V();

           Console.WriteLine("得到手机的充电电压!");

        }

    }

}

  Client.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ClassAdapter

{

    publicclass Client

    {

       static void Main(string[] args)

        {

           Console.WriteLine("手机:");

           ITarget t = new Adapter();

           t.GetPower();

        }

    }

}

  运行输出:

手机:

从电源中得到220V的电压

得到手机的充电电压!

请按任意键继续. . .

4.2>、对象适配器结构实现

  ITarget.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ObjectAdapter

{

    publicinterface ITarget

    {

        voidGetPower();

    }

}

  Power.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ObjectAdapter

{

    publicclass Power

    {

       public void GetPower220V()

        {

            Console.WriteLine("从电源中得到220V的电压");

        }

    }

}

  Adapter.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ObjectAdapter

{

    publicclass Adapter : ITarget

    {

       public Power _power;

       public Adapter(Power power)

        {

           this._power = power;

        }

        ///

        ///得到想要的电压

        ///

       public void GetPower()

        {

            _power.GetPower220V();

           Console.WriteLine("得到手机的充电电压!");

        }

    }

}

  Client.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespaceDesignPatterns.AdapterPattern.Practical.ObjectAdapter

{

    publicclass Client

    {

       static void Main(string[] args)

        {

           Console.WriteLine("手机:");

           ITarget t = new Adapter(new Power());

           t.GetPower();

        }

    }

}

郑重声明:本文主要取材于网络:Libing的博客,在此鸣谢。本文非商业用途,学习交流。如有侵权请联系我:guozhenjun03@gmail.com

相关文章

  • 简说设计模式之适配器模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是适配器模式 适配器模式(Adapter...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 设计模式详解——适配器模式

    本篇文章介绍一种设计模式——命令模式。本篇文章内容参考《JAVA与模式》之适配器模式,Android设计模式源码解...

  • 设计模式之适配器模式

    设计模式之适配器模式 1. 模式定义 适配器模式又称包装器模式,属于结构型模式,它可以将一个接口转换成客户希望的另...

  • 最常用的设计模式---适配器模式(C++实现)

    适配器模式属于结构型的设计模式,它是结构型设计模式之首(用的最多的结构型设计模式)。 适配器设计模式也并不复杂,适...

  • iOS设计模式(5)策略模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(6)模板模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(7)建造者模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(4)抽象工厂模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(1)简单工厂模式

    设计模式系列文章 《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象工厂...

网友评论

    本文标题:设计模式之适配器模式

    本文链接:https://www.haomeiwen.com/subject/hjrlbftx.html