一个重构的简单例子


import java.util.*;
//重构前:根据所给出的数字n,返回1-n之间的所有质数.
class GeneratorPrimes
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
int[] a = PrimeGenerator.generate(10000000);
long end = System.currentTimeMillis();
//System.out.println(Arrays.toString(a));
System.out.println(end - start);
}
public static int[] generate(int maxValue) {
if (maxValue >= 2)
{
int s = maxValue + 1;
boolean[] f = new boolean[s];
int i;
for (i =0; i < s; i++)
{
f[i] = true;
}
f[0] = false;
f[1] = false;
int j;
for (i = 2; i < Math.sqrt(s) + 1; i++)
{
//此处对标识为true的元素不再进行标记.
if (f[i])
{
for (j = 2 * i; j < s; j += i)
{
f[j] = false;
}
}

}
int count = 0;
for (i = 0; i < s; i++)
{
if (f[i])
{
count++;
}
}
int[] primes = new int[count];
for (i = 0, j = 0; i < s; i++)
{
if (f[i])
{
primes[j++] = i;
}
}

return primes;
} else {
return new int[0];
}
}
}


/*
*重构后:根据所给出的数字n,返回1-n之间的所有质数.
*
*步骤:通过一个长度为这个数的boolean数组标识出这个数字之间的所有数字.
* 1-n之间的数字进行过滤,凡是能够分解质因数n*(2..n)都标记为true,表示已被过滤掉.
* 重复执行此步骤,至到上限值.
*/
class PrimeGenerator
{

private static boolean[] isCrossed;
private static int[] result;
public static int[] generate(int maxValue) {
if (maxValue < 2)
{
return new int[0];
} else {
initializeSieve(maxValue);
sieve();
loadPrimes();
return result;
}
}
private static void sieve() {


int limit = determineIterationLimit();
for (int i = 2; i <= limit; i++)
{
//如果已标识为过滤掉的数字则不再进行标记.
//因为对于n*(2..n),如果有n=x*y,则n*(2..n)=x*y*(2..n),已被完整标记过,n序列是x序列的一个子集.
if (notCrossed(i))
{
crossOutMultiplesOf(i);
}
}
}
//过滤掉的所有数字,标记为true.
private static void crossOutMultiplesOf(int i) {
for (int multiple = 2 * i; multiple < isCrossed.length; multiple += i)
{
isCrossed[multiple] = true;
}
}
//判断当前位置(i)数字是否被过滤掉.
//已过滤掉返回true
//未过滤掉返回false
private static boolean notCrossed(int i) {
return isCrossed[i] == false;
}
//上限值.上限值是小于或等于一个数的开方根的最大素数,e.x : 100 上限值为 7.
private static int determineIterationLimit() {
double iterationLimit = Math.sqrt(isCrossed.length);

return (int)iterationLimit;
}
//根据boolean数组中的标记,填充结果质数数组.
private static void loadPrimes() {
result = new int[numberOfUncrossedIntegers()];
for (int j = 0, i = 2; i < isCrossed.length; i++)
{
if (notCrossed(i))
{
result[j++] = i;
}
}
}
//返回所有质数的个数
private static int numberOfUncrossedIntegers() {
int count = 0;
for (int i = 2; i < isCrossed.length; i++)
{
if (notCrossed(i))
{
count++;
}
}
return count;
}
private static void initializeSieve(int maxValue) {
//初始化boolean数组,默认值为false,表数字没有被过滤掉.
//0,1不会被访问到.
isCrossed = new boolean[maxValue + 1];
for (int i = 2; i < isCrossed.length; i++)
{
isCrossed[i] = false;
}

}

}
/* * 原始需求背景: * 网宿CDN要按月收取客户的服务费用,根据流量的大小、 * 服务的类型等,收取不同的费用,收费规则如下: * web应用:1000元/M * 流媒体应用:1000元/M*0.7 * 下载应用:1000元/M*0.5 * 月末打印报表时,要罗列每个用户每个频道的费用、客户总费用, * 还要打印该客户的重要性指数,重要性指数=网页流/100+下载流量/600; * * 需求变更场景: * 系统已经开发出来了,接下来,运维部门现在希望对系统做一点修改, * 首先,他们希望能够输出xml,这样可以被其它系统读取和处理,但是, * 这段代码根本不可能在输出xml的代码中复用report()的任何行为,唯一 * 可以做的就是重写一个xmlReport(),大量重复report()中的行为,当然, * 现在这个修改还不费劲,拷贝一份report()直接修改就是了。 * 不久,成本中心又要求修改计费规则,于是我们必须同时修改xmlReport() * 和report(),并确保其一致性,当后续还要修改的时候,复制-黏贴的问题就 * 浮现出来了,这造成了潜在的威胁。 * 再后来,客服部门希望修改服务类型和用户重要性指数的计算规则, * 但还没决定怎么改,他们设想了几种方案,这些方案会影响用户的计费规则, * 程序必须再次同时修改xmlReport()和report(),随着各种规则变得越来越复杂, * 适当的修改点越 来越难找,不犯错误的机会越来越少。 * 现在,我们运用所学的OO原则和方法开始进行改写吧。 */
当涉及到重构时,一个常见的例子是将重复的代码提取为一个可复用的方法或函数。下面是一个简单的Java重构示例,假设我们有以下代码: ```java public class Calculator { public int add(int a, int b) { int result = a + b; System.out.println("The sum of " + a + " and " + b + " is " + result); return result; } public int subtract(int a, int b) { int result = a - b; System.out.println("The difference between " + a + " and " + b + " is " + result); return result; } public int multiply(int a, int b) { int result = a * b; System.out.println("The product of " + a + " and " + b + " is " + result); return result; } public int divide(int a, int b) { int result = a / b; System.out.println("The quotient of " + a + " divided by " + b + " is " + result); return result; } } ``` 在这个示例中,我们可以看到四个方法(add, subtract, multiply, divide)中存在重复的打印语句。为了更好地组织代码并避免重复,我们可以进行重构。下面是重构后的示例: ```java public class Calculator { public int add(int a, int b) { int result = a + b; printOperation("sum", a, b, result); return result; } public int subtract(int a, int b) { int result = a - b; printOperation("difference", a, b, result); return result; } public int multiply(int a, int b) { int result = a * b; printOperation("product", a, b, result); return result; } public int divide(int a, int b) { int result = a / b; printOperation("quotient", a, b, result); return result; } private void printOperation(String operation, int a, int b, int result) { System.out.println("The " + operation + " of " + a + " and " + b + " is " + result); } } ``` 在重构后的代码中,我们将重复的打印语句提取到了一个私有方法`printOperation`中,通过传递不同的操作名称和计算结果,可以在不同的方法中调用该方法来打印对应的结果。这样可以提高代码的可读性和可维护性,并避免了代码的重复编写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值