我应该使用哪种设计模式来设置竞赛点数?

时间:2015-09-12 10:48:31

标签: java design-patterns software-design

我有一个班级AthleticCompetitionResults,想要创建一个带有方法的类,以返回从行中初始化的AthleticCompetitionResults的对象,例如

  

Siim Susi; 12.61; 5.00; 9.22; 1.50; 60.39; 16.43; 21.60; 2.60; 35.81; 5.25.72

     

Siim Susi2; 12.61; 5.00; 9.22; 1.50; 60.39; 16.43; 21.60; 2.60; 35.81; 5.25.72

     

Jaana Lind; 13.75; 4.84; 10.12; 1.50; 68.44; 19.18; 30.85; 2.80; 33.88; 6.22.75

public class AthleticCompetitionResults {
  private Athletic athletic;
  private float resultM100;
  private float resultLongJump;
  private float resultShotPut;
  private float resultHighJump;
  private float resultM400;
  private float resultHurdles110m;
  private float resultDiscusThrow;
  private float resultPoleVault;
  private float resultJavelinThrow;
  private float resultM1500;
  private int points;


public AthleticCompetitionResults(Athletic athletic) {
    this.athletic = athletic;
}

public void countPoints(){
   //counting
}

// setters and getters
...

已编辑:
我的目标是按比例打印List<AthleticCompetitionResults>排序列表并在比赛中打印地点。如果点数等于打印1-2个地方。例如:

  

1-2 Siim Susi 4200分   1-2 Siim Susi2 4200分   3 Jaana Lind 3494分

什么是保存运动竞赛场地的最佳方法?那么List<Integer>' places in class 'AthleticCompetitionResults呢?

public class Competition{
  private List<AthleticCompetitionResults> athleticsData;

  public Competition(List<AthleticCompetitionResults> data){
     this.athleticsData = data;
  }

  public void sortByPoints(){
   //sorting by points
  }

  public void setPlaces(){
    //setting athletics places in competition by points
  }

  public void print(){
     //printing e.g 1-2 Siim Susi 4200 pts ...
  }
}

2 个答案:

答案 0 :(得分:5)

考虑:

并非所有事件都会导致时间

  • 例如摔跤,射击)

结果取决于事件类型

  • 冲刺有完成时间和排名
  • 摔跤有赢家/宽松

某些事件组的结果属性相似

  • 100米短跑,500米冲刺,游泳,马拉松都有完成时间。

  • javelin,shotput,讨论投掷距离。

  • 所有活动都有明显的赢家

我们需要策略,以便根据事件类型,我们的结果属性可以更改。

让我们尝试为它设置一些示例代码:

class Result {
  protected boolean winner;
  ..
};

class ThrowingResult extends Result{
  protected float range;
  ..
}

class SprintResult extends Result{
  protected double timeToFinishInMilliseconds;
  protected int finalPosition;
  ..
}

class AthleticCompetitionResults{
  private Athletic athletic;

  private SprintResult resultM100;
  private SprintResult resultLongJump;

  private ThrowingResult javelin;
  private ThrowingResult shotPut;

  private Result wrestling;
  ...
}

答案 1 :(得分:3)

最好的方法是将这些数据保存在Map

密钥将作为final写在此类中。

喜欢这样:

private Map<String, String> valuesMap = new HashMap<String, String>();

public static final String ATHLETIC_NAME = "Athletic_Name";
public static final String FIELD_1 = "Field_1";
public static final String FIELD_2 = "Field_2";
public static final String FIELD_3 = "Field_3";
public static final String FIELD_4 = "Field_4";
public static final String FIELD_5 = "Field_5";

private static final String[] FIELDS_LIST = new String[]{
    ATHLETIC_NAME,
    FIELD_1,
    FIELD_2,
    FIELD_3,
    FIELD_4,
    FIELD_5
};


private void getParametersMap(String values){

    System.out.println("Loaded Parameters:");

    try{ 
        String[] splitted = values.split(";");

        if (splitted.length != FIELDS_LIST.length){
            throw new Exception("Unmatched arrays size!!");
        }
        int i=0;
        for (String keyItem : FIELDS_LIST){     
            valuesMap.put(keyItem, splitted[i++]);
        }
        System.out.println();
    }
    catch (Exception e){
        e.printStackTrace();
    }
}