Case-Sensitive Posted April 10, 2007 Share Posted April 10, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/46466-visitor-design-pattern/ Share on other sites More sharing options...
Jenk Posted April 11, 2007 Share Posted April 11, 2007 Do you have a reason to use the visitor pattern, or do you simply want to learn about it? Quote Link to comment https://forums.phpfreaks.com/topic/46466-visitor-design-pattern/#findComment-226638 Share on other sites More sharing options...
Case-Sensitive Posted April 12, 2007 Author Share Posted April 12, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/46466-visitor-design-pattern/#findComment-227690 Share on other sites More sharing options...
Jenk Posted April 13, 2007 Share Posted April 13, 2007 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." Quote Link to comment https://forums.phpfreaks.com/topic/46466-visitor-design-pattern/#findComment-228276 Share on other sites More sharing options...
Case-Sensitive Posted April 13, 2007 Author Share Posted April 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/46466-visitor-design-pattern/#findComment-228373 Share on other sites More sharing options...
Jenk Posted April 13, 2007 Share Posted April 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/46466-visitor-design-pattern/#findComment-228403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.