Issue
I found this code in github for student management system thats use DTO pattern
public interface SuperController<T extends SuperDTO>{
public boolean add(T dto) throws SQLException;
public T getAll(String id)throws SQLException; }
and SuperDTO is empty class without any fields or methods
public class SuperDTO {
}
my problem i can uderstand how to call this SuperController using the following class :
public class ControllerFactory{
private static ControllerFactory controllerFactory;
private BatchDBControllerImpl ctrlBatch;
public enum ControllerType{
BATCH,CLASSES,LOGIN,REGISTER,STUDENT,ATTENDANCE,PAYMENT,EXAM,EXAM_DETAIL;
}
private ControllerFactory() {
}
public static ControllerFactory getInstance(){
if(controllerFactory==null){
controllerFactory=new ControllerFactory();
}
return controllerFactory;
}
public SuperController getController(ControllerType type){
switch(type){
case BATCH:
return new BatchDBControllerImpl();
case CLASSES:
return new ClassesDBControllerImpl();
case LOGIN:
return new LoginDBControllerImpl();
case REGISTER:
return new RegisterDBControllerImpl();
case STUDENT:
return new StudentDBControllerImpl();
case ATTENDANCE:
return new AttendanceDBControllerImpl();
case PAYMENT:
return new PaymentDBControllerImpl();
case EXAM_DETAIL:
return new ExamDetailDBControllerImpl();
case EXAM:
return new ExamDBControllerImpl();
default :
return null;
}
}
}
please explain me this method :
public SuperController getController(ControllerType type)
Solution
I'm unsure what you do and don't understand so please comment or edit your question if you need further clarification.
ControllerFactory
is known as a factory object; this is a pattern of design in which instead of calling an object's constructor directly to instantiate it, we use a different class which is solely responsible for the task. In this case, the factory builds what seems to be different types of database controllers (each having different expected functionality) all of which implement the SuperController
interface. The benefit here is that we can use this factory to create all of these objects with the same shared type. In addition, this factory is known as a singleton because there can only ever be one instance of the object created (note the static reference and private constructor).
So, once you have your factory you can use the getController
method to get an instance of your desired object.
SuperController
can be thought of as a contract detailing what basic functionality is expected from each of the potential objects that implement it. The exact details of implementation differ, but the overall pattern of behavior should be similar between them.
ControllerType
is an enum that defines what valid types of controllers there are, and then based on which type is passed in a different kind of SuperController is constructed. Note that no matter which is returned it will always be of type SuperController
since it implemented that interface.
If all you want to know is how to use it, then you need to get your factory instance, then use it to get your controller:
ControllerFactory controllerFactory = ControllerFactory.getInstance();
SuperController controller = controllerFactory.getController(ControllerFactory.ControllerType.DESIRED_TYPE_HERE);
Answered By - Stalemate Of Tuning
Answer Checked By - Mildred Charles (JavaFixing Admin)