设计模式之适配器模式
1、类图
2、创建项目
…………………………
3、 新建ScoreOperation:抽象成绩操作类,充当目标接口
namespace AdapterSample
{
/// <summary>
/// 成绩操作类:用户希望的接口方法
/// </summary>
interface ScoreOperation
{
/// <summary>
/// 成绩排序
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
int[] Sort(int[] array);
/// <summary>
/// 成绩查找
/// </summary>
/// <param name="array"></param>
/// <param name="key"></param>
/// <returns></returns>
int Search(int[] array, int key);
}
}
4、 新建QuickSortClass:快速排序类,充当适配者
namespace AdapterSample
{
/// <summary>
/// 快速排序类
/// </summary>
class QuickSortClass
{
/// <summary>
/// 实现对数组的快速排序
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
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[] a, int p, int r)
{
int x = a[r];
int j = p - 1;
for (int i = p;i <= r - 1;i++)
{
if (a[i] <= x)
{
j++;
Swap(a,j,i);
}
}
Swap(a,j + 1,r);
return j + 1;
}
public void Swap(int[] a, int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
5、 新建BinarySearchClass:二分查找类,充当适配者
namespace AdapterSample
{
class BinarySearchClass
{
/// <summary>
/// 二分查找,如果找到,返回1,找不到返回-1
/// </summary>
/// <param name="array">查找的数组</param>
/// <param name="key">查找的关键字</param>
/// <returns></returns>
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
}
}
}
6、 新建OperationAdapter:操作适配器,充当适配器
namespace AdapterSample
{
/// <summary>
/// 适配器类:本类为对象适配器,在一个系统中可以根据需要定义多个适配器类,以实现不同的方法
/// </summary>
class OperationAdapter : ScoreOperation
{
//维护与适配者之间的关联关系
private QuickSortClass sortObj; //定义适配者QuickSortClass对象
private BinarySearchClass searchObj; //定义适配者BinarySearchClass对象
/// <summary>
/// 构造函数,初始化适配者对象
/// </summary>
public OperationAdapter()
{
sortObj = new QuickSortClass();
searchObj = new BinarySearchClass();
}
#region 实现用户的接口方法
/// <summary>
/// 排序方法实现
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public int[] Sort(int[] array)
{
return sortObj.QuickSort(array); //调用适配者类QuickSortClass的排序方法
}
/// <summary>
/// 实现成绩查找方法
/// </summary>
/// <param name="array"></param>
/// <param name="key"></param>
/// <returns></returns>
public int Search(int[] array, int key)
{
return searchObj.BinarySearch(array, key); //调用适配者类BinarySearchClass的查找方法
}
#endregion
}
}
7、 配置文件App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="adapter" value="AdapterSample.OperationAdapter"/>
</appSettings>
</configuration>
8、 Program:客户端测试类
using System;
using System.Configuration;
using System.Reflection;
namespace AdapterSample
{
class Program
{
static void Main(string[] args)
{
ScoreOperation operation; //针对抽象目标接口编程
//读取配置文件
string adapterType = ConfigurationManager.AppSettings["adapter"];
//反射生成对象
operation = (ScoreOperation)Assembly.Load("AdapterSample").CreateInstance(adapterType);
int[] scores = {84,76,50,69,90,91,88,96}; //定义成绩数组
int[] result;
int score;
Console.WriteLine("成绩排序结果:");
result = operation.Sort(scores);
//遍历输出成绩
foreach (int i in result)
{
Console.Write(i + ",");
}
Console.WriteLine();
Console.WriteLine("查找成绩90:");
score = operation.Search(result,90);
if (score != -1)
{
Console.WriteLine("找到成绩90。");
}
else
{
Console.WriteLine("没有找到成绩90。");
}
Console.WriteLine("查找成绩92:");
score = operation.Search(result,92);
if (score != -1)
{
Console.WriteLine("找到成绩92。");
}
else
{
Console.WriteLine("没有找到成绩92。");
}
Console.Read();
}
}
}
9、 结果及分析