SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast matid's Avatar
    Join Date
    May 2005
    Location
    Knurow, Poland
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Composite View and views' ralations problem

    I want to use Composite View pattern in my framework to be able to construct views from few parts, but I encountered a problem:
    I've got MainCompositeView, that uses 3 other Views: HeaderView, BodyView and FooterView.
    And now - BodyView takes care of getting article from the model and displaying it, however, I'd like the title of this article to be displayer on browsers' title bar, what can be archived by setting <title> tag in HeaderView.
    Should I be able to share data between these two Views? From logical point of view they're totally independant...

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should I be able to share data between these two Views?
    I use the Visitor pattern as my Composite render, of which all Composites visit this renderer, and thus able to share common data, and this is what your proberly looking for

  3. #3
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    I use the Visitor pattern as my Composite render, of which all Composites visit this renderer, and thus able to share common data, and this is what your proberly looking for
    I agree with that 100%....should do the job...
    Business as usual is off the menu folks, ...

  4. #4
    SitePoint Enthusiast matid's Avatar
    Join Date
    May 2005
    Location
    Knurow, Poland
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have some problem implementing it.
    I've got something like this: http://matid.emmatrade.pl/files/Visitor.phps
    I understand the idea of Vistor, but how should I visit one view from another?

  5. #5
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matid
    I have some problem implementing it.
    I've got something like this: http://matid.emmatrade.pl/files/Visitor.phps
    I understand the idea of Vistor, but how should I visit one view from another?
    This is not the exact solution but a goiod guideline on how to update the different components your view has, you want a different view, construct one with different settings by updating the current view and passing out a clone.

    class CompositeView extends View implements iCompositeView
    {
    public function update(ICompositeView $oCompositeView){
    $this->__compositeView = $oCompositeView;
    //OR
    $this->__compositeView->update($this); //clone fort deep copy.
    }

    public function __clone(){
    //Invocation or something...
    }
    }
    Hope Livingstonecan add his comment since im self educated in this so take it for 90% alright....

    cheers,
    Galo
    Business as usual is off the menu folks, ...

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay... Pause for a moment, till I gather my thoughts...

    First, I can't post any script I have upto now developed, but in all fairness, and to help the PHP community as a whole, I have been working on another approach, and on the theme of the Composite View pattern, will post the following 'proof of concept' script instead.

    It's rough, no unit testing, etc, but it works so you can get the basic idea at least

    PHP Code:
    interface IComposite {
            public function 
    getId();
            public function 
    getParent();
            public function 
    getTemplate();
        }
        
        interface 
    IActionHandler {
        }
        
        abstract class 
    BaseComposite implements IComposite {
            public 
    $children = array();
            
            abstract public function 
    __construct();
            abstract public function 
    attachIComposite $composite );
            abstract public function 
    acceptVisitor $visitor );
            
            public function 
    getId() {
                return 
    $this -> controller -> id;
            }
            
            public function 
    getParent() {
                return 
    $this -> controller -> parent;
            }
            
            public function 
    getTemplate() {
                return 
    $this -> controller -> template;
            }
        }
        
        abstract class 
    BaseActionHandler implements IActionHandler {
            public function 
    __construct() {}
            
        }
        
        class 
    PageActionHandler extends BaseActionHandler {
            public 
    $id;
            public 
    $parent;
            public 
    $template;
            
            public function 
    __construct() {
                
    $this -> id 'page';
                
    $this -> parent '';
            }
            
            public function 
    execute$request ) {
                
    // you would do you own complex calculations here,
                // work out your models, etc, keeping the models here with
                // the controller, thus
                //
                // letting the visitor access those models (from the visitor), so
                // it's the visitor that composes the template (view) with the 
                // model data, from the actual visitor it's self
                //
                // as this is an example, i'm just returning a basic template file
                // so you can see how it all gells together ;)
                
    return file_get_contents'page.tpl' );
            }
        }
        
        class 
    BodyActionHandler extends BaseActionHandler {
            public 
    $id;
            public 
    $parent;
            public 
    $template;
            
            public function 
    __construct() {
                
    $this -> id 'body';
                
    $this -> parent 'page';
            }
            
            public function 
    execute$request ) {
                return 
    file_get_contents'body.tpl' );
            }
        }
        
        class 
    FooterActionHandler extends BaseActionHandler {
            public 
    $id;
            public 
    $parent;
            public 
    $template;
            
            public function 
    __construct() {
                
    $this -> id 'footer';
                
    $this -> parent 'page';
            }
            
            public function 
    execute$request ) {
                return 
    file_get_contents'footer.tpl' );
            }
        }
        
        class 
    MenuActionHandler extends BaseActionHandler {
            public 
    $id;
            public 
    $parent;
            public 
    $template;
            
            public function 
    __construct() {
                
    $this -> id 'menu';
                
    $this -> parent 'body';
            }
            
            public function 
    execute$request ) {
                return 
    file_get_contents'menu.tpl' );
            }
        }
        
        class 
    Composite extends BaseComposite {
            public function 
    __constructIActionHandler $controller ) {
                
    $this -> controller $controller;
            }
            
            public function 
    attachIComposite $composite ) {
                
    $this -> children[] = $composite;
            }
            
            public function 
    acceptVisitor $visitor ) {
                
    $visitor -> visit$this );
            }
        }
        
        class 
    Visitor {
            private 
    $request;
            
            public function 
    __construct$request ) {
                
    $this -> request $request;
            }
            
            public function 
    visitIComposite $composite ) {
                
    $this -> template[$composite -> getId()] = $composite -> controller -> execute$this -> request );
                foreach( 
    $composite -> children as $child ) {
                    
    $this -> visit$child );
                    
    $this -> template[$composite -> getId()] = ereg_replace'<'.$child -> getId().' />'$this -> template[$child -> getId()], $this -> template[$composite -> getid()] );
                    
                }
            }
            
        }
        
        class 
    Request {} // your request,
        
        
    $visitor = new Visitor( new Request() );
        
    $c = new Composite( new PageActionHandler() );
        
    $body = new Composite( new BodyActionHandler() );
        
    $body -> attach( new Composite( new MenuActionHandler() ) );
        
    $c -> attach$body );
        
    $c -> attach( new Composite( new FooterActionHandler() ) );
        
    $c -> accept$visitor );
        
        echo( 
    $visitor -> template['page'] ); 
    And the crappy template files,

    page.tpl
    Code:
    <html><body>
    
    	<p>This is the PAGE template</p>
    	<p><body /></p>
    	<p><footer /></p>
    	
    </body></html><!-- a shi**y template but it'll do :p -->
    body.tpl
    Code:
    This is the BODY template<p><menu /></p>
    menu.tpl
    Code:
    And finally, this is the MENU, belonging to the BODY template
    footer.tpl
    Code:
    This is the FOOTER template
    And, the resultant output generated,

    Code:
    <html><body>
    
    	<p>This is the PAGE template</p>
    	<p>This is the BODY template<p>And finally, this is the MENU, belonging to the BODY template</p></p>
    	<p>This is the FOOTER template</p>
    	
    </body></html><!-- a shi**y template but it'll do :p -->
    Umm...

    The Visitor in question would require a means to gain access to the Model data from the Controller it's self [MVC], and in one form or another, prior to the foreach() { ... } loop, insert the Model data to the template.

    For this example, I just returned a basic file_get_contents() but in fact, it's the template to use based on the logic that the Controller has. The View [MVC] is the Visitor it's self, and for most purposes, you only need the one Visitor, so let's keep it simple.

    Questions, Comments, Etc Etc I'm all open to hear from you. As to the problem of MatID, I would need to see more script, bar just the Interfaces,

    Dr Livingston


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •