Skip to content

Instantly share code, notes, and snippets.

@bastman
Created April 15, 2015 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastman/e3334af833262c328be6 to your computer and use it in GitHub Desktop.
Save bastman/e3334af833262c328be6 to your computer and use it in GitHub Desktop.
ClientFactory - Generics Example. Create Instances using Reflection
public class ClientFactory {
public static <T extends Client> T createClient(Class<T> clientClass, String host, Integer port)
{
try {
Constructor<T> constructor = clientClass.getConstructor(
String.class, Integer.class
);
T client = constructor.newInstance(
host, port
);
return client;
} catch (Exception e) {
throw new RuntimeException("Unable to create client: " + e.getClass().getName()+" "+e.getMessage(), e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment