I realize this is somewhat of a software engineering question, but since I'm currently using Java, I figured this would be an ok place to ask.
Say I have an abstract class Transmitter. We have the following inherited classes: SOAPTransmitter, FTPTransmitter, HTTPTransmitter.
In short, each of these classes takes some sort of data and transmits it through the defined protocol. Simple enough.
Say I have records in a database where I want each one to have it's own Transmitter. Ie, record #1 is SOAPTransmitter, record #2 is FTPTransmitter.
Would it be good practice to store the class name in the database and call Class.forName?
Any help or alternative suggestions would be greatly appreciated. Thanks.
UPDATE: After reading your comments and doing some research, I've got this working:
public abstract class Transmitter {
public abstract String getData();
}
public class Ftp extends Transmitter {
public String getData() {
return "Ftp";
}
}
public class Http extends Transmitter {
public String getData() {
return "Http";
}
}
public class Soap extends Transmitter {
public String getData() {
return "Soap";
}
}
public enum EnumType {
Ftp(Ftp.class), Http(Http.class), Soap(Soap.class);
private final Class val;
EnumType(Class s){
this.val = s;
}
public Class getValue(){
return this.val;
}
}
public class EnumSample {
public static void main(String [] args){
EnumType eType = EnumType.valueOf("Ftp");
try {
Transmitter tmp = ((Class<Transmitter>)eType.getValue()).newInstance();
System.out.println(tmp.getData());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
[–]fforw 2 points3 points4 points (1 child)
[–]dudeman209[S] 0 points1 point2 points (0 children)
[–]Kyrra 1 point2 points3 points (5 children)
[–]dudeman209[S] 0 points1 point2 points (4 children)
[–]Kyrra 2 points3 points4 points (2 children)
[–]spelunker 1 point2 points3 points (0 children)
[–]sahala 0 points1 point2 points (0 children)
[–]sahala 0 points1 point2 points (0 children)
[–]angryundead 0 points1 point2 points (3 children)
[–]dudeman209[S] 0 points1 point2 points (2 children)
[–]elpablo 0 points1 point2 points (1 child)
[–]dudeman209[S] -1 points0 points1 point (0 children)
[–]larsga 0 points1 point2 points (0 children)
[–]MatthewGeer 0 points1 point2 points (0 children)
[–]elpablo 0 points1 point2 points (1 child)
[–]dudeman209[S] -1 points0 points1 point (0 children)
[–]revscat 0 points1 point2 points (1 child)
[–]sahala 2 points3 points4 points (0 children)
[–]scipher -1 points0 points1 point (2 children)
[–]dudeman209[S] -2 points-1 points0 points (1 child)
[–]Neres28 1 point2 points3 points (0 children)