1.Cat类
package com.design.strategy;
public class Cat {
public int weight, height;
public Cat(int weight, int height) {
this.weight = weight;
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}';
}
}
2.Dog类
package com.design.strategy;
public class Dog {
public int food;
public Dog(int food) {
this.food = food;
}
@Override
public String toString() {
return "Dog{" +
"food=" + food +
'}';
}
}
3.Comparator接口
package com.design.strategy;
public interface Comparator<T> {
int compare(T o1,T o2);
}
4.CatHightComparator
package com.design.strategy;
public class CatHightComparator implements Comparator<Cat> {
@Override
public int compare(Cat o1, Cat o2) {
if (o1.height < o2.height) return -1;
else if (o1.height > o1.height) return 1;
else return 0;
}
}
5.CatWeightCompatator
package com.design.strategy;
public class CatWeightCompatator 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;
}
}
6.DogFoodComparator
package com.design.strategy;
public class DogFoodComparator 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;
}
}
7.Sort
package com.design.strategy;
import java.util.Arrays;
public class Sort<T> {
public void sort(T[] arr, Comparator<T> comparator){
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i+1; j < arr.length; j++) {
minPos = comparator.compare(arr[j], arr[i]) == -1 ? j : minPos;
}
swap(arr,i,minPos);
}
System.out.println(Arrays.toString(arr));
}
public void swap(T[] arr,int i,int j){
T temp = arr[i];
arr[i] =arr[j];
arr[j] = temp;
}
}
8.MAIN
package com.design.strategy;
public class MAIN {
public static void main(String[] args) {
Cat[] cats = new Cat[]{new Cat(1, 2), new Cat(3, 5), new Cat(2, 3)};
Dog[] dogs = new Dog[]{new Dog(1), new Dog(5), new Dog(3), new Dog(8)};
Sort sort = new Sort();
sort.sort(cats, new CatHightComparator());
sort.sort(cats, new CatWeightCompatator());
sort.sort(dogs, new DogFoodComparator());
}
}
/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=61027:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/rt.jar:/Users/chenjingjing/Downloads/JavaSourceCodeLearning-master/Algorithm/target/classes com.design.strategy.MAIN
[Cat{weight=1, height=2}, Cat{weight=2, height=3}, Cat{weight=3, height=5}]
[Cat{weight=1, height=2}, Cat{weight=2, height=3}, Cat{weight=3, height=5}]
[Dog{food=1}, Dog{food=3}, Dog{food=5}, Dog{food=8}]
Process finished with exit code 0
9.总结:
策略模式解决一种行为,不同策略,不同实现方式。解决if,switch代码问题。