开篇提神,多态,父类引用指向子类对象,其最主要功能就是功能拓展!
开始正文,分别模仿jdk中Comparable接口以及Comparato对实现此接口的类进行排序操作,通过对比以此引出策略模式.
首先,对Cat类实现Comparable接口进行排序操作,观察这种排序方式的不足,以下为代码片段.
/**
* @Title: Comparable
* @Description:自定义的Comparable接口
* @Auther:
* @Version: 1.0
* @create 2019/4/2 22:15
*/
public interface Comparable {
//>0,比较的对象大,<0,被比较的对象大
int compareTo(Object o);
}
/**
* @Title: Cat
* @Description:Cat类实现Comparable接口
* @Auther:
* @Version: 1.0
* @create 2019/4/2 22:02
*/
public class Cat implements Comparable{
private int height;
private int weight;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Cat{" +
"height=" + height +
", weight=" + weight +
'}';
}
public Cat(int height, int weight) {
this.height = height;
this.weight = weight;
}
@Override
public int compareTo(Object o) {
if(o instanceof Cat){
Cat c = (Cat) o;
if(this.weight>c.weight) return 1;
else if (this.weight<c.weight) return -1;
else return 0;
}
return -100;
}
}
/**
* @Title: DataSource
* @Description:通用的对象排序类
* @Auther:
* @Version: 1.0
* @create 2019/4/2 21:33
*/
public class DataSorter {
public static void sort(Object[] objects){
for (int i = 1; i <=objects.length-1; i++) {
for (int n = 1; n <= objects.length - i; n++) {
Comparable c1 = (Comparable) objects[n - 1];
Comparable c2 = (Comparable) objects[n];
//如果第一个对象大于第二个对象,那么就交换位置
if (c1.compareTo(c2) == 1) {
swap(objects,n-1,n);
}
}
}
}
private static void swap(Object[] objects, int a, int b) {
Object temp = objects[a];
objects[a]=objects[b];
objects[b]=temp;
}
public static void print(Object[] objects) {
for (int i = 0; i <objects.length ; i++) {
System.out.println(objects[i].toString());
}
}
}
这种方式虽然实现了对通用对象的排序,只要该对象实现Comparable接口,并且有自己的实现方式,但缺点在于排序方式固定,以上面代码为例,猫以重量排序在实现里面是写死的,那如果我下次要以身高为排序,怎么办?或者以更多的混合方式排序,如何做?那下面就以Compartor接口的方式通过写不同的排序比较器来完成策略模式.其实像商场打折,线上活动打折之类的都可以借鉴这种模式,因为不可能你一改变打折方式就要改变源码,那样累不累?
以下为策略模式的代码片段:
/**
* @Title: Comparator
* @Description:自己模拟的Comparator接口
* @Auther:
* @Version: 1.0
* @create 2019/4/2 23:43
*/
public interface Comparator {
int compare(Object o1,Object o2);
}
/**
* @Title: CatHeightComparator
* @Description:实现Comparator 接口的猫的身高排序比较器
* @Auther:
* @Version: 1.0
* @create 2019/4/2 23:45
*/
public class CatHeightComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Cat c1 = (Cat) o1;
Cat c2 = (Cat) o2;
if(c1.getHeight()>c2.getHeight()) return 1;
else if(c1.getHeight()<c2.getHeight()) return -1;
else if(c1.getHeight()==c2.getHeight()) return 0;
else return -100;
}
}
**
* @Title: Cat
* @Description:Cat类的具体实现,通过多态的方式定义身高比较器字段
* @Auther:
* @Version: 1.0
* @create 2019/3/31 22:02
*/
public class Cat implements Comparable{
private int height;
private int weight;
//猫的排序比较器
private Comparator comparator = new CatHeightComparator();
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Cat{" +
"height=" + height +
", weight=" + weight +
'}';
}
public Cat(int height, int weight) {
this.height = height;
this.weight = weight;
}
@Override
public int compareTo(Object o) {
return comparator.compare(this,o);
}
}
如果下次还要有其他的排序方式,那就在写另一个比较器类,通过注入对象的方式完成代码的复用!