针对上一篇文章 "剑指offer 面试题14 把数组所有偶数放在奇数后面" http://blog.youkuaiyun.com/nx188/article/details/51599210
如果需要改成"负数在正数前面"或"被3整除的排不被3整除的前面", 则需要改函数,如何让其具有可扩展性, 即用下面的方法,在判断是正数 / 负数, 奇数 / 偶数时,
将方法抽象出来,其他代码复用.抽象出来的方法,用反射方式将类forName 注入, 而后反射invoke 方式调用判断奇数偶数 / 正数负数的方法.实现可重用 / 可扩展性.
原代码不需要改,只需要注入类即可.而注入类的类名可以写在配置文件,这样java 代码均不需要改,这就是可重用性可扩展性.
代码如下:
ExtensibilityArrayService 类:
package com.extensibility.reusability;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 包名的意思是可重用性可扩展性
* 类名是可扩展的数组服务,针对数组奇数排偶数前面,负数在正数前面,被3整除的排不被3整除的前面,这一类问题的解决
* 此类是针对Move2NumberAfter 的扩展Move2NumberAfter 类只做到了"数组奇数排偶数前面"这一点
* @author Administrator
*
*/
public class ExtensibilityArrayService {
private static void display(int[] array) {
for(int i = 0;i<array.length;i++)
System.out.println(array[i]);
}
/**
* 第一位和最后一位分别两个指针,类似快排的一趟,时间复杂度是O(n)
* @param array
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private static void twoPointer(int[] array) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
// Class compareFuncClass = Class.forName("com.extensibility.reusability.NumberOf2");//这里注入类,只要改这一处,功能改变,实现可扩展
// Class compareFuncClass = Class.forName("com.extensibility.reusability.Divide3");
Class compareFuncClass = Class.forName("com.extensibility.reusability.NegtiveAndPositive");
Object compareFuncInstance = compareFuncClass.newInstance();
Method isEvenMethod = compareFuncClass.getMethod("isEven", int.class);
int firstPointer = 0;
int lastPointer = array.length-1;
while(firstPointer < lastPointer ) {
while(firstPointer < lastPointer && !(boolean)isEvenMethod.invoke(compareFuncInstance, array[firstPointer]))
firstPointer++;
while(firstPointer < lastPointer && (boolean)isEvenMethod.invoke(compareFuncInstance, array[lastPointer]))
lastPointer--;
if(firstPointer < lastPointer)
exchange(array, firstPointer, lastPointer);
}
}
private static void exchange(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
int array[] = {3,1,2,5,7,8,-9,3,-10,1};//3,1,2,5,7,8,9,3,10,1
twoPointer(array);
display(array);
}
}
NumberOf2 类:
package com.extensibility.reusability;
public class NumberOf2 {
public boolean isEven(int number) {
if(number % 2 == 0)
return true;
else
return false;
}
}
Divide3 类:
package com.extensibility.reusability;
public class Divide3 {
public boolean isEven(int number) {
if(number % 3 == 0)
return true;
else
return false;
}
}
NegtiveAndPositive 类:
package com.extensibility.reusability;
/**
* NegtiveAndPositive 意思是负数正数
* @author Administrator
*
*/
public class NegtiveAndPositive {
public boolean isEven(int number) {
if(number > 0)
return true;
else
return false;
}
}