<dl id="xausj"></dl>
    1. <ul id="xausj"><th id="xausj"></th></ul>

      <b id="xausj"></b>
      <cite id="xausj"></cite>
      <b id="xausj"><meter id="xausj"></meter></b>
      <dl id="xausj"><noframes id="xausj"></noframes></dl>
    2. 您好,歡迎訪問上海意泓電子科技有限責(zé)任公司網(wǎng)站!
      4新聞資訊
      您的位置: 首頁 ->  新聞資訊 -> 電源

      ?適配器模式實(shí)例之算法適配

      文章出處:電源 責(zé)任編輯:上海意泓電子科技有限責(zé)任公司 發(fā)表時(shí)間:
      2018
      01-15

      在計(jì)算機(jī)編程中,適配器模式(有時(shí)候也稱包裝樣式或者包裝)將一個(gè)類的接口適配成用戶所期待的。一個(gè)適配允許通常因?yàn)榻涌诓患嫒荻荒茉谝黄鸸ぷ鞯念惞ぷ髟谝黄穑龇ㄊ菍㈩愖约旱慕涌诎谝粋€(gè)已存在的類中。


      將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作?!狦ang of Four


      基本概念


      客戶:需要調(diào)用我們的代碼的對象。


      Adapter模式的宗旨:保留現(xiàn)有類所提供的服務(wù),向客戶提供接口,以滿足客戶的期望。


      主要內(nèi)容


      (1)類適配器


      當(dāng)客戶在接口中定義了他期望的行為時(shí),我們就可以應(yīng)用適配器模式,提供一個(gè)實(shí)現(xiàn)該接口的類,并且擴(kuò)展已有的類,通過創(chuàng)建子類來實(shí)現(xiàn)適配。


      下面是類適配器的UML圖:


      (2)對象適配器:


      對象適配器”通過組合除了滿足“用戶期待接口”還降低了代碼間的不良耦合。在工作中推薦使用“對象適配”。下面是對象適配器的UML圖:


      (3) 缺省適配器模式:


      缺省適配器模式是一種特殊的適配器模式,但這個(gè)適配器是由一個(gè)抽象類實(shí)現(xiàn)的,并且在抽象類中要實(shí)現(xiàn)目標(biāo)接口中所規(guī)定的所有方法,但很多方法的實(shí)現(xiàn)都是“平庸”的實(shí)現(xiàn),也就是說,這些方法都是空方法。而具體的子類都要繼承此抽象類。


      結(jié)構(gòu)型模式之對象適配器模式


      現(xiàn)有一個(gè)接口DataOperaTIon定義了排序方法sort(int[])和查找方法search(int[],int),已知類QuickSort的quickSort(int[])方法實(shí)現(xiàn)了快速排序算法,類BinarySearch的binarySearch(int[],int)方法實(shí)現(xiàn)了二分查找算法?,F(xiàn)使用適配器模式設(shè)計(jì)一個(gè)系統(tǒng),在不修改源代碼的情況下將類QuickSort和類BinarySearch的方法適配到DataOperaTIon接口中。繪制類圖并編程實(shí)現(xiàn)。


      代碼


      [java] view plain copypackage 適配器模式實(shí)例之算法適配;


      public interface DataOperaTIon { //目標(biāo)類


      public void sort(int sort[], int i, int j);


      public int search(int search[], int n);


      }


      [java] view plain copypackage 適配器模式實(shí)例之算法適配;


      public class AlgoTIthmAdapter implements DataOperation{ //適配器類


      private QuickSort quick;


      private BinarySearch binary;


      public AlgotithmAdapter(QuickSort quick) {


      this.quick = quick;


      }


      public AlgotithmAdapter(BinarySearch binary) {


      this.binary = binary;


      }


      public void sort(int sort[], int i, int j) {


      quick.quickSort(sort, i, j);


      }


      public int search(int search[], int n) {


      return binary.binarySearch(search, n);


      }


      }


      [java] view plain copypackage 適配器模式實(shí)例之算法適配;


      public class QuickSort { //適配者類


      //劃分?jǐn)?shù)組


      int partion(int array[], int p, int r) {


      int x = array[r];


      int i = p - 1;//注意這點(diǎn),把i設(shè)成負(fù)值,然后作為移動的標(biāo)志


      int j;


      for (j = p; j 《 r; j++) {


      if (array[j] 《= x) {


      i++;


      int temp = array[j];


      array[j] = array[i];


      array[i] = temp;

      }


      }


      int temp = array[j];


      array[j] = array[i + 1];


      array[i + 1] = temp;


      return i+1;//返回的應(yīng)該是交換后的哨兵的位置


      }


      //遞歸解決每個(gè)劃分后的小數(shù)組


      void quickSort(int[] array, int p, int r) {


      if (p 《 r) {


      int q = partion(array, p, r);


      quickSort(array, p, q - 1);


      quickSort(array, q + 1, r);


      }


      }


      }


      [java] view plain copypackage 適配器模式實(shí)例之算法適配;


      public class BinarySearch { //適配者類


      public int binarySearch(int[] srcArray, int des){


      int low = 0;


      int high = srcArray.length-1;


      while(low 《= high) {


      int middle = (low + high)/2;


      if(des == srcArray[middle]) {


      return middle;


      }else if(des 《srcArray[middle]) {


      high = middle - 1;


      }else {


      low = middle + 1;


      }


      }


      return -1;


      }


      }


      [java] view plain copypackage 適配器模式實(shí)例之算法適配;


      public class Client { //客戶端類


      public static void main(String[] args) {


      int[] array = { 4, 3, 5, 2, 3, 6, 8, 9, 18, 12, 53, 20};


      int i;


      System.out.print(“排序前:”);


      for (i=0; i《array.length; i++)


      System.out.print(array[i]+“ ”);


      BinarySearch binary = new BinarySearch();


      AlgotithmAdapter algotithm1 = new AlgotithmAdapter(binary);


      System.out.println(“\n通過二分查找得到數(shù)字5:位于數(shù)組的第”+

      (algotithm1.search(array, 5)+1)+“位”);


      QuickSort sort = new QuickSort();


      AlgotithmAdapter algotithm2 = new AlgotithmAdapter(sort);


      algotithm2.sort(array, 0, array.length - 1);


      System.out.println(“------------------------------”);


      System.out.print(“排序后:”);


      for (i=0; i《array.length; i++)


      System.out.print(array[i]+“ ”);


      //int[] src = new int[] {1, 3, 5, 7, 8, 9};


      System.out.println(“\n通過二分查找得到數(shù)字5:位于數(shù)組的第”+

      (algotithm1.search(array, 5)+1)+“位”);


      }


      }

      上海意泓電子科技有限責(zé)任公司 版權(quán)所有 未經(jīng)授權(quán)禁止復(fù)制或鏡像

      CopyRight 2020-2025 m.pendragonrpg.com All rights reserved   滬ICP備2021005866號

      国产中文欧美日韩,色播在线永久免费视频,另类专区亚洲无码,亚洲中文字无码av

      <dl id="xausj"></dl>
      1. <ul id="xausj"><th id="xausj"></th></ul>

        <b id="xausj"></b>
        <cite id="xausj"></cite>
        <b id="xausj"><meter id="xausj"></meter></b>
        <dl id="xausj"><noframes id="xausj"></noframes></dl>