Skip to content

Instantly share code, notes, and snippets.

@jonasurbano jonasurbano/Developer Secret
Created Apr 3, 2015

Embed
What would you like to do?
Example of Pattern Factory with Java EE 7. A recruiter should choose among developers depending on their English level
import javax.enterprise.context.Dependent;
@EnglishLevel(EnglishLevel.Type.Intermediate)
@Dependent
public class Developer implements Person {
@Override
public String readResume() {
return "I could be your man";
}
}
import javax.enterprise.context.Dependent;
@EnglishLevel(EnglishLevel.Type.Advanced)
@Dependent
public class DeveloperWithC1 extends Developer {
@Override
public String readResume() {
return "I'm your man";
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface EnglishLevel {
Type value();
enum Type { Intermediate, Advanced, Proficiency};
}
import javax.enterprise.util.AnnotationLiteral;
public class EnglishLevelLiteral extends AnnotationLiteral<EnglishLevel> implements EnglishLevel {
private static final long serialVersionUID = 1L;
private Type type;
public EnglishLevelLiteral(Type type) {
this.type = type;
}
@Override
public Type value() {
return type;
}
}
public interface Person {
public String readResume();
}
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
public class PersonFactory {
@Inject
@Any
private Instance<Person> developers;
/**
* Search among hundreds of resumes
*/
public Person getDeveloper(EnglishLevel.Type type) {
EnglishLevelLiteral literal = new EnglishLevelLiteral(type);
return developers.select(literal).get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.