User Tools

Site Tools


zalozeni:logika:calculatorfactory

CalculatorFactory

mereni dobehlo do konce, mame namerena data. Posledni, co je treba, je nad ExperimentInstance vyhodnotit mereni. DokuwikiInstanceCalculatorFacotry je trida, ktera tak ucini.

Jedna se o factory, nebot kazdy zakaznik si muze vymyslet, ze chce vyhodnoceni upravit oproti vyhodnoceni defautlniho definovaneho normou. Proto je v DokuwikiInstanceCalculatorFacotry zaveden enum CalculatorEnum a staticka metoda calculate(), ktera vytvori konkretni instanci abstraktni tridy DokuwikiInstanceCalculator v zavislosti na tomto enumu.

public class DokuwikiInstanceCalculatorFacotry {
 
	public enum CalculatorEnum {
		BY_NORM_CALCULATOR
	}
 
	public static void calculate(ExperimentInstance expInstance) {
		calculate(expInstance, CalculatorEnum.BY_NORM_CALCULATOR);
	}
 
	public static void calculate(ExperimentInstance expInstance, CalculatorEnum calculatorEnum) {
		DokuwikiInstanceCalculator calculator;
		switch (calculatorEnum) {
		case BY_NORM_CALCULATOR:
			calculator = new DokuwikiInstanceByNormCalculator(expInstance);
			break;
		default:
			calculator = new DokuwikiInstanceByNormCalculator(expInstance);
			break;
		}
		calculator.calculate();
		calculator.save();
	}
 
}

Samotny DokuwikiInstanceCalculator je pak v jeho nejjednodussi forme definovan nasledovne

public abstract class DokuwikiInstanceCalculator {
 
	protected ExperimentInstance expInstance;
	protected DokuwikiDataService dataService;
	protected List<DokuwikiDataRow> dataRows;
 
	private double res1;
 
	public DokuwikiInstanceCalculator(ExperimentInstance expInstance) {
		this.expInstance = expInstance;
		dataService = (DokuwikiDataService) expInstance.getDataService();
		dataRows = dataService.getDataRows();
	}
 
	public void calculate() {
		res1 = calculateRes1();
	}
 
	public void save() {
		dataService.saveResult1(res1);
	}
 
	protected abstract double calculateRes1();
}
public class DokuwikiInstanceByNormCalculator extends DokuwikiInstanceCalculator {
 
	public DokuwikiInstanceByNormCalculator(ExperimentInstance expInstance) {
		super(expInstance);
	}
 
	@Override
	protected double calculateRes1() {
		double result = 0;
		for (DokuwikiDataRow dr : dataRows) {
			if (dr.getValue1() > result) result = dr.getValue1();
		}
		return result;
	}
 
}
zalozeni/logika/calculatorfactory.txt · Last modified: 2016/07/29 13:46 by jlochman