Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Implementing the Adapter Design Pattern


Published on

Learn how to implement the adapter design pattern

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Implementing the Adapter Design Pattern

  1. 1. Adapters Design Pattern
  2. 2. OverviewObjectiveLearn how to use adapter/strategy design pattern inProdigyView.Requirements Knowledge of how extend PVPatterns or PVObjectEstimated Time10 Minutes
  3. 3. Follow Along With A Code Example1. Download a copy of the example code at Install the system in an environment you feel comfortable testing in.3. Proceed to examples/data/Adapters.php
  4. 4. What are adapters?An adapter is a design pattern than translates oneinterface for a class into a compatible interface.Come again?Adapters allows one class to use another classesmethods when using inheritance would not be the bestsolution(meaning using something like ClassA extendsClassB would be more work than its work).
  5. 5. Strategy Design PatternFor you to understand how ProdigyView usesadapter, then you should also have an understanding ofthe strategy design pattern. This design patternsencapsulates a set of algorithms and make theminterchange.Put in a different way, you have an interface or anabstract class. The methods are only defined in thatclass, the logic of those methods are created in otherclasses that extend them.
  6. 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. 7. Adapters with a TwistIn ProdigyView, the adapters are not quite adapters but acombination of the Adapter and Strategy design pattern.The purpose of the adapter in ProdigyView is to completelyreplace the execution of a method without altering the core code.When you add an adapter to a method of a class, it will callanother method to perform the execution of the code in it’s place.If that sounds confusing, read on through this slideshow and itshould become clearer.Many of the methods in ProdigyView can be altered throughadapters.
  8. 8. Adapters VisualCall method MyObject::doSomething() Call method MyObject::doSomething() MyObject MyObject DifferentObject Executes method Executes method Executes method doingSomething doingSomething doingSomething Output Output
  9. 9. PVPatterns and PVStaticPatternsThe classes that contain the methods for using adapters is thePVPatterns and PVStaticPatterns classes.PVPatterns is for instances and PVStaticPatterns is for staticfunctions.Both PVObject and PVStaticObject extend the patternclasses. PVPattern Instance PVStaticPatterns MyObject->addAdapter() vs MyObject::addAdapter()
  10. 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 adapterCode that executes if no adapter is set
  11. 11. Has Adapter? Then Execute!The code on this slide is the same code that appeared at the topof method on the previous slide.Adapters are meant to be tied to a class and a method. In ourexample we are checking is an adapter has been set for thisclass and this method and method combination. Class Adapter is in Function the adapter is in If adapter exist, execute and return the adapters results. This will override the current function.
  12. 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 sameparameters as the method ‘build’ in the class ‘Car’.
  13. 13. Round 1We have all our code set up so lets run it ! Create aparameter of arguments describing the car and pass itthrough!
  14. 14. Results 1The results should have come out to this
  15. 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
  16. 16. Round 2 Result
  17. 17. The Not So ObviousArguments PassedWhen _callAdapter was executed in this example, theparameters passed was details. But an infinite amount ofparameters can be passed through this function.Function BindingThe default method to being called in the adapted classhas the same name of the function calling it. The can beoverridden in ‘addAdapter in the options array by settingthe call_method to a function name.
  18. 18. ReviewWasnt to hard, was it? So lets 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. 19. API ReferenceFor a better understanding of the Adapters, check out the api at the two links below.PVStaticPatternsPVPatterns More TutorialsFor more tutorials, please visit: