2014-10-28 62 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

相关问题