0

我有兩個接口可以用來通過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); 

} 

所以有人可以建議什麼是改變的最佳途徑我代碼是通用的,並接受任何類型的對象,例如學生,並儘可能地乾淨,也許通過應用設計模式?

回答

1

您的課堂上有很多開銷,並且對POJO's(簡單課程)的界面有誤解。在高層次上,你應該做以下事情:

1)。刪除界面Statistic和類Collectors。他們只是在封裝數據。相反 - 創建POJO Employee必要的字段+ getters + setter。不要使用「鍵- value`,給這些領域的有意義的名字:

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

創建構造函數,如果需要

2)看起來像你的Employee類也是多餘的,將其刪除。使用新員工代替 3)使用Collections框架來存儲員工實例的集合。

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

4)。與驗證方法創建界面EmployeeValidator並執行它:

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) { 
     .... 
     } 
    } 
} 
+0

我不能刪除接口統計和收集器作爲任務是要實現它們 –

+0

主要想法是,你濫用接口。用類重構它,並留下必要的 –

+0

好吧,我明白你的意思謝謝你,現在我怎樣才能使它一般,並使它爲示例容易實現一個學生對象相同? –