Adapter Design Pattern

Views:
 
     
 

Presentation Description

Learn how to implement the adapter design pattern

Comments

Presentation Transcript

Adapters Design Pattern: 

Adapters Design Pattern

Overview: 

Overview Objective Learn how to use adapter/strategy design pattern in ProdigyView . Requirements Knowledge of how extend PVPatterns or PVObject Estimated Time 10 Minutes

Follow Along With A Code Example: 

Follow Along With A Code Example Download a copy of the example code at www.prodigyview.com/source . Install the system in an environment you feel comfortable testing in. Proceed to examples/data/Adapters.php

What are adapters?: 

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).

Strategy Design Pattern: 

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.

Strategy Visual: 

Strategy Visual MyObect Function doSomething () { } OtherObject1 extends MyObect Function doSomething () { echo ‘I am happy’; } OtherObject2 extends MyObect Function doSomething () { echo ‘I am sad’; } Empty method Do something does one thing doSomething does something else

Adapters with a Twist: 

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.

Adapters Visual: 

Adapters Visual Call method MyObject :: doSomething () MyObject Executes method doingSomething Output Call method MyObject :: doSomething () MyObject Executes method doingSomething DifferentObject Executes method doingSomething Output Without Adapter With Adapter

PVPatterns and PVStaticPatterns: 

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 MyObject -> addAdapter () PVStaticPatterns MyObject :: addAdapter () vs

Start Our Example: 

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

Has Adapter? Then Execute!: 

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. If adapter exist, execute and return the adapter's results . This will override the current function. Class Adapter is in Function the adapter is in

Class To Adapt To: 

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’.

Round 1: 

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!

Results 1: 

Results 1 The results should have come out to this

Add The Adapter: 

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: 

Round 2 Result

The Not So Obvious: 

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.

Review: 

Review Wasn't to hard, was it? So let's review. Add _hasAdapter and _callAdapter into the class you want to make adaptable. Make sure there is a class that has function to be adapted too. Apply the adapter by setting the class name and function name to adapter and the class that has the adapter. Execute the Function

API Reference: 

API Reference For a better understanding of the Adapters, check out the api at the two links below. PVStaticPatterns PVPatterns www.prodigyview.com More Tutorials For more tutorials, please visit: http:// www.prodigyview.com /tutorials