Anonymous Functions and Design Patterns

1,181 views
1,086 views

Published on

Learn how to integrate anonymous functions/closures with design patterns.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,181
On SlideShare
0
From Embeds
0
Number of Embeds
84
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Anonymous Functions and Design Patterns

  1. 1. Anonymous Functions and Design Patterns When Yin Finally Meets Yang
  2. 2. OverviewObjectiveLearn how to use the closures/anonymous functions with thedesign patternsRequirements Knowledge of the adapter design pattern Knowledge of the observer design pattern Knowledge of the intercepting filter design patternEstimated Time12 minutes www.prodigyview.com
  3. 3. Follow Along With A Code Example1. Download a copy of the example code at www.prodigyview.com/source.2. Install the system in an environment you feel comfortable testing in.3. Proceed to examples/data/ClosureDesigns.php
  4. 4. Why ClosuresIn our previous examples, we implemented theintercepting filters, observers and adapter design patternsby calling other classes. That can be overkillTo make things a little faster and easier to write, we canimplement these same design patterns by having themcall anonymous functions.
  5. 5. Our Class We are starting this example by creating a class with the 3 design patterns implemented.Extend PVStaticObject Implemented the adapter design pattern Implement Filters Implement Observer
  6. 6. Results We have the design patterns implemented, so lets run the method once and see what result we get without using any of the patterns.Very plain and basic, none of the filters or observers hadany affect. Time to make this method a little bit sexier.
  7. 7. Arguments Filter Now let’s add a filter to the arguments that are initially coming in. Filters with closures are just like regular filters except we have to add the option ‘type’ as a closure.Define the closure Set the class to filter Set the method to filter Add the closure Set the type to ‘closure’ Executes on this event
  8. 8. Arguments Filter Part 2 Next we are going to take adding the anonymous function to the next level. Before we defined the function in a variable and passed it the method. Lets start adding the function directly into the method. Class to Add Filter Method to add filter to Give the filter a name Create the anonymous function byThe type as ‘closure’ Add an event passing it as the parameter
  9. 9. Add the Observer Similar to how we added the anonymous function in the filter, we are going to add the anonymous function to the observer.Set the name of the event Make up a name for the anonymous functionSet the options type to closure Create the anonymous function
  10. 10. ResultsBefore we add the adapter design pattern, we are goingto test the output when we run the method with the filtersand observers.We a little magic, we’ve made our output a little bit livelier.
  11. 11. Adapter and Closure For our final design pattern, we are going to completely override the method by using an adapter and a closure.The class to add the adapter too The method to assign the adapter too The anonymous function in place of the parameterSet the option ‘type’ to ‘closure’
  12. 12. ResultsRemember from our other examples, adding the adapterwill completely override the method. This means theexecution of the code will never reach other designpatterns. The result of adding the adapter will give us this:
  13. 13. Challenge!This is an optional challenge designed to give you a betterunderstanding of the design patterns.1. Look into the source code of the file PVFileManager in the core/util/PVFileManager.php.2. Find the method for writing a file. Notice how it contains the ability to use adapters, filters and observers.3. First apply filters and observers using anonymous functions to the method.4. Then apply an adapter using an anonymous function and write the file out yourself.
  14. 14. API ReferenceFor a better understanding of the Design Patterns, check out the api at the two links below.PVStaticPatternsPVPatterns More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com

×