lundi 14 septembre 2015

Or a good practice add parameter to constructor on the singleton design pattern?

I have Table2001 class and Events factory class. And my goal is EventsFactory class use as singleton by EventsFactory.getInstance(). I want to ask witch way is best to set field in Singleton? Or I should use other patern? Becouse now i must first initialize EventsFactory before use static getInstance() method. I want set as config before start use this factory, now I must implement new EventsFactory(Table2001.getInstance()). For me looks something wrong here.

public final class Table2001 implements DecathlonTable{

public static final TableRow R100M = new TableRow(25.4347f,18f,1.81f);
public static final TableRow LONG_JUMP = new TableRow(0.14354f, 220f, 1.4f);
public static final TableRow SHOT_PUT = new TableRow(51.39f, 1.5f, 1.05f);
public static final TableRow HIGH_JUMP = new TableRow(0.8465f, 75f, 1.42f);
public static final TableRow R400M = new TableRow(1.53775f, 82f, 1.81f);
public static final TableRow HURDLES_110M = new TableRow(5.74352f, 28.5f, 1.92f);
public static final TableRow DISCUS_THROW = new TableRow(12.91f, 4f, 1.1f);
public static final TableRow POLE_VAULT = new TableRow(0.2797f, 100f, 1.35f);
public static final TableRow JAVELIN_THROW = new TableRow(10.14f, 7f, 1.08f);
public static final TableRow R1500M = new TableRow(0.03768f,480f,1.85f);

private static Table2001 instance = null;

protected Table2001() {
}

public static Table2001 getInstance() {
    if(instance == null) {
        instance = new Table2001();
    }
    return instance;
}

@Override
public  TableRow getR100M() {
    return R100M;
}

@Override
public TableRow getLONG_JUMP() {
    return LONG_JUMP;
}

... // setters and getters

And events factory class:

public class EventsFactory {

private DecathlonTable table;
private static EventsFactory instance = null;

public EventsFactory(DecathlonTable table){
    this.table = table;
    instance = this;
}

public static EventsFactory getInstance() throws Exception {
    if(instance == null) {
        throw new Exception("Events factory not exist");
    }
    return instance;
}

/**
 * Create ir return Event by type with counted result. Result is counting by DecathlonTable.
 * Events types:
 * 100M          - 100 metres run,
 * LONG_JUMP     - long jump,
 * SHOT_PUT      - shot put,
 * HIGH_JUMP     - high jump,
 * 400M          - 400 metres run,
 * HURDLES_110M  - hurdles 110 metres run,
 * DISCUS_THROW  - discus throw,
 * POLE_VAULT    - pole vault,
 * JAVELIN_THROW - javelin throw,
 * 1500M         - 1500 metres run.
 * @param eventType
 * @param result
 * @return
 */
public Event getEvent(String eventType, Metric result){
    switch (eventType){
        case "100M":
            return new TrackEvent((Time)result,table.getR100M());
        case "LONG_JUMP":
            return new JumpEvent((Distance)result,table.getLONG_JUMP());
        case "SHOT_PUT":
            return new ThrowEvent((Distance)result,table.getSHOT_PUT());
        case "HIGH_JUMP":
            return new JumpEvent((Distance)result,table.getHIGH_JUMP());
        case "400M":
            return new TrackEvent((Time)result,table.getR400M());
        case "HURDLES_110M":
            return new TrackEvent((Time)result,table.getHURDLES_110M());
        case "DISCUS_THROW":
            return new ThrowEvent((Distance)result,table.getDISCUS_THROW());
        case "POLE_VAULT":
            return new JumpEvent((Distance)result,table.getPOLE_VAULT());
        case "JAVELIN_THROW":
            return new ThrowEvent((Distance)result,table.getJAVELIN_THROW());
        case "1500M":
            return  new TrackEvent((Time)result,table.getR1500M());
    }
   return null;
}

}

Aucun commentaire:

Enregistrer un commentaire