实现可重用性即可扩展性 针对剑指offer 面试题14 把数组所有偶数放在奇数后面

针对上一篇文章 "剑指offer 面试题14 把数组所有偶数放在奇数后面" http://blog.csdn.net/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;
	}
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值