-
-
Save jonasurbano/f4f4b1b53e4dcec92be9 to your computer and use it in GitHub Desktop.
Example of Pattern Factory with Java EE 7. A recruiter should choose among developers depending on their English level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.enterprise.context.Dependent; | |
@EnglishLevel(EnglishLevel.Type.Advanced) | |
@Dependent | |
public class DeveloperWithC1 extends Developer { | |
@Override | |
public String readResume() { | |
return "I'm your man"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Person { | |
public String readResume(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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