Download - Adapter design-pattern2015
Object Oriented Programming
Design Patterns
The Adapter Pattern
An example of Object Oriented PHP
Implementation for
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
Links
Drupal.org: VicOnt, nafes
Presentation Code:
https://www.drupal.org/project/oop_design_patterns
https://www.drupal.org/project/oop_examples
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns: The Gang of Four!
It all started around 1994 when a group of 4 IBM programmers:
Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides
based on their coding experience , were able to observe and document
a set of about 23 common problems and their best accepted solutions
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
What are design patterns?
Recurring solutions to common software development challenges
< https://www.drupal.org/project/oop_design_patterns >
O
OOP Design Patterns
Some common software development challenges:
➢ Building new applications while leveraging existing codebases
➢ Building/enhancing applications securely, quickly and effectively
➢ Integrating new and existing applications
➢ Multiple developers programming the same product
➢ Few developers programming many products
➢ Varying, sometimes unknown, data sources, OS, Setups
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
What are design patterns not?
a) They are not the end to all of your coding challenges.
b) They are not a requirement of object oriented programming.
c) They are not a substitute for application code and logic.
d) They are not always the best solution for the job.
e) They are not always the easiest concept to grasp and/or use.
< https://www.drupal.org/project/oop_design_patterns >
b) DDT DRY DOT OnceAndOnlyOnce KISS/YAGNI PARETODon’t Do Twice Don’t Repeat Yourself Do One Thing Each and every declaration of behavior should appear Once (For) You ain't gonna need it 80effort/20functionality
normalization in the RelationalModel and removing redundancies 80%of code is crap
Programmers’ Code, Rule #27, Rules are best broken by those who have mastered them
OOP Design Patterns
GOF Classification for patterns
CreationalWhat: deal with object creation mechanisms
How: somehow controlling the object
creation
Example: Abstract Factory
Create a series of related objects without
specifying their distinct classes.
Usually all classes implements the same
interface.
< https://www.drupal.org/project/oop_design_patterns >
StructuralWhat: ease/simplify the design
How: by identifying a simple way to realize
relationships between entities.
Example: Adapter/Wrapper
Translate one interface for a class into a
compatible interface –by providing it’s interface
to clients while using the original interface.
BehavioralWhat: identify common communication
patterns between objects
How: realizing these patterns
Example: Template Method
A skeleton of an algorithm, well-suited for
framework libraries. The user implements
one method and the superclass do the job.
OOP Design Patterns
The Adapter Pattern
Purpose: allows the interface of an existing class to be used from another interface. It is often used to make existing classes work with others without modifying their source code.
Examples where Adapter Pattern may help:
• I want to add search capabilities to my application and support
multiple search engines such as Google, Yahoo, Bing…
• Have an image loader for different kinds of images JPG, GIF, PNG…
• Use different Email providers: Gmail, Hotmail, YahooMail…
• Use different data sources: Db, XML, TXT, CSV…
• Connect to different Db: MySQL, PostgreSQL, MSSQL, Oracle
• Have multiple downloads using different protocols: HTTP, HTTPS, FTP…
• Use different payment providers: Moneris, AuthorizeNet, Skrill…
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
If Client and Adaptee are incompatible, we may create a new class called Adapter, and place it in between Target and Adaptee.Components become compatible with each other through Adapter.As long as some component integrates incompatible interfaces, it can be considered an adapter.
< https://www.drupal.org/project/oop_design_patterns >
Usually Adaptee classimplementsthe Target interface.
Client and Adapteeare compatible!
OOP Design Patterns
PHP Adapter: When you've opened a can of worms, then the
only way to seal them again - Is using a bigger jar
< https://www.drupal.org/project/oop_design_patterns >
The Interface
Incompatible
classes
OOP Design Patterns
Design patterns are a great way to modularize your code
and create reusable structures.
But they mean nothing if they go unused.
Go code something.
< https://www.drupal.org/project/oop_design_patterns >
OOP Design PatternsThe Problem: multiple Payment Providers
< https://www.drupal.org/project/oop_design_patterns >
OOP Design PatternsThe Solution: Adapter/Wrapper for Providers 1, 3
< https://www.drupal.org/project/oop_design_patterns >Scope res op
OOP Design PatternsThe Solution: Adapter/Wrapper for Provider 2
< https://www.drupal.org/project/oop_design_patterns >
OOP Design PatternsAdapter Pattern: UML diagram, programming to the interface
< https://www.drupal.org/project/oop_design_patterns >
OOP Design PatternsAdapter Pattern: Easy of use, programming to the interface
OOP Design PatternsAdapter Pattern: Drupal 7 implementation
< https://www.drupal.org/project/oop_design_patterns >
OOP Design PatternsAdapter Pattern: Drupal 8 implementation
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
CodeCase in action:
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
CodeCase in action:
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
To be Continued...
Controller (GenRespAsignSoftPatt), Facade (GOF)
➢ Probably one of the most common patterns in OOP
➢ One of the most powerful patterns in development
➢ Opens a wealth of new possibilities, when understood
➢ Very flexible when favoring composition over inheritance
➢ Encapsulates what changes to allow for greater extendability
< https://www.drupal.org/project/oop_design_patterns >
OOP Design Patterns
Acknowledgments
Donquixote (Andreas Hennings)
For PHP Class loading suite (PSR-4)
https://www.drupal.org/project/xautoload
Nafes (Nik Alexandrov)
For Starting OOP examples in Drupal
https://www.drupal.org/project/oop_examples
< https://www.drupal.org/project/oop_design_patterns >