Kohana,模拟多重继承?

发布于 2024-12-02 14:22:01 字数 1617 浏览 2 评论 0原文

我现在正在开发一个网络应用程序,用于管理在医学领域学习的大学生。这些学生中的每一位都需要在大学生涯的某个阶段参加“临床体验”。我正在编写的 Web 应用程序旨在帮助大学、诊所(学生去那里获得体验)和学生能够轻松地相互协调。

我正在使用 Kohana 框架,即 MVC 架构模式。这是我现在构建应用程序的方式。

                      Controller
                          |
              --------------------------
              |                        |
            Public                 Dashboards
        ------|------                  |
        |           |                  |
      Login  Recover Password          |
                                       |
                         -------------------------------
                         |             |               |
                     Students     Universities      Clinics

因此,在公共部分下,用户可以登录或恢复密码。

用户登录后即可访问仪表板部分,具体取决于用户帐户的类型,他们将拥有学生、大学或诊所仪表板。现在,在三种仪表板类型中,每个仪表板都将具有一组不同的控制器,用于控制用户可以执行的操作。所以上图实际上也代表了控制器的继承。作为学生仪表板一部分的控制器对其仪表板部分之一具有以下继承:

仪表板部分 -> 学生仪表板 -> 仪表板 -> 现在,如果每个不同的仪表板类型的

每个不同的仪表板部分都是唯一的,那么这一切都很好。就像这样,属于该控制器一部分的所有操作仅对该特定控制器来说是必需的。

然而事实是,某些仪表板类型确实具有共同的控制器。例如,每个仪表板类型都有一个account控制器。该控制器允许用户管理他们的帐户。问题是,我不想为学生、大学和诊所仪表板下的每个帐户控制器编写完全相同的控制器代码。

现在,如果每个 account 控制器不仅能够继承其各自的仪表板类型(StudentUniversityClinic,那就太好了),但也可以从单独的类(可能称为 Account_Controller)继承方法,在其中我为帐户控制器实现正确的操作。

当然,PHP 缺乏多重继承。那么在这里实现什么是正确的设计模式来保持我的代码非常干燥呢?

现在,我在 Dashboard 类中有一个名为 account_update 的受保护方法,我只需在每个仪表板类型的每个 Account 控制器中调用该方法。然而这看起来真的很混乱。另外,在其他情况下,仪表板之间通用的控制器将具有多个操作。

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, university, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2024-12-09 14:22:01

我认为您正在寻找通用类。

I think you're looking for generic classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文