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