Jump to content

Recommended Posts

Hey guys

 

I want to use the visitor design pattern. I kind of understand but thought you guys might help more

 

Should each visitor perform its own actions on the collection it visits, if so is this concurrent in php? Are there locks in place to prevent access when another visitor is iterating through the same elements of the collection.

 

any help on this would be great

cheers

Link to comment
https://forums.phpfreaks.com/topic/46466-visitor-design-pattern/
Share on other sites

Hey

 

Yeah I want to implement it and learn about it as well. I have been doing some reading and I found a solution to iterating different visitors over one collection:

 

foreach($allVisitors as $visitor)
{
$indexed->accept($visitor);
}

 

is that right, this then brings me to the question - if i have two visitors they both duplicate iteration (it may be an advantage because they can implement their own visit functions and do whatever they want.

 

How would you make it so that the Visitable object can iterate its own collection and each Visitor performs it function per element.

 

any help be great

I meant do you have a problem that requires the visitor pattern to solve it :) It's very difficult to just "implement" a pattern without a problem to solve.

 

Visitor pattern can be metamorph'd as "A guest comes to your house, for every guest you offer a cup of tea. The guest is then free to do what it likes with that cup of tea."

My reason to use it is for visiting and discovering web pages within a website structure and the visitor pattern seems to be ideal for that (I think).

 

1. Ive implemented a crawler that searches a website and places the pages its found in a collection

2. I use the visitors(stored in an array) and pass them along to the collection and they perform their tasks as needed.

 

Step 2 is my worry, Is there a better way to implement the pattern so i reduce the step of storing it in the collection and just perform the tasks when it finds a new page

 

cheers for your help

You could call the function at the point where you would store the page in a collection; ergo just skipping the collection all together.

 

so for a brief example:

<?php

$array = array();
for ($i = 0; $i < 10; $i++)
{
    $array[$i] = $i;
}

foreach ($array as $var)
{
    doSomething($var);
}

// change to..
for($i = 0; $i < 10; $i++)
{
    doSomething($i);
}

?>

That may seem quite a weak example, but the logic is still the same.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.