Beruflich Dokumente
Kultur Dokumente
Overview
Objective Learn how to use the intercepting filter design pattern.
Requirements
Array
Extending an Object
Estimated Time
10 minutes
www.prodigyview.com
3. Proceed to examples/design/Filters.php
My Filter Object Filter passes data to another object to filter Filter is reached Continue Execution Filter Data
Output
We are going to start off with a vending machine example. Our VendingMachine extends PVObject. PVObject extends PVPatterns which has the filter methods we need.
Extending PVObject
Adding A Filter
If you noticed in our filter class, we had an explicit section where the filters are were defined.
Checks if there is a filter for this class and this method
Filters are suppose to be tied to a specific function in a specific class. We can simply use get_class and __Function__ to get the current class and function. The method _hasFitler first determines if they method has any filters. If true, lets execute them. The function has two different filters because there are two different events being performed, a check and a selection. The filter is being applied by passing in the class name, the function name, the data to be filtered, and options. Keep in mind that the filter can only filter one piece of data.
Item Check
Now for our vending we have items like beer which need to be checked before buying. Lets add in a class that handles that.
Options passed about the filter Data passed from the filter
Item Chooser
Soda sounds generic. What kind of soda? Next we are going to add a class to handle what kind of item we are picking.
Data passed from the filter
Vend! Round 1
So we have our vending machine set up. Lets run the function with some data and look at the results. Remember the parameters was item, money and age.
Round 1 Results
Oh man, thats not good. We are having minors getting beer and anyone putting any price and getting what they want. And all the items are generic! Lets fix this.
Round 2 Results
Ok a little better, at least we are getting some none generic items back. But we still need to verify the age and amount tendered.
Calls an instance
Round 3 Result
Finally, the results we need. So that is filters in a nutshell. We modified the data in the method without ever modifying the method.
Challenge!
Below is an optional challenge designed to help you gain a better understanding of the design pattern.
4. Execute code.
Summary
1. Apply a filter using the _applyFilter method. The name
of the class and the name of the function calling the filter should be used. The function only accepts 1 parameter for data to be filters.
API Reference
For a better understanding of the Collections and the Iterator, check out the api at the two links below.
PVStaticPatterns
PVPatterns
More Tutorials
For more tutorials, please visit: http://www.prodigyview.com/tutorials
www.prodigyview.com