Java中的comparable和comparator
定义了一族算法(业务规则);
封装了每个算法;
这族的算法可互换代替(interchangeable)
comparable
public interface Comparable<T> {//在泛型中指定自己的类型
int compareTo(T o);
}
comparator
public interface Comparator<T> {
int compare(T o1, T o2);
}
cat
public class Cat implements Comparable<Cat> {
int weight, height;
public Cat(int weight, int height) {
this.weight = weight;
this.height = height;
}
public int compareTo(Cat cat) {
if (this.weight < cat.weight) {
return -1;
} else if (this.weight > cat.weight) {
return 1;
} else return 0;
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}';
}
}
CatComparator
public class CatComparator implements Comparator<Cat> {
@Override
public int compare(Cat o1, Cat o2) {
return o2.weight - o1.weight;
}
}
dog
public class Dog implements Comparable<Dog> {
int food;
public Dog(int food) {
this.food = food;
}
@Override
public int compareTo(Dog dog) {
if (this.food < dog.food) return -1;
else if (this.food > dog.food) return 1;
else return 0;
}
@Override
public String toString() {
return "Dog{" +
"food=" + food +
'}';
}
}
DogComparator
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;
}
}
Sorter 利用传入的数据类型自己的比较方法进行排序
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;
}
}
Main
public class Main {
public static void main(String[] args) {
Dog[] dogs = {new Dog(1), new Dog(9), new Dog(2)};
Cat[] cats = {new Cat(1,1),new Cat(3,3),new Cat(2,2)};
Sorter<Dog> dogSorter = new Sorter<>();
Sorter<Cat> catSorter = new Sorter();
dogSorter.sort(dogs,new DogComparator());
catSorter.sort(cats,new CatComparator());
System.out.println(Arrays.toString(dogs));
for (Cat cat : cats) {
System.out.println(cat);
}
}
}
给坦克的射击重构为策略模式
接口FireStrategy
public interface FireStrategy {
void fire(Tank t);
}
默认射击方法
+public class DefaultFireStrategy implements FireStrategy{
@Override
public void fire(Tank t) {
int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
new Bullet(bX, bY, t.dir, t.group, t.tf);
if (t.group == Group.GOOD) new Thread(() -> new Audio("audios/tank_fire.wav").play()).start();
}
}
四个方向射击方法
public class DefaultFireStrategy implements FireStrategy{
@Override
public void fire(Tank t) {
int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
new Bullet(bX, bY, t.dir, t.group, t.tf);
if (t.group == Group.GOOD) new Thread(() -> new Audio("audios/tank_fire.wav").play()).start();
}
}
给坦克加上属性FireStrategy
己方坦克使用四个方向的射击策略
敌方坦克使用一个方向的策略
if (group == Group.GOOD) {
String goodFSName = (String) PropertyManager.get("goodFS");
try {
fs = (FireStrategy) Class.forName(goodFSName).getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
} else {
String badFS = (String) PropertyManager.get("badFS");
try {
fs = (FireStrategy) Class.forName(badFS).getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
}
config
#fireStrategy
goodFS=com.xxx.tank.FourDirFireStrategy
badFS=com.xxx.tank.DefaultFireStrategy