带视图的装饰器

发布于 2024-12-19 14:01:54 字数 577 浏览 0 评论 0原文

如何在结构视图中应用装饰器,知道只有一个视图负责执行代码块来呈现模板?如:

<?php
          class View {

                 public function render ( $template ) {
                        printf ( 'Rendering template %s' , $template ) ;
                 }

          }

          class MemberView { }

          class AdministratorView { }


          $Admin = new MemberView ( new AdministratorView ( ) ) ;
          $Admin->render ( ) ;

在本例中,我正在呈现管理员,但是在浏览驱动程序时,将获取每个成员已到达视图并执行以显示面板的基本功能,或者只有管理员可以看到的东西,但在逻辑上,管理员也是成员..因此必须制定结构,适用于版主、全局版主..

我的问题是如何在这种情况下应用装饰器..

How to apply a decorator in a structure views, knowing that only a single view is responsible for executing the block of code to render the template? as:

<?php
          class View {

                 public function render ( $template ) {
                        printf ( 'Rendering template %s' , $template ) ;
                 }

          }

          class MemberView { }

          class AdministratorView { }


          $Admin = new MemberView ( new AdministratorView ( ) ) ;
          $Admin->render ( ) ;

In this case, I am rendering an administrator, but when going through the drivers, will acquire and the basic functions that every member has reached the view and perform to show panels, or things that only administrators can see, but in logic, a administrator is also a member .. so the structure has to be made​​, applies to moderators, global moderators ..

My question is that of how to apply the decorator in this case ..

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

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

发布评论

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

评论(1

旧话新听 2024-12-26 14:01:54

好吧,我们设法解决了问题.. 解决方案:

<?php

          class View {

                 public function render ( $template ) {
                        printf ( 'Rendering template %s' , $template ) ;
                 }

          }

          abstract class ViewDecorator extends View {

                 protected $view ;

                 public function __construct ( View $view ) {
                        $this->view = $view ;
                 }

          }

          class MemberView extends ViewDecorator {

                 public function render ( ) {
                        $this->view->render ( 'Member' ) ;
                 }

          }

          class AdministratorView extends ViewDecorator {

                 public function render ( ) {
                        $this->view->render ( 'Administrator' ) ;
                 }

          }

          $ADM = new MemberView ( new AdministratorView ( new View ( ) ) ) ;
          $ADM->render ( ) ;

输出:渲染模板 管理员

如果我删除管理员的渲染。

<?php
          $Member = new MemberView ( new View ( ) ) ;
          $Member->render ( ) ;

输出:渲染模板成员

Well, we managed to solve the problem .. the solution:

<?php

          class View {

                 public function render ( $template ) {
                        printf ( 'Rendering template %s' , $template ) ;
                 }

          }

          abstract class ViewDecorator extends View {

                 protected $view ;

                 public function __construct ( View $view ) {
                        $this->view = $view ;
                 }

          }

          class MemberView extends ViewDecorator {

                 public function render ( ) {
                        $this->view->render ( 'Member' ) ;
                 }

          }

          class AdministratorView extends ViewDecorator {

                 public function render ( ) {
                        $this->view->render ( 'Administrator' ) ;
                 }

          }

          $ADM = new MemberView ( new AdministratorView ( new View ( ) ) ) ;
          $ADM->render ( ) ;

The output: Rendering template Administrator

If I remove the rendering of the administrator.

<?php
          $Member = new MemberView ( new View ( ) ) ;
          $Member->render ( ) ;

The output: Rendering template Member

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