# 耦合和组合，第 1 部分

## 此内容是该系列的一部分：函数式思维

— Michael Feathers，Working with Legacy Code 的作者

## 通过结构实现代码重用

### 疏忽的代码重复

##### 清单 1. 命令式的数字分类器
```import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import static java.lang.Math.sqrt;

public class ClassifierAlpha {
private int number;

public ClassifierAlpha(int number) {
this.number = number;
}

public boolean isFactor(int potential_factor) {
return number % potential_factor == 0;
}

public Set<Integer> factors() {
HashSet<Integer> factors = new HashSet<Integer>();
for (int i = 1; i <= sqrt(number); i++)
if (isFactor(i)) {

}
return factors;
}

static public int sum(Set<Integer> factors) {
Iterator it = factors.iterator();
int sum = 0;
while (it.hasNext())
sum += (Integer) it.next();
return sum;
}

public boolean isPerfect() {
return sum(factors()) - number == number;
}

public boolean isAbundant() {
return sum(factors()) - number > number;
}

public boolean isDeficient() {
return sum(factors()) - number < number;
}

}```

##### 清单 2. 命令式的质数检验
```import java.util.HashSet;
import java.util.Set;

import static java.lang.Math.sqrt;

public class PrimeAlpha {
private int number;

public PrimeAlpha(int number) {
this.number = number;
}

public boolean isPrime() {
Set<Integer> primeSet = new HashSet<Integer>() {{
return number > 1 &&
factors().equals(primeSet);
}

public boolean isFactor(int potential_factor) {
return number % potential_factor == 0;
}

public Set<Integer> factors() {
HashSet<Integer> factors = new HashSet<Integer>();
for (int i = 1; i <= sqrt(number); i++)
if (isFactor(i)) {
}
return factors;
}
}```

### 采用重构去除重复

##### 清单 3. 常用的重构分解代码
```import java.util.Set;
import static java.lang.Math.sqrt;
import java.util.HashSet;

public class FactorsBeta {
protected int number;

public FactorsBeta(int number) {
this.number = number;
}

public boolean isFactor(int potential_factor) {
return number % potential_factor == 0;
}

public Set<Integer> getFactors() {
HashSet<Integer> factors = new HashSet<Integer>();
for (int i = 1; i <= sqrt(number); i++)
if (isFactor(i)) {
}
return factors;
}
}```

##### 清单 4. 重构和简化的数字分类器
```import java.util.Iterator;
import java.util.Set;

public class ClassifierBeta extends FactorsBeta {

public ClassifierBeta(int number) {
super(number);
}

public int sum() {
Iterator it = getFactors().iterator();
int sum = 0;
while (it.hasNext())
sum += (Integer) it.next();
return sum;
}

public boolean isPerfect() {
return sum() - number == number;
}

public boolean isAbundant() {
return sum() - number > number;
}

public boolean isDeficient() {
return sum() - number < number;
}

}```

##### 清单 5. 重构和简化的质数检验器
```import java.util.HashSet;
import java.util.Set;

public class PrimeBeta extends FactorsBeta {
public PrimeBeta(int number) {
super(number);
}

public boolean isPrime() {
Set<Integer> primeSet = new HashSet<Integer>() {{
return getFactors().equals(primeSet);
}
}```

## 通过组合实现代码重用

##### 清单 6. 更加函数化版本的数字分类器
```public class FClassifier {

static public boolean isFactor(int number, int potential_factor) {
return number % potential_factor == 0;
}

static public Set<Integer> factors(int number) {
HashSet<Integer> factors = new HashSet<Integer>();
for (int i = 1; i <= sqrt(number); i++)
if (isFactor(number, i)) {
}
return factors;
}

public static int sumOfFactors(int number) {
Iterator<Integer> it = factors(number).iterator();
int sum = 0;
while (it.hasNext())
sum += it.next();
return sum;
}

public static boolean isPerfect(int number) {
return sumOfFactors(number) - number == number;
}

public static boolean isAbundant(int number) {
return sumOfFactors(number) - number > number;
}

public static boolean isDeficient(int number) {
return sumOfFactors(number) - number < number;
}
}```

##### 清单 7. 函数版的质数检验器
```public static boolean isPrime(int number) {
Set<Integer> factors = factors(number);
return number > 1 &&
factors.size() == 2 &&
factors.contains(1) &&
factors.contains(number);
}```

##### 清单 8. 函数化重构的 `Factors` 类
```import java.util.HashSet;
import java.util.Set;
import static java.lang.Math.sqrt;

public class Factors {
static public boolean isFactor(int number, int potential_factor) {
return number % potential_factor == 0;
}

static public Set<Integer> of(int number) {
HashSet<Integer> factors = new HashSet<Integer>();
for (int i = 1; i <= sqrt(number); i++)
if (isFactor(number, i)) {
}
return factors;
}
}```

##### 清单 9. 重构的数字分类器
```public class FClassifier {

public static int sumOfFactors(int number) {
Iterator<Integer> it = Factors.of(number).iterator();
int sum = 0;
while (it.hasNext())
sum += it.next();
return sum;
}

public static boolean isPerfect(int number) {
return sumOfFactors(number) - number == number;
}

public static boolean isAbundant(int number) {
return sumOfFactors(number) - number > number;
}

public static boolean isDeficient(int number) {
return sumOfFactors(number) - number < number;
}
}```

##### 清单 10. 重构的质数检验器
```import java.util.Set;

public class FPrime {

public static boolean isPrime(int number) {
Set<Integer> factors = Factors.of(number);
return number > 1 &&
factors.size() == 2 &&
factors.contains(1) &&
factors.contains(number);
}
}```

## 结束语

#### 相关主题

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=10
Zone=Java technology
ArticleID=776976
ArticleTitle=函数式思维: 耦合和组合，第 1 部分
publish-date=11282011