程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 設計模式之適配器模式,設計模式適配器

設計模式之適配器模式,設計模式適配器

編輯:關於.NET

設計模式之適配器模式,設計模式適配器


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、 結果及分析

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved