如何建模用户创建对象模型然后用于创建许多实例

时间:2014-10-28 03:09:59

标签: java spring

原谅缺乏知识,我第一次尝试使用Spring并编写一个简单的锻炼跟踪器。用例是

  • 用户创建练习
    • 设定名称,运动类型(有氧运动或举重),如果举重,设定肌肉群工作
  • 当用户计算出来时,他会添加练习的实例。 (在10/27的压力下,压力被抬起)

我坚持如何使用Spring对此进行建模。我目前有一个看起来像

的练习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天,他们将从他们创建的练习列表中添加Bench Press,添加具有该日期的集合/代表并保存(可能是数据库)。两天后,他们可能会做同样的事情,不同的设置/代表和日期,但相同的练习。

如何使用Spring模拟这个?当然,我不会创建一个BenchPress类,Squat类等,因为它们都具有相同的字段/方法,只是不同的值。

1 个答案:

答案 0 :(得分: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)。