jeudi 12 novembre 2015

Clarification regarding Factory Method Patern

I have one super class LoginUser and two subclass AdminUser and StudentUser which extend LoginUser.

Previously, I had two methods getAdminUser and getStudentUser in a class and I used to create an object of AdminUser or StudentUser after fetching their respective role from database and return the same object.

Recently, I have tried implementing factory method pattern:

Following is my new code.

   public LoginUser getUser(String emailId)
{
    SQLCmd cmd = new GetUserIDByEmail(emailId);
    cmd.execute();
    Integer userId = (Integer)cmd.getResult().get(0);
    String userType = cmd.getResult().get(1).toString().toLowerCase();      

    GetUserDetailsCmd gudCMD = null;
    switch(userType)
    {
        case "student":
        {
            gudCMD = new GetStudentById(userId);
            gudCMD.execute();
            break;
        }
        case "admin":
        {
            gudCMD = new GetAdminById(userId);
            gudCMD.execute();
            break;
        }

    }
    return (LoginUser)gudCMD.getResult().get(0);
}

When i call gudCMD.execute(), GetAdminById will create an object of AdminUser and put it to a List. similarly for getStudentById; that is why i type cast and return it.

Please clarify whether this is the correct implementation of Factory method pattern.

Aucun commentaire:

Enregistrer un commentaire