【设计模式】适配器模式

适配器模式(Adapter Pattern)属于结构型模式。它将一个接口转换成客户希望的另一个接口,适配器模式使得原本由于接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)。适配器模式既可以作为类结构型模式,也可以作为对象结构型模式。适配器模式让我们用一个新的适配器类(XxxxAdapter)来解决原本接口不兼容的问题。

可分为两种:

  1. 类适配器:适配器集成适配者并实现新的接口
  2. 对象适配器:适配器包含适配者并实现新的接口

比如MacBookPro上面的雷电接口(IThunderboltPort),我们手头有U盘(USBFlashDisk)和SD卡(SDFlashDisk)想插入到MacBookPro拷贝文件。那么就需要对应的适配器(USB2ThunderboltPortAdapter和SD2ThunderboltPortAdapter)。

C#代码(类适配器)

using System;
using System.Collections.Generic;

namespace ConsoleApp2
{
    //macbook pro的雷电口
    public interface IThunderboltPort
    {
        bool Support(Type type);
        void ThunderboltInsert();
    }

    //U盘,U盘要插到macbookpro上,要一个USB转Thunderbolt的适配器(usb口要被适配为Thunderbolt口)(ThunderboltPortAdapter)
    public class USBFlashDisk
    {
        public void UsbInsert()
        {
            Console.WriteLine("USB接口插入");
        }
    }

    public class SDFlashDisk
    {
        public void SDInsert()
        {
            Console.WriteLine("SD接口插入");
        }
    }

    /// <summary>
    /// 类适配模式1
    /// </summary>
    public class USB2ThunderboltPortAdapter : USBFlashDisk, IThunderboltPort
    {
        public bool Support(Type type)
        {
            return false;
        }

        public void ThunderboltInsert()
        {
            base.UsbInsert();
        }
    }
    /// <summary>
    /// 类适配模式2
    /// </summary>
    public class SD2ThunderboltPortAdapter : SDFlashDisk, IThunderboltPort
    {
        public bool Support(Type type)
        {
            return false;
        }

        public void ThunderboltInsert()
        {
            base.SDInsert();
        }
    }

    /// <summary>
    /// 对象适配模式1
    /// </summary>
    public class USB2ThunderboltPortAdapter2 : IThunderboltPort
    {
        private USBFlashDisk usbFlashDisk = new USBFlashDisk();

        public bool Support(Type type)
        {
            return type == usbFlashDisk.GetType();
        }

        public void ThunderboltInsert()
        {
            usbFlashDisk.UsbInsert();
        }
    }

    /// <summary>
    /// 对象适配模式2
    /// </summary>
    public class SD2ThunderboltPortAdapter2 : IThunderboltPort
    {
        private SDFlashDisk sdFlashDisk = new SDFlashDisk();

        public bool Support(Type type)
        {
            return type == sdFlashDisk.GetType();
        }
        public void ThunderboltInsert()
        {
            sdFlashDisk.SDInsert();
        }
    }



    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("类适配器模式-----------------------------------");
            USB2ThunderboltPortAdapter usb2ThunderboltPortAdapter = new USB2ThunderboltPortAdapter();
            usb2ThunderboltPortAdapter.ThunderboltInsert();

            SD2ThunderboltPortAdapter sd2ThunderboltPortAdapter = new SD2ThunderboltPortAdapter();
            sd2ThunderboltPortAdapter.ThunderboltInsert();


            Console.WriteLine("对像适配器模式-----------------------------------");
            USB2ThunderboltPortAdapter usb2ThunderboltPortAdapter2 = new USB2ThunderboltPortAdapter();
            usb2ThunderboltPortAdapter2.ThunderboltInsert();

            SD2ThunderboltPortAdapter sd2ThunderboltPortAdapter2 = new SD2ThunderboltPortAdapter();
            sd2ThunderboltPortAdapter2.ThunderboltInsert();


            Console.WriteLine("对像适配器模式 自动选择-----------------------------------");
            Program p = new Program();
            IThunderboltPort thunderboltPortAdapter;
            thunderboltPortAdapter = p.GetThunderboltPortAdapter(typeof(USBFlashDisk));
            thunderboltPortAdapter.ThunderboltInsert();

            thunderboltPortAdapter = p.GetThunderboltPortAdapter(typeof(SDFlashDisk));
            thunderboltPortAdapter.ThunderboltInsert();


            Console.Read();
        }


        private List<IThunderboltPort> thunderboltPortList = null;
        private IThunderboltPort GetThunderboltPortAdapter(Type type)
        {
            if (thunderboltPortList == null)
            {
                thunderboltPortList = new List<IThunderboltPort>();
                thunderboltPortList.Add(new USB2ThunderboltPortAdapter2());
                thunderboltPortList.Add(new SD2ThunderboltPortAdapter2());
            }

           foreach(var thunderboltPort in thunderboltPortList)
            {
                if(thunderboltPort.Support(type))
                {
                    return thunderboltPort;
                }
            }

            throw new Exception("error: 找不到合适的适配器");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值