设计模式(六)适配器

一、定义

将一个类的接口转换成客户希望的另一个接口。适配器模式让那些接口不兼容的类可以一起工作。适配器模式是一种结构型模式。

二、描述

包含以下三个角色:
1、Target(目标抽象类):目标抽象类定义了客户所需要的接口,可以是一个抽象类或接口,也可以是一个具体的类,由于C#不支持多继承,所以它只能是接口。
2、Adapter(适配器类):它可以调用另一个接口,作为一个转换器,对Adaptee和Target进行适配。适配器Adapter是适配者模式的核心,在适配器模式中,它通过继承Target并关联一个Adaptee对象使二者产生联系。
3、Adaptee(适配者类):适配者即被适配的角色,它定义了一个已经存在的接口,这个接口需要适配,适配者类一般是一个具体类,包含了客户希望使用的业务方法,在某些情况下甚至没有适配者类的源代码。

三、例子

X公司很久以前曾经开发了一个算法库,包含了排序、查找等算法。在为某学校开发教务管理系统时,开发人员设计了一个成绩操作接口IScoreOperation,在该接口中声明了排序方法Sort(int[])和查找方法Search(int[],int),但是为了提高排序和查找的效率,决定重用算法库中的快速排序算法类QuickSort和二分查找算法类BinarySearch。算法库已经没有源码了,需要在不改动两边代码的情况下完成功能。 IScoreOperation:抽象成绩操作接口,充当目标抽象类

C# 复制代码
public interface IScoreOperation
{
    int[] Sort(int[] array);
    int Search(int[] array, int key);
}

QuickSortHelper、BinarySearchHelper:快速排序算法类、二分查找算法类,充当适配者类

C# 复制代码
public class QuickSortHelper
{
    public int[] QuickSort(int[] array)
    {
        Sort(array, 0, array.Length - 1);
        return array;
    }

    public void Sort(int[] array, int p, int r)
    {
        int q = 0;
        if (p < r)
        {
            q = Partition(array, p, r);
            Sort(array, p, q - 1);
            Sort(array, q + 1, r);
        }
    }

    public int Partition(int[] array, int p, int r)
    {
        int x = array[r];
        int j = p - 1;

        for (int i = p; i <= r - 1; i++)
        {
            if (array[i] <= x)
            {
                j++;
                Swap(array, j, i);
            }
        }

        Swap(array, j + 1, r);
        return j + 1;
    }

    public void Swap(int[] array, int i, int j)
    {
        int t = array[i];
        array[i] = array[j];
        array[j] = t;
    }
}

public class BinarySearchHelper
{
    public int BinarySearch(int[] array, int key)
    {
        int low = 0;
        int high = array.Length - 1;

        while (low <= high)
        {
            int mid = (low + high) / 2;
            int midVal = array[mid];

            if (midVal < key)
            {
                low = mid + 1;
            }
            else if (midVal > key)
            {
                high = mid - 1;
            }
            else
            {
                return 1;   // 找到元素返回1
            }
        }

        return -1;  // 未找到元素返回-1
    }
}

OperationAdapter:成绩操作类,充当适配器类

C# 复制代码
public class OperationAdapter : IScoreOperation
{
    private QuickSortHelper sortTarget;
    private BinarySearchHelper searchTarget;

    public OperationAdapter()
    {
        sortTarget = new QuickSortHelper();
        searchTarget = new BinarySearchHelper();
    }

    public int Search(int[] array, int key)
    {
        return searchTarget.BinarySearch(array, key);
    }

    public int[] Sort(int[] array)
    {
        return sortTarget.QuickSort(array);
    }
}

Program:测试代码

C# 复制代码
IScoreOperation operation = new OperationAdapter();
if (operation == null)
{
    return;
}

int[] scores = { 84, 76, 50, 69, 90, 91, 88, 96 };
int[] result;
int score;

Console.WriteLine("测试成绩排序结果:");
result = operation.Sort(scores);
foreach (int s in result)
{
    Console.Write("{0},", s.ToString());
}
Console.WriteLine();

Console.WriteLine("查找是否有90分的人:");
score = operation.Search(scores, 90);
if (score == -1)
{
    Console.WriteLine("抱歉,这个真没找到~~~");
}
else
{
    Console.WriteLine("恭喜,的确存在90分选手~~~");
}

Console.WriteLine("查找是否有92分的人:");
score = operation.Search(scores, 92);
if (score == -1)
{
    Console.WriteLine("抱歉,这个真没找到~~~");
}
else
{
    Console.WriteLine("恭喜,的确存在92分选手~~~");
}
Console.ReadLine(); 

四、总结

1、优点

(1)将目标类和适配者类解耦,通过引入一个适配器类来重用现有的适配者类,无须修改原有结构。
(2)增加了类的透明性和复用性,将具体的业务实现过程封装在适配者类中,对于客户端类而言是透明的,而且提高了适配者的复用性,同一个适配者类可以在多个不同的系统中复用。
(3)灵活性和可扩展性很好,可以通过配置文件、反射机制等,配合增加或切换新的适配器,完全符合开闭原则。

2、缺点

(1)C#、Java等不支持多继承,一次最多只能适配一个适配者类,不能同时使用多个。
(2)适配者类不能成为最终类,例如C#中不能为sealed类。
(3)C#、Java等类适配器模式中目标抽象类只能是接口,具有一定局限性。

相关推荐
李广坤11 小时前
状态模式(State Pattern)
设计模式
李广坤12 小时前
观察者模式(Observer Pattern)
设计模式
李广坤13 小时前
中介者模式(Mediator Pattern)
设计模式
李广坤13 小时前
迭代器模式(Iterator Pattern)
设计模式
李广坤14 小时前
解释器模式(Interpreter Pattern)
设计模式
阿无,16 小时前
java23种设计模式之前言
设计模式
Asort17 小时前
JavaScript设计模式(八):组合模式(Composite)——构建灵活可扩展的树形对象结构
前端·javascript·设计模式
数据智能老司机17 小时前
数据工程设计模式——数据基础
大数据·设计模式·架构
笨手笨脚の20 小时前
设计模式-代理模式
设计模式·代理模式·aop·动态代理·结构型设计模式
Overboom1 天前
[C++] --- 常用设计模式
开发语言·c++·设计模式