I'm working on a web application right now that deals with managing university students who are studying in the medical field. Each of these students are required to attend a "Clinical Experience" during some point of their university career. The web application I'm writing is designed to help the universities, clinics (where the students go for their experience), and students to be able to easily coordinate with each other.
I'm using the Kohana framework, so a MVC architectural pattern. Here is the way I have structured the application right now.
Controller
|
--------------------------
| |
Public Dashboards
------|------ |
| | |
Login Recover Password |
|
-------------------------------
| | |
Students Universities Clinics
So under the public section users can either login or recover their password.
The Dashboard section is accessible after the user has logged in, based on the type of user account they will either have a student, univers开发者_运维百科ity, or clinic dashboard. Now of the three dashboard types, each dashboard will have a different set of controllers for what actions the users can take. So the above diagram actually also represents the controller inheritances. A Controller that is part of the Student dashboard has the following inheritance for one of their dashboard sections:
Dashboard Section
-> Student Dashboard
-> Dashboard
-> Controller
Now, this is all fine if each of the different Dashboard Section
for every different dashboard type is unique. As in, all of the actions that are part of that controller are only required for that specific controller.
However the thing is, some of the dashboard types DO have controllers in common. For instance each Dashboard type has a account
controller. This controller allows the user to manage their account. The problem is, I don't want to write the exact same controller code for each account
controller under the Student, University, and Clinic dashboards.
Now what would be nice is if each account
controller could not only inherit from its respective dashboard type (Student
, University
, or Clinic
), but could also inherit methods from a separate class (perhaps called Account_Controller
), where I implement the proper actions for the account controller.
Of course PHP lacks multiple inheritance. So what would be the correct design pattern to implement here to keep my code very DRY?
Right now I have a protected method in the Dashboard
class called account_update
that I just call in each of the Account
controllers for each dashboard type. However this seems really messy. Plus there are other situations where controllers that are common between dashboards will have more than one action.
I think you're looking for generic classes.
精彩评论