Java: How to use Generic Types to create a generics class
A generic type is a generic class or interface that is parameterized over types.
Las clases genéricas (con tipos genéricos) permiten, entre otras cosas, forzar la seguridad de los tipos creados por nosostros, en tiempo de compilación. También permiten abstraer comportamiento independientemente de tipo de objeto que se manipule.
Esto se puede apreciar con un ejemplo:
Example diagram
Example code
//........................................................
//File name: IMyGenericClass.java
package com.daro.generic.example.generic;
import java.util.List;
public interface IMyGenericClass<T> {
public void printClassName(T p);
public List<T> list(Class<T> clazz);
}
//........................................................
//File name: MyGenericClass.java
package com.daro.generic.example.generic;
import com.daro.generic.example.generic.IMyGenericClass;
import java.util.ArrayList;
import java.util.List;
public abstract class MyGenericClass<T>
implements IMyGenericClass<T> {
private final Class<T> type; public MyGenericClass(Class<T> type) { this.type = type; } public Class<T> getMyType() { return this.type; }
@Override
public void printClassName(T p) {
System.out.println ("Class name: " + p.getClass().getName());
}
@SuppressWarnings("unchecked")
@Override
public List<T> list(Class<T> clazz) {
@SuppressWarnings("rawtypes")
Object object = null;
try {
object = clazz.newInstance();
//equal to this.getMyType().newInstance()
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
List<T> list = new ArrayList<T>();
list.add((T)object);
return list;
}
}
//........................................................
//File name: MyGenericClassImpl.java
package com.daro.generic.example.generic;
import java.util.List;
import com.daro.generic.example.generic.MyGenericClass;
import com.daro.generic.example.generic.MyClass;
public class MyGenericClassImpl extends MyGenericClass<MyClass> {
public MyGenericClassImpl() {
super(MyClass.class);
}
public void doSomething() {
MyClass myClass = new MyClass();
List<MyClass> myList = this.list(MyClass.class);
this.printClassName(myClass);
System.out.println("To string my object: " + myList.get(0).toString());
}
public static void main(String[] args) {
System.out.println("Running test!"); // Display the string.
MyGenericClassImpl myGenericClassImpl = new MyGenericClassImpl();
myGenericClassImpl.doSomething();
}
}
//........................................................
//File name: MyClass.java
package com.daro.generic.example.generic;
import java.io.Serializable;
public class MyClass {
public String toString(){
return "MyClass Example";
}
}
Running Example
Result of run MyGenericClassImpl:
Running test!
Class name: com.daro.generic.example.generic.MyClass
To string my object: MyClass Example
References:
http://docs.oracle.com/javase/tutorial/java/generics/types.html
http://www.arquitecturajava.com/uso-de-java-generics/
http://jonsegador.com/2012/10/clases-y-tipos-genericos-en-java/
No hay comentarios:
Publicar un comentario