0 设计模式
不了解设计模式的小伙伴可以通过这篇文章了解一下什么是设计模式
https://blog.csdn.net/qq_42874315/article/details/120006447?spm=1001.2014.3001.5502
1 策略模式
策略模式是一种比较的思想,就像日常生活中遇到什么样的事情,就选择相应的解决办法(策略)去应对它一样。
本文以比较器(Comparator/Comparable)为例详细的介绍一下策略模式,当然策略模式不单单是比较器这么一种,也可以使用策略模式+工厂模式替代代码中的if-else。
使用策略模式+工厂模式替代代码中的if-else:https://blog.csdn.net/qq_42874315/article/details/119877790
2 实现思路
2.1 Comparable实现思路
Comparable主要用在两个对象之间的比较,像是自带的比较规则,在类中定义的比较规则
2.2 Comparator实现思路
Comparator主要用在多个对象的比较,通常会将需要比较的对象数组和对应对象类型的比较器传入到一个排序比较的方法中,一般用于多个相同的类进行比较排序使用。
3 需要的类
3.1 Comparable需要的类
- 比较接口
- 实现比较接口的具体实现类(被比较的对象)
- 测试类
3.2 Comparator需要的类
-
实体类(被比较的对象,一般来说也会实现Comparable接口,这里为了避免大家混淆,我就不实现Comparable了)
-
比较器接口
-
实现比较器接口的具体比较器
-
排序类
传入要排序的对象数组和比较器
-
测试类(定义对象数组,创建比较器,传入排序类)
4 具体实现
4.1 Comparable具体实现
4.1.1 定义Comparable接口
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 16:54
*/
public interface Comparable<T> {
int compareTo(T o);
}
4.1.2 Comparable接口的实现类
Cat
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 16:54
*/
public class Cat implements Comparable<Cat> {
int weight;
int height;
public Cat(int weight, int height) {
this.weight = weight;
this.height = height;
}
/**
* 定义一下两只猫怎么比较大小
* @param o 比较的Cat对象
* @return 利用数字判断大小
*/
@Override
public int compareTo(Cat o) {
if(this.weight < o.weight){
return -1;
}else if (this.weight > o.weight){
return 1;
}else{
return 0;
}
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}';
}
}
Dog
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 16:54
*/
public class Dog implements Comparable<Dog>{
int food;
public Dog(int food) {
this.food = food;
}
/**
* 定义一下两只狗怎么比较大小
* @param d 比较的Dog对象
* @return 利用数字判断大小
*/
@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 +
'}';
}
}
4.1.3 测试类
/**
* 使用comparable
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 16:58
*/
public class Test {
public static void main(String[] args) {
// 使用Comparable比较两只猫
Cat cat = new Cat(15, 15);
Cat cat1 = new Cat(16, 16);
int retValue = cat.compareTo(cat1);
System.out.println(retValue);
// 使用Comparable比较两只狗
Dog dog = new Dog(15);
Dog dog1 = new Dog(16);
int retValue1 = dog.compareTo(dog1);
System.out.println(retValue1);
}
}
4.2 Comparator具体实现
4.2.1 比较的实体类
一般来说也会实现Comparable接口,这里为了避免大家混淆,我就不实现Comparable了
Cat
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 16:54
*/
public class Cat {
int weight;
int height;
public Cat(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}';
}
}
Dog
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 16:54
*/
public class Dog {
int food;
public Dog(int food) {
this.food = food;
}
@Override
public String toString() {
return "Dog{" +
"food=" + food +
'}';
}
}
4.2.2 Comparator接口
/**
* 比较器
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 17:02
*/
public interface Comparator<T> {
int compare(T o1,T o2);
}
4.2.3 Comparator接口实现类
CatComparator(Cat类的比较器)
/**
* Cat比较器
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 17:03
*/
public class CatComparator 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;
}
}
}
DogComparator(Dog类的比较器)
/**
* Dog比较器
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 17:03
*/
public class DogComparator implements Comparator<Dog> {
@Override
public int compare(Dog o1, Dog o2) {
if (o1.food < o2.food){
return -1;
}else if (o1.food > o2.food){
return 1;
}else{
return 0;
}
}
}
4.2.4 Sorter(排序类,传入对象数组以及对应对象的比较器)
/**
* 排序类
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 17:07
*/
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.2.5 测试类
/**
* 使用Comparator
* @Author ChenJiahao(程序员五条)
* @Date 2021/9/12 17:08
*/
public class Test {
public static void main(String[] args) {
// 使用Comparator
Cat[] a = {new Cat(3,3),new Cat(5,5),new Cat(1,1)};
Sorter<Cat> sorter = new Sorter<>();
sorter.sort(a,new CatComparator());
System.out.println(Arrays.toString(a));
}
}
5 总结
- 如果只需要两个相同对象比较的话,只需要实现Comparable就够了
- 如果只需要多个相同对象比较的话,实现Comparator就可以了
- 如果既需要两个相同对象比较,也需要多个相同对象比较的话,只实现Comparator其实也够了,但是进行两两比较的时候不是很方便(看自己抉择了),也可以用实体类去实现Comparable,进行两两比较
6 思维导图
7 示例源码地址
https://github.com/ChenJiahao0205/design-pattern/tree/master
最后
我是通过马士兵老师的视频和菜鸟教程学习的,部分内容可能会有雷同
想阅读更多设计模式相关文章,欢迎到我的专栏【设计模式学习笔记】、【设计模式】中去查看
在23篇设计模式文章发布完成之后,我会公开完整的思维导图,点关注,不迷路
感谢大家看到这里,文章如有不足,欢迎大家指出;彦祖点个赞吧彦祖点个赞吧彦祖点个赞吧,欢迎关注程序员五条!