gpt4 book ai didi

java - 将代码重构为泛型和函数式接口(interface)

转载 作者:行者123 更新时间:2023-12-02 13:29:10 27 4
gpt4 key购买 nike

我有两个接口(interface),一个可用于通过 Key 和 Value 查找一些统计信息,另一个用于访问对象并迭代它,第一个接口(interface)具有以下方法:

public interface Statistic {

public String getKey();

public Object getValue();

public String getDetails();
}

这是它的实现:

public class Collector implements Statistic {

private String key;
private int val;

public Collector(String key, int val) {
this.key = key;
this.val = val;
}

public void setValue(int val) {
this.val = val;
}

@Override
public String getKey() {
return key;
}

@Override
public Integer getValue() {
return val;
}

@Override
public String getDetails() {
return null;
}
}

另一个具有以下内容:

public interface StatisticsCollector<T extends Object, S extends Statistic> {

public String getName();

public void visit(T object);

public Iterator<S> calculatedStatistics();
}

这是它的实现:

public class CalculateFromObject<K, V> implements StatisticsCollector<Object, Collector> {

EmployeeValidator empValidator = new EmployeeValidator();
StringValidator strValidator = new StringValidator();

@Override
public String getName() {
return null;
}

@Override
public void visit(Object object) {
if (object instanceof String) {
String str = object.toString();

int upperCaseCount = strValidator.upperCaseFreq(str);
strValidator.set.add(new Collector("Upper Case Letters: ", upperCaseCount));
int lowerCaseCount = strValidator.lowerCaseFreq(str);
strValidator.set.add(new Collector("Lower Case Letters: ", lowerCaseCount));
int digitsCount = strValidator.digitFreq(str);
strValidator.set.add(new Collector("Digits Count: ", digitsCount));
int wordCount = strValidator.wordFreq(str);
strValidator.set.add(new Collector("Words Count: ", wordCount));
int nonWordCount = strValidator.nonWordFreq(str);
strValidator.set.add(new Collector("Non Word Count: ", nonWordCount));

} else if (object instanceof Employee) {

Employee emp = (Employee) object;
empValidator.salaryValidator(emp);
empValidator.birthDateValidator(emp);
empValidator.birthPlaceValidator(emp);
empValidator.resignationDateValidator(emp);
empValidator.positionValidator(emp);
}
}

@Override
public Iterator<Collector> calculatedStatistics() {
return empValidator.set.iterator();
}


}

在我的包中,我有一个 Employee Bean,它有一些属性,如名字、姓氏、薪水和位置及其 setter 和 getter 。

我想做一些验证,比如获取 1990 年出生、薪水为 x 的员工数量,并为这些验证执行以下类(class):

public class EmployeeValidator {

public Set<Collector> set = new HashSet<>();

public void salaryValidator(Employee emp) {
int count = 0;
// each collector consist of a condition (function), key, value (always incremented)
if (emp.getSalary() < 350) {
set.add(new Collector("Employee with salaries less than 350JD: ", ++count));
} else if (emp.getSalary() >= 350 && emp.getSalary() < 600) {
set.add(new Collector("Employee with salaries between 350JD And 600JD: ", ++count));
} else if (emp.getSalary() >= 600 && emp.getSalary() < 1200) {
set.add(new Collector("Employee with salaries between 600JD And 1200JD ", ++count));
} else if (emp.getSalary() >= 1200) {
set.add(new Collector("Employee with salaries more than 1200JD: ", ++count));
}

}

public void birthDateValidator(Employee emp) {
for (Collector stats : set) {
if (("Employees that where born in " + emp.getBirthDate().getYear() + " = ").equals(stats.getKey())) {
count(stats);
return;
}
}
set.add(new Collector("Employees that where born in " + emp.getBirthDate().getYear() + " = ", 1));
}

public void birthPlaceValidator(Employee emp) {
for (Collector stats : set) {
if (("Employees that where born in " + emp.getBirthPlace() + " = ").equals(stats.getKey())) {
count(stats);
return;
}
}
set.add(new Collector("Employees that where born in " + emp.getBirthPlace() + " = ", 1));
}

public void resignationDateValidator(Employee emp) {
for (Collector stats : set) {
if (("Employees that where Resignation in " + emp.getResignationDate().getYear() + " = ").equals(
stats.getKey())) {
count(stats);
return;
}
}
set.add(new Collector("Employees that where Resignation in " + emp.getResignationDate().getYear() + " = ", 1));
}

public void positionValidator(Employee emp) {
for (Collector stats : set) {
if (("Employees that occupy the " + emp.getPosition() + " position = ").equals(stats.getKey())) {
count(stats);
return;
}
}
set.add(new Collector("Employees that occupy the " + emp.getPosition() + " position = ", 1));
}

private void count(Collector stats) {
int counter = stats.getValue() + 1;
stats.setValue(counter);
}
}

我还有另一个类来验证字符串并查看字符串有多少个大写字母,有多少个小写字母......等等

正如您在CalculateFromObject 类中的访问方法中看到的那样,我正在调用我的所有方法来进行验证,一切正常,我得到了预期的结果,但我的代码效率并不高,正如我想要的那样它通用并使其接受任何类型的对象,我做了几次尝试,但我卡住了。

我尝试编写一个名为 Conditions 的功能接口(interface),它有一个可以传递条件并检查它的方法,如下所示:

public interface Conditions {

boolean checkCondition(Object obj);

}

那么有人可以建议什么是最好的方法来将我的代码更改为通用并接受任何类型的对象(例如学生),并通过应用设计模式尽可能干净?

最佳答案

您的类中有很多开销,并且对 POJO's 的接口(interface)存在误解。 (简单的类(class))。在较高的层面上,您应该执行以下操作:

1).删除接口(interface)Statistic和类(class)Collectors 。它们只是封装数据。相反 - 创建一个 POJO Employee具有必要的字段+ getter + setter。不要使用“ key -” value`,为这些字段指定有意义的名称:

public class Employee
{
private String name;
private int id;
private double salary;
...
public String getName() {...}
public void setName(..) {...}
// other getters / setters
}

如果需要,创建构造函数

2) 看起来像你的Employee class 也是多余的,删除它。使用新员工代替3)使用Collections用于存储员工实例集合的框架。

`List<Employee> employees = new ArrayList<>();
employees.add(new Employee(.... )); `

4).创建带有验证方法的 EmployeeValidator 接口(interface)并实现它:

public interface EmployeeValidator {
void validate(List<Employee> employees);
}

5)如果要操作一些统计数据,单独创建Statistics将对员工集合进行操作的类,例如

public class Statistics {

public double getAvgSalary(List<Employee> employees)
{
double avgSalary = 0;
for (Employee e : employees) {
....
}
}
}

关于java - 将代码重构为泛型和函数式接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com