• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Implementing the Adapter Design Pattern
 

Implementing the Adapter Design Pattern

on

  • 1,884 views

Learn how to implement the adapter design pattern

Learn how to implement the adapter design pattern

Statistics

Views

Total Views
1,884
Views on SlideShare
1,864
Embed Views
20

Actions

Likes
0
Downloads
14
Comments
0

1 Embed 20

http://www.prodigyview.com 20

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Implementing the Adapter Design Pattern Implementing the Adapter Design Pattern Presentation Transcript

    • Adapters Design Pattern
    • OverviewObjectiveLearn how to use adapter/strategy design pattern inProdigyView.Requirements Knowledge of how extend PVPatterns or PVObjectEstimated Time10 Minutes
    • 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/Adapters.php
    • 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).
    • 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.
    • 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’; } }
    • 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.
    • Adapters VisualCall method MyObject::doSomething() Call method MyObject::doSomething() MyObject MyObject DifferentObject Executes method Executes method Executes method doingSomething doingSomething doingSomething Output Output
    • 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()
    • 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
    • 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.
    • 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’.
    • Round 1We have all our code set up so lets run it ! Create aparameter of arguments describing the car and pass itthrough!
    • Results 1The results should have come out to this
    • 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
    • Round 2 Result
    • 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.
    • 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
    • API ReferenceFor a better understanding of the Adapters, check out the api at the two links below.PVStaticPatternsPVPatterns More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com