乐趣区

关于java:Java之策略模式比较器案例讲解

Comparable 比拟器,内置定义的比拟办法,实现比拟 较简略

Comparator 策略模式,须要定义不同的策略和比拟的对象,实现比拟 较简单

打个比方,狗有 foot 一种属性咱们用 Comparable 比拟器实现比拟

猫有 height 和 weight 两种属性,咱们用 Comparator 策略模式实现比拟

一、Comparable – 狗比拟
毛病:自定义排序规定,规定定义好之后,再改起来就不不便,还须要从新开发 Sort 比拟类

1、狗对象

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、狗的比拟类,也能够代表猫的汽车的比拟类,然而比拟策略无奈批改((除非改原来的办法))

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 办法验证
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、猫对象
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】体重比拟器
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;}
}

}

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、比拟器 也能够用作狗或者其余比拟类,比拟策略能够从新指定不同的

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 校验

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 能够实现比拟

退出移动版