SlideShare uma empresa Scribd logo
1 de 14
Anonymous Functions and
    Design Patterns




      When Yin Finally Meets Yang
Overview
Objective
Learn how to use the closures/anonymous functions with the
design patterns
Requirements
 Knowledge of the adapter design pattern
 Knowledge of the observer design pattern
 Knowledge of the intercepting filter design pattern
Estimated Time
12 minutes


                       www.prodigyview.com
Follow Along With A Code
          Example
1. 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
Why Closures
In our previous examples, we implemented the
intercepting filters, observers and adapter design patterns
by calling other classes.

                   That can be overkill

To make things a little faster and easier to write, we can
implement these same design patterns by having them
call anonymous functions.
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
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 had
any affect. Time to make this method a little bit sexier.
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
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 by
The type as ‘closure’   Add an event        passing it as the parameter
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 function




Set the options type to closure             Create the anonymous function
Results
Before we add the adapter design pattern, we are going
to test the output when we run the method with the filters
and observers.




We a little magic, we’ve made our output a little bit livelier.
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 parameter

Set the option ‘type’ to ‘closure’
Results
Remember from our other examples, adding the adapter
will completely override the method. This means the
execution of the code will never reach other design
patterns. The result of adding the adapter will give us this:
Challenge!
This is an optional challenge designed to give you a better
understanding 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.
API Reference
For a better understanding of the Design Patterns, 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

Mais conteúdo relacionado

Mais de ProdigyView

Audio Content Management
Audio Content ManagementAudio Content Management
Audio Content ManagementProdigyView
 
File Content Management
File Content ManagementFile Content Management
File Content ManagementProdigyView
 
HTML5 Tags and Elements Tutorial
HTML5 Tags and Elements TutorialHTML5 Tags and Elements Tutorial
HTML5 Tags and Elements TutorialProdigyView
 
HTML Forms Tutorial
HTML Forms TutorialHTML Forms Tutorial
HTML Forms TutorialProdigyView
 
Html Tags Tutorial
Html Tags TutorialHtml Tags Tutorial
Html Tags TutorialProdigyView
 
Video Conversion PHP
Video Conversion PHPVideo Conversion PHP
Video Conversion PHPProdigyView
 
Sending Email Basics PHP
Sending Email Basics PHPSending Email Basics PHP
Sending Email Basics PHPProdigyView
 
Tools ProdigyView
Tools ProdigyViewTools ProdigyView
Tools ProdigyViewProdigyView
 
Custom Validation PHP
Custom Validation PHPCustom Validation PHP
Custom Validation PHPProdigyView
 
Basic File Cache Tutorial - PHP
Basic File Cache Tutorial - PHPBasic File Cache Tutorial - PHP
Basic File Cache Tutorial - PHPProdigyView
 
Understanding Web Cache
Understanding Web CacheUnderstanding Web Cache
Understanding Web CacheProdigyView
 
Javascript And CSS Libraries
Javascript And CSS LibrariesJavascript And CSS Libraries
Javascript And CSS LibrariesProdigyView
 
SQL Prepared Statements Tutorial
SQL Prepared Statements TutorialSQL Prepared Statements Tutorial
SQL Prepared Statements TutorialProdigyView
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design PatternProdigyView
 
Intercepting Filters Design Pattern
Intercepting Filters Design PatternIntercepting Filters Design Pattern
Intercepting Filters Design PatternProdigyView
 
PV Standard Search Query
PV Standard Search QueryPV Standard Search Query
PV Standard Search QueryProdigyView
 
Learning PHP Basics Part 2
Learning PHP Basics Part 2Learning PHP Basics Part 2
Learning PHP Basics Part 2ProdigyView
 

Mais de ProdigyView (19)

Audio Content Management
Audio Content ManagementAudio Content Management
Audio Content Management
 
File Content Management
File Content ManagementFile Content Management
File Content Management
 
HTML5 Tags and Elements Tutorial
HTML5 Tags and Elements TutorialHTML5 Tags and Elements Tutorial
HTML5 Tags and Elements Tutorial
 
HTML Forms Tutorial
HTML Forms TutorialHTML Forms Tutorial
HTML Forms Tutorial
 
Html Tags Tutorial
Html Tags TutorialHtml Tags Tutorial
Html Tags Tutorial
 
Video Conversion PHP
Video Conversion PHPVideo Conversion PHP
Video Conversion PHP
 
Sending Email Basics PHP
Sending Email Basics PHPSending Email Basics PHP
Sending Email Basics PHP
 
Tools ProdigyView
Tools ProdigyViewTools ProdigyView
Tools ProdigyView
 
Custom Validation PHP
Custom Validation PHPCustom Validation PHP
Custom Validation PHP
 
Basic File Cache Tutorial - PHP
Basic File Cache Tutorial - PHPBasic File Cache Tutorial - PHP
Basic File Cache Tutorial - PHP
 
Understanding Web Cache
Understanding Web CacheUnderstanding Web Cache
Understanding Web Cache
 
Javascript And CSS Libraries
Javascript And CSS LibrariesJavascript And CSS Libraries
Javascript And CSS Libraries
 
PHP Libraries
PHP LibrariesPHP Libraries
PHP Libraries
 
SQL Prepared Statements Tutorial
SQL Prepared Statements TutorialSQL Prepared Statements Tutorial
SQL Prepared Statements Tutorial
 
Database Basics
Database BasicsDatabase Basics
Database Basics
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design Pattern
 
Intercepting Filters Design Pattern
Intercepting Filters Design PatternIntercepting Filters Design Pattern
Intercepting Filters Design Pattern
 
PV Standard Search Query
PV Standard Search QueryPV Standard Search Query
PV Standard Search Query
 
Learning PHP Basics Part 2
Learning PHP Basics Part 2Learning PHP Basics Part 2
Learning PHP Basics Part 2
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Anonymous Functions and Design Patterns

  • 1. Anonymous Functions and Design Patterns When Yin Finally Meets Yang
  • 2. Overview Objective Learn how to use the closures/anonymous functions with the design patterns Requirements  Knowledge of the adapter design pattern  Knowledge of the observer design pattern  Knowledge of the intercepting filter design pattern Estimated Time 12 minutes www.prodigyview.com
  • 3. Follow Along With A Code Example 1. 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. Why Closures In our previous examples, we implemented the intercepting filters, observers and adapter design patterns by calling other classes. That can be overkill To make things a little faster and easier to write, we can implement these same design patterns by having them call anonymous functions.
  • 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. 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 had any affect. Time to make this method a little bit sexier.
  • 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. 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 by The type as ‘closure’ Add an event passing it as the parameter
  • 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 function Set the options type to closure Create the anonymous function
  • 10. Results Before we add the adapter design pattern, we are going to test the output when we run the method with the filters and observers. We a little magic, we’ve made our output a little bit livelier.
  • 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 parameter Set the option ‘type’ to ‘closure’
  • 12. Results Remember from our other examples, adding the adapter will completely override the method. This means the execution of the code will never reach other design patterns. The result of adding the adapter will give us this:
  • 13. Challenge! This is an optional challenge designed to give you a better understanding 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. API Reference For a better understanding of the Design Patterns, 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