设计模式-结构型-适配器模式

设计模式-结构型-适配器模式

适配器模式


适配器模式(变压器模式)

适配器模式是一种结构型设计模式,当上游结构于下游结构不相同,不能直接使用时进行上下游数据结构的适配处理,完成上下游数据的流转。
例如:
电源插头(2口/3口)手机充电头,显示器转接头等等。

一、适配器模式?

1.作用

将一个类的接口变成客户端所期望的另一种接口(让汽车跑在铁轨上),使原本因接口不匹配而导致无法在一起工作的两个类可以一起工作。

2.优点

  • 提高类的透明及复用性,但现有的类复用不需要改变。
  • 适配器类和原角色类解耦,提高程序的扩展性。
  • 很多业务场景中复合开闭原则。

3.缺点

  • 适配器编写过程需要结合业务场景全面考虑,有可能增加系统的复杂性。
  • 增加代码阅读难度,降低了代码的可读性,过多使用适配器会使系统代码变得凌乱。

二、角色

目标角色(ITarget):我们期望的目标接口(目标参数,返回值类型)。
源角色(Adaptee):内容满足客户需求(需转换)但接口不匹配的接口实例。
适配器(Adapter):将Adaptee转换为ITarget的实例。
说明:client访问ITarget,但ITarget不满足需求,而Adaptee满足需求不能直接访问,所以需要一个适配器Adapter来进行中转适配,使Adaptee可以转换为ITarget可访问形式。

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、类适配器

1.应用:通过类的继承实现适配器功能,

2.操作:使用Adapter继承Adaptee及ITarget来实现适配器的功能。

3.代码

代码如下(示例):

    /// <summary>
    /// Adaptee
    /// </summary>
    public class AC220
    {
        public int Out_Ac220()
        {

            Console.WriteLine("输出AC220");
            return 220;
        }
    }
    /// <summary>
    /// ITarget
    /// </summary>
    public interface DC5
    {
        public int Out_DC5();
    }
    /// <summary>
    /// Adapter
    /// </summary>
    public class AdapterAcToDC : AC220, DC5
    {
        public int Out_DC5()
        {
            var input = Out_Ac220();
            var CanOutPut = input / 44;
            Console.WriteLine($"输出{CanOutPut}");
            return CanOutPut;
        }
    }
     /// <summary>
    /// Client
    /// </summary>
    public class AdapterClient
    {
        public void OutClientDC5()
        {
            DC5 d5 = new AdapterAcToDC();
            d5.Out_DC5();
        }
    }

二、对象适配器

1.应用:通过组合来实现适配器功能。

2.操作:Adapter实现ITarget并且内部有Adaptee实例属性,并在抽象方法实现种实现适配

3.代码

代码如下(示例):

/// <summary>
    /// ITarget
    /// </summary>
    public interface DC5
    {
        public int Out_DC5();
    }
    /// <summary>
    /// Adaptee
    /// </summary>
    public class ObjectAC220
    {
        public int Out_Ac220()
        {

            Console.WriteLine("输出AC220");
            return 220;
        }
    }
    /// <summary>
    /// Adapter
    /// </summary>
    public class ObjectAdapterAcToDC : DC5
    {
        private ObjectAC220 _ac220;
        public ObjectAdapterAcToDC(ObjectAC220 aC220)
        {
            _ac220 = aC220;
        }
        public int Out_DC5()
        {
            var input = _ac220.Out_Ac220();
            var CanOutPut = input / 44;
            Console.WriteLine($"输出{CanOutPut}");
            return CanOutPut;
        }
    }

二、接口适配器

1.作用:当接口的方法过多时

2.操作:使用抽象类实现接口

3.代码

代码如下(示例):

  namespace Adapter.Demo.InterfaceAdapter
{
    /// <summary>
    /// 定义适配
    /// 多接口可实现多样适配
    /// </summary>
    public interface IInterfaceTarget
    {
        int AdapterTo5V();
        int AdapterTo12V();
        int AdapterTo24V();
        int AdapterTo36V();
        int AdapterTo48V();
    }
    /// <summary>
    /// Adaptee
    /// </summary>
    public class InterfaceAC220
    {
        public int OutPut220V()
        {
            Console.WriteLine("输出AC220");
            return 220;
        }
    }
    /// <summary>
    /// 适配器抽象
    /// </summary>
    public abstract class InterFaceAdapter : IInterfaceTarget
    {
        private InterfaceAC220 _V220v;
        public InterFaceAdapter(InterfaceAC220 v220v)
        {
            _V220v = v220v;
        }
        public virtual int AdapterTo12V()
        {
            return 0;
        }

        public virtual int AdapterTo24V()
        {
            return 0;
        }

        public virtual int AdapterTo36V()
        {
            return 0;
        }

        public virtual int AdapterTo48V()
        {
            return 0;
        }
        /// <summary>
        /// 适配器
        /// </summary>
        /// <returns></returns>
        public virtual int AdapterTo5V()
        {
            return 0;

        }
    }
    /// <summary>
    /// 适配器具体实现
    /// </summary>
    public class InterfaceAdapter : InterFaceAdapter
    {
        private InterfaceAC220 _V220v;
        public InterfaceAdapter(InterfaceAC220 aC220) : base(aC220)
        {
            _V220v = aC220;
        }
        public override int AdapterTo5V()
        {
            var result5v = _V220v.OutPut220V() / 44;
            Console.WriteLine(result5v);
            return result5v;
        }
    }
}


总结

`一般考虑版本迭代,系统扩展及对接外部系统可以优先考虑适配器模式,来实现具体的业务逻辑。
都是套路套路套路!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenny@chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值