您的位置:首页 > 编程语言 > C#

C#设计模式—适配器模式

2016-06-14 21:27 357 查看
从本节开始描述结构型设计模式。

一、结构型模式(StructuralPattern):

(1)关注将现有类和对象组织在一起形成更强大的结构;

(2)结构型模式可分为两类:类结构型模式和对象结构型模式。

(3)类结构型模式:关心的是类的组合,形成更大的系统,一般只存在继承关系和实现关系。

(4)对象结构型模式:关心的是类和对象的组合,通过关联关系在一个类中定义另一个类的实例对象,然后通过对象调用相应的方法。

二、适配器模式的定义:

将一个类的接口转换成客户希望的另一个类的接口。让接口不兼容的类一起工作。(这里的接口是广义上的接口,它可以表示一个方法或者一个方法的集合)。适配器模式又称为包装器模式。

三、适配器模式的结构:

1)目标抽象类(Target):定义客户所需要的接口,可以是一个抽象类或接口,也可以是具体的类。

2)适配器类(Adapter):可以调用另一个接口,作为转换器,对Adaptee和Target进行适配。该类是适配器模式的核心类。在类结构型适配器模式实现过程中,它通过实现Target接口并继承Adaptee类来使二者产生联系。在对象结构型适配器模式实现过程中,它通过继承Target并关联一个Adaptee对象使二者产生联系。

3)适配者类(Adaptee):被适配的角色,它定义一个已经存在的接口,这个接口需要适配,适配者类一般是一个具体的类,包含客户希望使用的业务方法。

四、适配器模式的应用实例:下载地址:http://download.csdn.net/detail/qq_30507287/9548579

开发教务管理系统,对学生成绩进行排序和查找,在不利用算法库的源代码的情况下,利用对象适配器模式来实现。

其中该系统包含以下几个部分:目标接口类:ScoreOperation.cs;适配器类:OperationAdapter.cs;适配者类:QuickSortClass.cs和BinarySearchClass.cs。

目标接口类:ScoreOperation.cs

    //抽象成绩操作类,充当目标接口
    interfaceScoreOperation
    {
        int[]Sort(int[] array);//成绩排序
        intSearch(int[] array,
intkey);//成绩查找
}

适配者类:QuickSortClass.cs

    //快速排序类,充当适配者类
    classQuickSortClass
    {
        publicint[]QuickSort(int[] arrray)
        {
           Sort(arrray, 0, arrray.Length - 1);
            returnarrray;
        }
        publicvoidSort(int[] array,
intp, int r)
        {
            intq = 0;
            if(p < r)
            {
               q = Partition(array, p, r);
               Sort(array, p, q - 1);
               Sort(array, q + 1, r);
            }
        }
        publicintPartition(int[] a,
intp, int r)
        {
            //设置的关键值x
            intx = a[r];
            intj = p - 1;
            for(inti= p; i <= r - 1; i++)
            {
               if (a[i] <= x)
               {
                   j++;
                   Swap(a, j, i);
               }
            }
           Swap(a, j + 1, r);
            returnj + 1;
        }
        publicvoidSwap(int[] a,
inti, int j)
        {
            intt = a[i];
           a[i] = a[j];
           a[j] = t;
        }
}

适配者类:BinarySearchClass.cs

    //适配者类:二分查找
    classBinarySearchClass
    {
        publicintBinarySearch(int[] array,
intkey)
        {
            int low =0;
            inthigh = 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.cs

    //适配器类
    classOperationAdapter:ScoreOperation
    {
        //定义适配者QuickSortClass对象
        privateQuickSortClass sortObj;
        //定义适配者BinarySearchClass对象
        privateBinarySearchClass searcObj;
 
        publicOperationAdapter()
        {
           sortObj = new
QuickSortClass();
           searcObj = new
BinarySearchClass();
        }
 
        publicint[]Sort(int[] array)
        {
            returnsortObj.QuickSort(array);//调用适配者类的方法
        }
 
        publicintSearch(int[] array,
intkey)
        {
            returnsearcObj.BinarySearch(array, key);//调用适配者类的方法
        }
}

配置文件:App.config

<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
    <startup>

        <supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5.2"/>
    </startup>
  <appSettings>
    <addkey="adapter"value="AdapterSample.OperationAdapter"/>
  </appSettings>
</configuration>

客户端代码:Program.cs

    classProgram
    {
        staticvoidMain(string[] args)
        {
            ScoreOperation operation;//抽象目标接口对象
            //读取配置文件
            stringadapterType =
ConfigurationManager.AppSettings["adapter"];
            //反射生成对象
           operation = (ScoreOperation)Assembly.Load("AdapterSample").CreateInstance(adapterType);
 
            int[]scores = { 84, 76, 50, 69, 90, 91, 88, 96 };
            int[]result;
            intscore;
 
            Console.WriteLine("成绩排序结果:");
           result = operation.Sort(scores);
 
            //遍历输出成绩
            foreach(inti
in result)
            {
               Console.Write(i +
",");
            }
            Console.WriteLine();
            Console.WriteLine("成绩为90:");
           score = operation.Search(result, 90);
            if (score!= 1)
            {
               Console.WriteLine("zhaodao90");
            }
            else
            {
               Console.WriteLine("meizhaodao90");
            }
            Console.Read();
        }
}

五、适配器模式的优点

1)将目标类和适配者类解耦;

2)增加了类的透明性和复用性;

3)灵活性和扩展性都非常好。

六、适配器模式的缺点

1)C#语言和Java语言不支持多重类继承的语言,不能同时适配多个适配者;

2)适配者类不能设置为最终的类,在C#中不能为sealed类;

3)C#和Java中类适配器模式中的目标抽象类只能为接口,不能为类,具有局限性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: