# AgeDiffFinder **Repository Path**: jacob-lab/agedifffinder ## Basic Information - **Project Name**: AgeDiffFinder - **Description**: 重构练习项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-05-13 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 重构 Finder 工程 ## 思路 1. 获取最大年龄差和最小年龄差,抽象为两个独立的类 2. `AgeDiffFinder` 依赖年龄差策略接口 `IAgeDiffStrategy`,接口的子类实现具体的计算逻辑 3. 减少无关对象的暴露,减少耦合,提高内聚性 4. 减少代码的缩进 ## 实现代码 ```java package algorithm; import algorithm.strategy.IAgeDiffStrategy; import java.util.ArrayList; import java.util.List; /** * 获取策略查找年龄差值 */ public class AgeDiffFinder { private final List persons; public AgeDiffFinder(List persons) { this.persons = persons; } /** * 根据查获策略, 获取年龄差 */ public PersonAgeDiff find(IAgeDiffStrategy ageDiffStrategy) { List ageDiffList = new ArrayList<>(); int size = persons.size(); for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { PersonAgeDiff ageDiff = createPersonAgeDiff(i, j); ageDiffList.add(ageDiff); } } if (ageDiffList.size() < 1) { return new PersonAgeDiff(); } return ageDiffStrategy.get(ageDiffList); } /** * 创建 PersonAgeDiff 对象, 并且保证对象里 person2 年龄比 person1 的大 */ private PersonAgeDiff createPersonAgeDiff(int i, int j) { PersonAgeDiff ageDiff = new PersonAgeDiff(); if (persons.get(i).compareWith(persons.get(j)) < 0) { ageDiff.person1 = persons.get(i); ageDiff.person2 = persons.get(j); } else { ageDiff.person1 = persons.get(j); ageDiff.person2 = persons.get(i); } ageDiff.calcAgeDiff(); return ageDiff; } } ``` ### 接口策略 ```java package algorithm.strategy; import algorithm.PersonAgeDiff; import java.util.Comparator; import java.util.List; /** * 计算年龄差策略 */ public interface IAgeDiffStrategy { Comparator COMPARING_BY_AGE_DIFF = Comparator.comparing(PersonAgeDiff::getAgeDiff); PersonAgeDiff get(List ageDiffList); } ``` ```java package algorithm.strategy; import algorithm.PersonAgeDiff; import java.util.List; import java.util.Optional; /** * 求最小年龄差 */ public class AgeDiffMinStrategy implements IAgeDiffStrategy { @Override public PersonAgeDiff get(List ageDiffList) { Optional ageDiff = ageDiffList.stream().min(COMPARING_BY_AGE_DIFF); if (!ageDiff.isPresent()) { throw new RuntimeException("获取最小年龄差失败"); } return ageDiff.get(); } } ``` ```java package algorithm.strategy; import algorithm.PersonAgeDiff; import java.util.List; import java.util.Optional; /** * 求最大年龄差 */ public class AgeDiffMaxStrategy implements IAgeDiffStrategy { @Override public PersonAgeDiff get(List ageDiffList) { Optional ageDiff = ageDiffList.stream().max(COMPARING_BY_AGE_DIFF); if (!ageDiff.isPresent()) { throw new RuntimeException("获取最大年龄差失败"); } return ageDiff.get(); } } ``` ### 实体类 ```java package algorithm; import java.util.Date; public class Person { public String name; public Date birthDate; public long compareWith(Person other) { return this.birthDate.getTime() - other.birthDate.getTime(); } public long calcAgeDiffWith(Person other) { return this.birthDate.getTime() - other.birthDate.getTime(); } } ``` ```java package algorithm; public class PersonAgeDiff { public Person person1; public Person person2; public long ageDiff; public long getAgeDiff() { return ageDiff; } public void calcAgeDiff() { ageDiff = person2.calcAgeDiffWith(person1); } } ```