Simple implementation of factory pattern in JAVA