package ee.ut.math.tvt2008.salessystem.penalty;

import org.apache.log4j.Logger;

/**
 * Implementation of abstract factory design pattern. Abstracts away creation of strategies.
 * 
 * @author Hannes Lehemets
 */
public abstract class StrategyFactory {

	private static final Logger log = Logger.getLogger(StrategyFactory.class);

	/**
	 * Method that creates and configures strategies.
	 * 
	 * @return configured strategy
	 */
	public abstract Strategy build();

	/**
	 * Method that returns a strategy factory implementation.
	 * 
	 * 
	 * Factory class should be specified in a system property
	 * <code>-Dpenalty.strategy.factory=full.name.of.factory</code>. Specified
	 * class should extend StrategyFactory.
	 * 
	 * @return instance of strategy factory
	 */
	public static StrategyFactory getInstance() {
		try {
			Class clazz = Class.forName(System.getProperty("penalty.strategy.factory"));

			return (StrategyFactory) clazz.newInstance();
		} catch (Exception e) {
			log.error("Failed to create penalty strategy factory", e);
			throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
		}
	}

}
