[017]Java设计模式3——策略模式
来源:互联网 发布:学科数据分析 编辑:程序博客网 时间:2024/06/05 00:15
本节主要介绍Comparable和Comparator两个接口,同种比较方法,使用不同比较策略,多态化,程序更容易扩展、维护。
首先列出比较类DataSorter :
public class DataSorter { //冒泡排序,从小到大排序 public static void sort(Object[] a) { for(int i=a.length; i>0; i--) { for(int j=0; j<i-1; j++) { Comparable o1 = (Comparable)a[j]; Comparable o2 = (Comparable)a[j+1]; if(o1.compareTo(o2) == 1) { swap(a, j , j+1); } } } } private static void swap(Object[] a, int x, int y) { Object temp = a[x]; a[x] = a[y]; a[y] = temp; } public static void sort(int[] a) { for(int i=a.length; i>0; i--) { for(int j=0; j<i-1; j++) { if(a[j] > a[j+1]) { swap(a, j , j+1); } } } } private static void swap(int[] a, int x, int y) { int temp = a[x]; a[x] = a[y]; a[y] = temp; } public static void p(int[] a) { for(int i=0; i<a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void p(Object[] a) { for(int i=0; i<a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); }}
上述列出了两种sort()方法,一个参数为Object数组,一个为int类型数组,根据比较对象的不同,可以再写更多的sort函数,但显然这样不易扩展,sort函数内的逻辑是不变的,只变化参数类型,所以当然要用多态,使用一个sort函数,为了解决各种类型对象的比较,猫和狗都可以比较,所以添加一个Comparable接口,要比较的对象只要实现这个接口的compareTo()函数即可。
示例代码如下:
public interface Comparable { public int compareTo(Object o);}
public class Cat implements Comparable { //实现自定义的Comparable 接口 private int height; //不同比较规则,多态 private Comparator comparator = new CatHeightComparator(); public Comparator getComparator() { return comparator; } public void setComparator(java.util.Comparator comparator) { this.comparator = comparator; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public Cat(int height, int weight) { super(); this.height = height; this.weight = weight; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } private int weight; @Override public String toString() { return height + "|" + weight; } @Override public int compareTo(Cat o) { return comparator.compare(this, o); }}
这样猫类对象之间就可以比较了,具体的比较规则,我们可以自己定义,又用到了多态。为了扩展的方便,我们添加比较器Comparator,比较器规则只要实现该接口即可。
public interface Comparator { int compare(Object o1, Object o2);}
如下是两种比较规则,均实现Comparator 接口,有再多的比较规则,则可继续写。
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; return 0; }}
public class CatWeightComparator implements Comparator { @Override public int compare(Object o1, Object o2) { Cat c1 = (Cat)o1; Cat c2 = (Cat)o2; if(c1.getWeight() > c2.getWeight()) return -1; else if(c1.getHeight() < c2.getHeight()) return 1; return 0; }}
测试代码如下:
public class Test { public static void main(String[] args) { //int[] a = {9, 5, 3, 7, 1}; Cat[] a = {new Cat(5, 5), new Cat(3, 3), new Cat(1, 1)}; //Dog[] a = {new Dog(5), new Dog(3), new Dog(1)}; DataSorter.sort(a); //java.util.Arrays.sort(a); DataSorter.p(a); }}
其实jdk已经给我们提供好了比较函数java.util.Arrays.sort(Object a)
,但是一样Java文档里明确写着数组对象a的元素要实现Comparable 接口。
0 0
- [017]Java设计模式3——策略模式
- Java设计模式—策略
- Java设计模式——策略模式
- JAVA设计模式——策略模式
- java设计模式——策略模式
- JAVA设计模式——策略模式
- Java设计模式——策略模式
- Java设计模式——策略模式
- java设计模式——策略模式
- Java设计模式——策略模式
- Java设计模式——策略模式
- Java设计模式——策略模式
- Java设计模式——策略模式
- java设计模式——策略模式
- java设计模式——策略模式
- Java设计模式——策略模式
- java设计模式——策略模式
- Java设计模式——策略模式
- 也谈JVM垃圾回收
- 用Qt实现一个桌面弹幕程序(四)--实现一个弹幕③
- 【图像处理】相机成像原理
- Android 删除手机相册中的图片
- Demo2:简单求职表格
- [017]Java设计模式3——策略模式
- Android 个人开发者接入支付功能
- Handler、Loop、MessageQueue的工作原理
- &qt;<等xml|Html转义字符
- Nagios远程监控windows方案补充说明
- Mac下Android Studio使用github
- leetcode 10. Regular Expression Matching
- 【C语言】PCM音频数据处理---音量增大或减小
- c++中的引用