19
Adapters Design Pattern

Implementing the Adapter Design Pattern

Embed Size (px)

DESCRIPTION

Learn how to implement the adapter design pattern

Citation preview

Page 1: Implementing the Adapter Design Pattern

Adapters Design Pattern

Page 2: Implementing the Adapter Design Pattern

Overview

Objective

Learn how to use adapter/strategy design pattern in ProdigyView.

Requirements

Knowledge of how extend PVPatterns or PVObject

Estimated Time

10 Minutes

Page 3: Implementing the Adapter Design Pattern

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

Page 4: Implementing the Adapter Design Pattern

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

Page 5: Implementing the Adapter Design Pattern

Strategy Design PatternFor 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.

Page 6: Implementing the Adapter Design Pattern

Strategy VisualMyObect

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

Page 7: Implementing the Adapter Design Pattern

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.

Page 8: Implementing the Adapter Design Pattern

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

Page 9: Implementing the Adapter Design Pattern

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 InstanceMyObject-

>addAdapter()

PVStaticPatternsMyObject::addAdapter

()vs

Page 10: Implementing the Adapter Design Pattern

Start Our ExampleIn 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

Page 11: Implementing the Adapter Design Pattern

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

Page 12: Implementing the Adapter Design Pattern

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

Page 13: Implementing the Adapter Design Pattern

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!

Page 14: Implementing the Adapter Design Pattern

Results 1

The results should have come out to this

Page 15: Implementing the Adapter Design Pattern

Add The AdapterNow 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

Page 16: Implementing the Adapter Design Pattern

Round 2 Result

Page 17: Implementing the Adapter Design Pattern

The Not So ObviousArguments 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.

Page 18: Implementing the Adapter Design Pattern

ReviewWasn'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

Page 19: Implementing the Adapter Design Pattern

API ReferenceFor 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