好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java之策略模式比较器案例讲解

Comparable 比较器,内置定义的比较方法,实现比较 较简单

Comparator 策略模式,需要定义不同的策略和比较的对象,实现比较 较复杂

打个比方,狗有foot一种属性我们用Comparable比较器完成比较

猫有height和weight两种属性,我们用Comparator策略模式完成比较

一、Comparable --狗比较

缺点:自定义排序规则,规则定义好之后,再改起来就不方便,还需要重新开发Sort比较类

1、狗对象

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package com.longze.guosh.strategy;

 

public class Dog implements Comparable<Dog> {

     int food;  //狗的饭量

     public Dog( int food){

         this .food=food;

     }

     @Override   //自定义排序规则,规则定义好之后,再改起来就不方便

     public int compareTo(Dog d) {

         if ( this .food<d.food) return - 1 ;

         else if ( this .food>d.food) return 1 ;

         else return 0 ;

     }

 

     @Override

     public String toString() {

         return "Dog{" +

                 "food=" + food +

                 '}' ;

     }

}

2、狗的比较类,也可以代表猫的汽车的比较类,但是比较策略无法修改((除非改原来的方法))

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

package com.longze.guosh.strategy;

 

import java.util.Comparator;

 

public class DogSorter {

     //排序

     public static void sort(Comparable[] arr){

         for ( int i= 0 ;i< arr.length- 1 ;i++){

             int minPos=i;

             for ( int j=i+ 1 ;j<arr.length;j++){

                 minPos=arr[j].compareTo(arr[minPos])==- 1 ?j:minPos;

             }

             swap(arr,i,minPos);

         }

     }

     //交换

     static void swap(Comparable[] arr, int i, int j){

         Comparable temp=arr[i];

         arr[i]=arr[j];

         arr[j]=temp;

     }

}

3、main方法验证

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.longze.guosh.strategy;

 

import java.util.Arrays;

 

public class Main {

     public static void main(String[] args) {

         Dog[] ds= { new Dog( 8 ), new Dog( 5 ), new Dog( 10 ), new Dog( 1 )};

         //comparater

         DogSorter dogsorter= new DogSorter();

         dogsorter.sort(ds);

         System.out.println( "Dogs===" +Arrays.toString(ds));

 

     }

}

二、Comparator 策略模式

优点,可以定义多种比较策略,不需要改sort比较类

1、猫对象

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.longze.guosh.strategy;

 

public class Cat{

     int weight,height;

     public Cat( int weight, int height){

         this .height=height;

         this .weight=weight;

     }

     @Override

     public String toString() {

         return "Cat{" +

                 "weight=" + weight +

                 ", height=" + height +

                 '}' ;

     }

}

2、猫的比较策略,可以有多种策略

如【CatHeightComparator.java】身高比较器  【CatWeightComparator】体重比较器

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.longze.guosh.strategy;

 

import java.util.Comparator;

 

public class CatHeightComparator implements Comparator<Cat> {

 

     @Override

     public int compare(Cat o1, Cat o2) {

         if (o1.height > o2.height) {

             return - 1 ;

         } else if (o1.height < o2.height) {

             return 1 ;

         } else {

             return 0 ;

         }

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.longze.guosh.strategy;

 

import java.util.Comparator;

 

public class CatWeightComparator implements Comparator<Cat> {

 

     @Override

     public int compare(Cat o1, Cat o2) {

         if (o1.weight < o2.weight) {

             return - 1 ;

         } else if (o1.weight > o2.weight) {

             return 1 ;

         } else {

             return 0 ;

         }

     }

}

3、比较器 也可以用作狗或者其他比较类,比较策略可以重新指定不同的

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

package com.longze.guosh.strategy;

 

import java.util.Comparator;

 

public class Sorter<T> {

 

     public void sort(T[] arr, Comparator<T> comparator){

         for ( int i= 0 ;i< arr.length- 1 ;i++){

             int minPos=i;

             for ( int j=i+ 1 ;j<arr.length;j++){

                 minPos=comparator.compare(arr[j],arr[minPos])==- 1 ?j:minPos;

             }

             swap(arr,i,minPos);

         }

     }

      void swap(T[] arr, int i, int j){

         T temp=arr[i];

         arr[i]=arr[j];

         arr[j]=temp;

     }

 

}

4、Main校验

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.longze.guosh.strategy;

 

import java.util.Arrays;

 

public class Main {

     public static void main(String[] args) {

         Cat[] cs={ new Cat( 3 , 3 ), new Cat( 5 , 5 ), new Cat( 1 , 1 ), new Cat( 10 , 10 )};

         //comparator

         Sorter<Cat> catsorter= new Sorter<>();

         catsorter.sort(cs, new CatHeightComparator());

         System.out.println( "Cat===" +Arrays.toString(cs));

 

     }

}

综上所述 使用简单比较器直接实现Comparable类,就可以完成

当使用策略模式时,需要实现不同的Comparator策略,配合Sort可以完成比较

Git地址:https://gitee.com/feng-qingxuan/dessign-pattrns.git  strategy

到此这篇关于Java之策略模式比较器案例讲解的文章就介绍到这了,更多相关Java之策略模式比较器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_36602951/article/details/119493586

查看更多关于Java之策略模式比较器案例讲解的详细内容...

  阅读:23次