2014-10-28 39 views
0

原諒缺乏知識,我第一次嘗試使用Spring並編寫簡單的鍛鍊跟蹤器。用例是如何建模用戶創建對象模型,然後用於創建多個實例

  • 用戶創建了一個鍛鍊
    • 集名稱,運動類型(有氧或舉重),如果舉重,設置肌肉羣的工作
  • 作爲用戶工作了,他增加了練習的例子。 (長凳按10/27與舉重)

我被困在如何使用春模型。我現在有一個練習POJO看起來像

public class Exercise { 

private int id; 
private String name; 
private List<MuscleGroup> muscleGroups; 
private String note; 
private Date performDate; 

/** 
* @return the id 
*/ 
public int getId() { 
    return id; 
} 

/** 
* @param id the id to set 
*/ 
public void setId(int id) { 
    this.id = id; 
} 

/** 
* @return the name 
*/ 
public String getName() { 
    return name; 
} 

/** 
* @param name the name to set 
*/ 
public void setName(String name) { 
    this.name = name; 
} 

/** 
* @return the note 
*/ 
public String getNote() { 
    return note; 
} 

/** 
* @param note the note to set 
*/ 
public void setNote(String note) { 
    this.note = note; 
} 

/** 
* @return the performDate 
*/ 
public Date getPerformDate() { 
    return performDate; 
} 

/** 
* @param performDate the performDate to set 
*/ 
public void setPerformDate(Date performDate) { 
    this.performDate = performDate; 
} 

/** 
* @return the muscleGroups 
*/ 
public List<MuscleGroup> getMuscleGroups() { 
    return muscleGroups; 
} 

/** 
* @param muscleGroups the muscleGroups to set 
*/ 
public void setMuscleGroups(List<MuscleGroup> muscleGroups) { 
    this.muscleGroups = muscleGroups; 
} 
} 

舉重POJO擴展練習,看起來像

public class WeightExercise extends Exercise{ 

private List<WeightSet> sets; 

/** 
* @return the sets 
*/ 
public List<WeightSet> getSets() { 
    return sets; 
} 

/** 
* @param sets the sets to set 
*/ 
public void setSets(List<WeightSet> sets) { 
    this.sets = sets; 
} 
} 

如果用戶可以提供任何名稱和肌肉羣,這將是微不足道的,但我'想讓用戶首先創建運動類型,例如作爲胸肌組的Bench Press,並且是WeightExercise類型的。然後在第1天,他們將從他們創建的練習列表中添加臥推,添加具有該日期的組/代表並保存(可能是數據庫)。兩天後,他們可能會做同樣的事情,不同的組合/代表和日期,但有相同的練習。

這是怎麼用Spring來建模的?當然,我不會創建BenchPress類,Squat類等,因爲它們都具有相同的字段/方法,只是具有不同的值。

回答

0

下面是一個解決方案,可以爲你想要的模型運行良好。

public interface Exercise { 
    String getName(); 
    List<String> getMuscleGroups(); 
    ... 
} 

public class BenchPress { 
    public String getName() { 
     return "bench press"; 
    } 

    List<String> getMuscleGroups() { 
     List<String> result; 

     result = new List<String>(); 
     result.add("chest"); 

     return result; 
    } 
} 

有很多離開了,有些可以作出改進,但我相信這顯示了一個好辦法,以避免在子類只由值來區分其成員。抽象基類是另一種好方法,特別是如果有通用字段(比如可能是ID)。

+0

謝謝。我考慮過這個問題,但這給我留下了兩個問題。首先,我希望用戶能夠創建一個名爲臥推的練習(不是在新的意義上的Java創建,而是通過「Bench Press」這個名稱)。第二個是我會有一堆鍛鍊課程(「BenchPress」,「Squat」,「DeadLift」等)都將具有相同的實施。 – user3442536 2014-10-28 11:43:37