samedi 3 février 2018

Re Factoring branch approach with factory design pattern

Currently, we are having 3 roles and we are using the branch approach to return results based on roles

Interface ICourses{
   getCourses();
}

class Courses: ICourses {
   getCourses(){
      if(role.user) return users;
      if(role.guest) return guests;
      if(role.admin) return admin;
   }
}

// Consumer class
class Consumer{
    ICourses _courses;
    Consumer(ICourses courses){ 
       _courses = courses;
    }
    getCourses(){
      _courses.getCourses();
    }
}

All the methods in all the classes, we have these 3 branch conditions everywhere and when new role gets added we have to modify all the methods which break open/closed principle. Today, I was looking at factory pattern which seems a good fit, we are trying to rearchitect our classes as following

class UserCourses: ICourses{ 
    getCourses(){ return users; }
}
class GuestCourses: ICourses{ 
    getCourses(){ return guests; }
}
class AdminCourses: ICourses{ 
    getCourses(){ return admins; }
}

If tomorrow, a new role is added we have to create a new class implementing the interface. I think this pattern pretty much solves the open/closed principle and also it is more readable. The challenge here is based on the role the concrete class must be injected.

class Consumer{
    ICourses _courses; // This should be of type based on role
    Consumer(ICourses courses){ 
       _courses = courses;
    }
}

Questions

  1. Is this good implementation of the factory pattern?
  2. Is this code more readable or does it create a lot of classes?
  3. Are there any better patterns?
  4. If we are using DI framework, is it possible to configure concrete class injection on every HTTP request?

Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire