我有兩個接口可以用來通過Key和Value查找一些統計信息,另一個用於訪問一個對象並迭代它,第一個接口有以下方法:重構代碼是泛型和功能接口
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();
}
}
而且在我的包我有員工豆具有像名字,姓氏,薪水和位置與他們的getter和setter方法幾個屬性。
我希望做一些驗證一樣讓我擁有X的工資和出生於1990年,並沒有爲這些驗證下面的類員工人數:
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類IM看到打電話給我所有的方法做了驗證,一切工作正常,我得到了預期的結果,但我的代碼效率不高,因爲我想使它成爲ge neric並讓它接受任何類型的對象,我做了很少的嘗試,但我卡住了。
我試圖寫一個功能接口稱爲條件有一個方法,我可以通過一個條件,並檢查它像下面這樣:
public interface Conditions {
boolean checkCondition(Object obj);
}
所以有人可以建議什麼是改變的最佳途徑我代碼是通用的,並接受任何類型的對象,例如學生,並儘可能地乾淨,也許通過應用設計模式?
我不能刪除接口統計和收集器作爲任務是要實現它們 –
主要想法是,你濫用接口。用類重構它,並留下必要的 –
好吧,我明白你的意思謝謝你,現在我怎樣才能使它一般,並使它爲示例容易實現一個學生對象相同? –