java學習

          java學習

           

          設計模式之策略模式

          使用策略模式自定義對象排序和比較屬性值排序的例子
          /**
           * 
           * 實體類的接口
           *
           */
          public interface DongWu {
          public int getAge();
          public void setAge(int age) ;
          public int getHigh();
          public void setHigh(int high) ;
          }
          /**
           * 
           * 排序接口
           *
           */
          public interface MyComparator  {
          int comparatorTo(Object object);
          }
          /**
           * 
           * 具體的排序接口,和具體的實體類對象無關
           *
           */
          public interface TypeComparator  {
          int comparatorTo(Object object1,Object object2);
          }
          /**
           * 
           * 按照年紀倒敘排序
           *
           */
          public class AgeDescTypeComparator implements TypeComparator {
          @Override
          public int comparatorTo(Object object1, Object object2) {
          DongWu c1 = (DongWu) object1;
          DongWu dw = (DongWu) object2;
          if (c1.getAge() > dw.getAge()) {
          return -1;
          } else if (c1.getAge() < dw.getAge()) {
          return 1;
          }
          return 0;
          }
          }
          /**
           * 
           * 按照年紀正敘排序
           *
           */
          public class AgeTypeComparator implements TypeComparator {
          @Override
          public int comparatorTo(Object object1, Object object2) {
          DongWu c1 = (DongWu) object1;
          DongWu dw = (DongWu) object2;
          if (c1.getAge() > dw.getAge()) {
          return 1;
          } else if (c1.getAge() < dw.getAge()) {
          return -1;
          }
          return 0;
          }
          }
          public class Cat implements MyComparator,DongWu{
          private int age;
          private int high;
          private TypeComparator typeComparator =new AgeDescTypeComparator();
          public Cat(){}
          public Cat(int age){
          this.age=age;
          }
          public int getAge() {
          return age;
          }
          public void setAge(int age) {
          this.age = age;
          }
          public int getHigh() {
          return high;
          }
          public void setHigh(int high) {
          this.high = high;
          }
          @Override
          public int comparatorTo(Object object) {
          DongWu cat = (DongWu) object;
          return typeComparator.comparatorTo(this, cat);
          }
          }
          public class Dog implements MyComparator,DongWu{
          private int age;
          private int high;
          private TypeComparator typeComparator =new AgeDescTypeComparator();
          public Dog(){}
          public Dog(int age){
          this.age=age;
          }
          public int getAge() {
          return age;
          }
          public void setAge(int age) {
          this.age = age;
          }
          public int getHigh() {
          return high;
          }
          public void setHigh(int high) {
          this.high = high;
          }
          @Override
          public int comparatorTo(Object object) {
          DongWu d = (DongWu) object;
          return typeComparator.comparatorTo(this, d);
          }
          }
          public class DataSorter {
          public static void sort(MyComparator[] arr){
          for (int i = arr.length; i >0 ; i--) {
          for(int j=0;j<i-1;j++){
          MyComparator m1=arr[j];
          MyComparator m2= arr[j+1];
          if(m1.comparatorTo(m2)==1){
          swap(arr,j,j+1);
          }
          }
          }
          }
          private static void swap(Object[] arr, int j, int i) {
          Object temp =arr[j];
          arr[j]=arr[j+1];
          arr[j+1]=temp;
          }
          }

          posted on 2017-09-13 11:06 楊軍威 閱讀(114) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 浏阳市| 莆田市| 杂多县| 仁怀市| 洞口县| 孝义市| 南木林县| 昭觉县| 锡林郭勒盟| 六盘水市| 丹东市| 海林市| 永福县| 昭平县| 英德市| 德江县| 阳春市| 茌平县| 五莲县| 寻乌县| 临颍县| 天镇县| 阿图什市| 新源县| 乐业县| 广灵县| 巴中市| 鄂伦春自治旗| 遵化市| 苍梧县| 临夏市| 农安县| 阳信县| 越西县| 贡山| 汪清县| 栖霞市| 安龙县| 托里县| 三江| 大连市|