Java 如何建模用户创建对象模型,然后用于创建多个实例
请原谅我缺乏知识,我第一次用Spring做实验,并写了一个简单的训练跟踪器。用例是Java 如何建模用户创建对象模型,然后用于创建多个实例,java,spring,Java,Spring,请原谅我缺乏知识,我第一次用Spring做实验,并写了一个简单的训练跟踪器。用例是 用户创建一个练习 设置名称、运动类型(有氧运动或举重),如果举重,则设置肌肉群 当用户进行练习时,他会添加练习的实例。(在10/27上压下工作台,提升重量) 我被困在如何使用Spring建模的问题上。我现在有一个练习POJO,看起来像 public class Exercise { private int id; private String name; private List<MuscleGr
- 用户创建一个练习
- 设置名称、运动类型(有氧运动或举重),如果举重,则设置肌肉群
- 当用户进行练习时,他会添加练习的实例。(在10/27上压下工作台,提升重量)
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;
}
}
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;
}
}
公开课练习{
私有int-id;
私有字符串名称;
私有列表肌肉群;
私人弦乐;
私人约会;
/**
*@返回id
*/
公共int getId(){
返回id;
}
/**
*@param id要设置的id
*/
公共无效集合id(内部id){
this.id=id;
}
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name要设置的名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@回条
*/
公共字符串getNote(){
退货单;
}
/**
*@param note要设置的注释
*/
公共无效设置注释(字符串注释){
this.note=注释;
}
/**
*@返回穿孔板
*/
公共日期getPerformDate(){
回程穿孔;
}
/**
*@param穿孔要设置的穿孔
*/
公共作废设置执行日期(执行日期){
this.performDate=performDate;
}
/**
*@返回肌肉群
*/
公共列表getMuscleGroups(){
返回肌肉群;
}
/**
*@param musclegroup要设置的musclegroup
*/
public void setMuscleGroups(列出muscleGroups){
this.muscleGroups=muscleGroups;
}
}
举重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;
}
}
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;
}
}
公共类权重练习扩展了练习{
私有列表集;
/**
*@还台
*/
公共列表getset(){
返回集;
}
/**
*@param设置要设置的集合
*/
公共无效集合(列表集合){
this.sets=集合;
}
}
如果用户可以提供任何名称和肌肉组,这将是微不足道的,但我希望允许用户首先创建运动类型,例如工作于胸部肌肉组且属于WeightExercise类型的卧推。然后在第1天,他们将从他们创建的练习列表中添加卧推,添加带有该日期的练习集/重复,并保存(可能保存到数据库)。两天后,他们可能会做同样的事情,使用不同的集合/重复和日期,但使用相同的练习
一个模型如何使用Spring?当然我不会创建BenchPress类、Shub类等,因为它们都有相同的字段/方法,只是值不同。这里有一个解决方案,可能适合您想要的模型
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;
}
}
公共接口练习{
字符串getName();
列出GetMuscleGroup();
...
}
公务舱长凳{
公共字符串getName(){
返回“台式压力机”;
}
列出GetMuscleGroup(){
列出结果;
结果=新列表();
结果:添加(“胸部”);
返回结果;
}
}
有很多遗漏,可以做一些改进,但我相信这是一个很好的方法,可以避免在子类中只使用值来区分成员。抽象基类是另一种很好的方法,特别是如果有公共字段(比如,可能是ID)。谢谢。我已经考虑过了,但这给我留下了两个问题。首先,我希望用户能够创建一个名为BenchPress的练习(不是新意义上的java创建,而是通过传入“BenchPress”的名称来创建)。第二个是,我有一系列练习类(“BenchPress”、“蹲”、“DeadLift”,等等),它们都有相同的实现。