I have written three program one using Generics and other one not used generics
Using isAssignableFrom
public class ObjectSpecificCondition {
private Class classType;
public boolean check(Object obj) {
boolean check = ((DateObject) obj).getLocalizationdate();
}
public ObjectSpecificCondition(Class classType) {
this.classType = classType;
}
boolean checkTypeSpecific(Object busineesObject) {
if (classType.isAssignableFrom(busineesObject.getClass())) {
return true;
}
else{
throw new Exception();
}
}
Using instanceOf
class ObjectSpecificCondition1 {
public boolean check(Object busineesObject) {
boolean check ;
if(busineesObject instanceof DateObject){
check= ((DateObject) busineesObject).getLocalizationdate();
}
else{
throw new IllegalArgumentException();
}
return check;
}
}
Using Generics
class ObjectSpecificConditionGenerics<T extends DateObject> {
private T classTypeGenerics;
public boolean check(T genericsobj) {
genericsobj.getLocalizationdate();
}
}
Business Object Rule
class DateObject {
boolean getLocalizationdate() {
// return true Or False according to business logic
}
}
Main test
public static void main(String[] args) throws Exception {
DateObject mockdateObject = new DateObject();
// caseI: No generics used caseI works fine no exception is generated but more lines of code to write
ObjectSpecificCondition mockanalysis = new ObjectSpecificCondition(DateObject.class);
if (mockanalysis.checkTypeSpecific(mockdateObject)) {
mockanalysis.check(mockdateObject);
}
// caseII:No generics used caseII throws exception in run time .More lines of code to write.It can not capture incompatible type at compile time
ObjectSpecificCondition mockanalysis1 = new ObjectSpecificCondition(String .class);
DateObject mockdateObject1 = new DateObject();
if (mockanalysis.checkTypeSpecific(mockdateObject1)) {
mockanalysis.check(mockdateObject1);
}
// caseIII;Generics used and line of code is reduced to less
ObjectSpecificConditionGenerics mockgenerics=new ObjectSpecificConditionGenerics() ;
mockgenerics.check(mockdateObject);
// caseIV;Generics used and line of code is reduced to less and error for compataible object is generated at compile time
ObjectSpecificConditionGenerics mockgenerics1=new ObjectSpecificConditionGenerics() ;
String mockstring=new String();
mockgenerics.check(mockstring); // it is captured at compile time ,i think its good to catch at compile time then to pass it at run time
}
}
I am seen in frameworks using three approaches .I want to get more confirm which one can be the best one?Using generics less line of code and at compile time error can be produced.But,another approach is also more used.I want to get more deeper answer .Any help please
The generic and non-generic versions should be the same, the only differences being the type parameters and casts. The non-generic version should be the type erasure of the generic version. Any generic code can be converted into equivalent non-generic code by applying the type erasure conversion.
Not Using Generics
public class ObjectSpecificCondition {
private Class classType;
public boolean check(DateObject obj) {
boolean check = obj.getLocalizationdate();
}
public ObjectSpecificCondition(Class classType) {
this.classType = classType;
}
boolean checkTypeSpecific(Object busineesObject) {
if (classType.isInstance(busineesObject)) {
return true;
}
else{
throw new Exception();
}
}
}
Using Generics
public class ObjectSpecificCondition<T extends DateObject> {
private Class<T> classType;
public boolean check(T obj) {
boolean check = obj.getLocalizationdate();
}
public ObjectSpecificCondition(Class<T> classType) {
this.classType = classType;
}
boolean checkTypeSpecific(Object busineesObject) {
if (classType.isInstance(busineesObject)) {
return true;
}
else{
throw new Exception();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.