2. Overview
Objective
Learn how to use adapter/strategy design pattern in
ProdigyView.
Requirements
Knowledge of how extend PVPatterns or PVObject
Estimated Time
10 Minutes
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/Adapters.php
4. What are adapters?
An adapter is a design pattern than translates one
interface for a class into a compatible interface.
'Come again?'
Adapters allows one class to use another classes
methods when using inheritance would not be the best
solution(meaning using something like 'ClassA extends
ClassB' would be more work than it's work).
5. Strategy Design Pattern
For you to understand how ProdigyView uses
adapter, then you should also have an understanding of
the strategy design pattern. This design patterns
encapsulates a set of algorithms and make them
interchange.
Put in a different way, you have an interface or an
abstract class. The methods are only defined in that
class, the logic of those methods are created in other
classes that extend them.
6. Strategy Visual
MyObect
Function doSomething() {
Empty method
}
doSomething does
something else
Do something does one thing
OtherObject1 extends MyObect OtherObject2 extends MyObect
Function doSomething() { Function doSomething() {
echo ‘I am happy’; echo ‘I am sad’;
} }
7. Adapters with a Twist
In ProdigyView, the adapters are not quite adapters but a
combination of the Adapter and Strategy design pattern.
The purpose of the adapter in ProdigyView is to completely
replace the execution of a method without altering the core code.
When you add an adapter to a method of a class, it will call
another method to perform the execution of the code in it’s place.
If that sounds confusing, read on through this slideshow and it
should become clearer.
Many of the methods in ProdigyView can be altered through
adapters.
9. PVPatterns and PVStaticPatterns
The classes that contain the methods for using adapters is the
PVPatterns and PVStaticPatterns classes.
PVPatterns is for instances and PVStaticPatterns is for static
functions.
Both PVObject and PVStaticObject extend the pattern
classes.
PVPattern Instance PVStaticPatterns
MyObject->addAdapter() vs MyObject::addAdapter()
10. Start Our Example
In the example code, we are going to be building a car. So
lets create the class for that. Notice how this class
extends PVObject which extends PVPatterns that has our
adapters.
Extending PVObject Placing the ability to call an adapter first
Pass the same parameters into the adapter
Code that executes if no adapter is set
11. Has Adapter? Then Execute!
The code on this slide is the same code that appeared at the top
of method on the previous slide.
Adapters are meant to be tied to a class and a method. In our
example we are checking is an adapter has been set for this
class and this method and method combination.
Class Adapter is in Function the adapter is in
If adapter exist, execute and return the adapter's
results. This will override the current function.
12. Class To Adapt To
Now that we have the ability to call an adapter in our
class, lets create a class and method to adapt too!
Notice: Has the same method name and accepts the same
parameters as the method ‘build’ in the class ‘Car’.
13. Round 1
We have all our code set up so lets run it ! Create a
parameter of arguments describing the car and pass it
through!
15. Add The Adapter
Now we are going to add the adapter in to change our
results.
The first two arguments is the class and method to set the
adapter for. The third is the class that has the function that
will be adapted too. The last argument tells the function
that the adapted function is an instance and not static.
The class the adapter is in The method to place the adapter with
The class new class that will handle execute the code
for the method build
17. The Not So Obvious
Arguments Passed
When _callAdapter was executed in this example, the
parameters passed was details. But an infinite amount of
parameters can be passed through this function.
Function Binding
The default method to being called in the adapted class
has the same name of the function calling it. The can be
overridden in ‘addAdapter' in the options array by setting
the 'call_method' to a function name.
18. Review
Wasn't to hard, was it? So let's review.
1. Add _hasAdapter and _callAdapter into the class you
want to make adaptable.
2. Make sure there is a class that has function to be
adapted too.
3. Apply the adapter by setting the class name and
function name to adapter and the class that has the
adapter.
4. Execute the Function
19. API Reference
For a better understanding of the Adapters, 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