Skip to document

Design Patterns The Adapter Pattern

Design Patterns The Adapter Pattern
Course

Human Computer Interaction (HCI 308)

17 Documents
Students shared 17 documents in this course
Academic year: 2019/2020
Uploaded by:
0followers
8Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

QUICK LINKS

ENVATO TUTS+

About Envato Tuts+ Terms of Use Advertise

JOIN OUR COMMUNITY

Teach at Envato Tuts+ Translate for Envato Tuts+ Forums

HELP

FAQ Help Center

Envato Elements Envato Studio Community Forum Help Subscribe Sign In

How-To Tutorials  Courses  eBooks  Pricing Search Envato Tuts+ 

Advertisement

CODE > DESIGN PATTERNS

Advertisement

Design Patterns: The

Adapter Pattern

LOOKING FOR SOMETHING TO HELP KICK START YOUR NEXT PROJECT?

Envato Market has a range of items for sale to help get you started.

36 Comments Tuts+ Hub  Yusto
⤤ Share Sort by Best

Tuts+ Hub requires you to verify your email address before posting. Send verification email to omondiy@gmail

×

Join the discussion...
  • Reply •

ehelguero • 4 years ago Really nice article! I ́m going to try it. 29 △ ▽

  • Reply •

Marcos Borunda • 4 years ago What I find amazing about Design Patterns is how stuff that you might already figure out by yourself, while coding, actually is a thing and has a name. Knowing the name for a design pattern and a structured definition make things clear and easy to express with your team. 20 △ ▽

  • Reply •

techniczny > Marcos Borunda • 4 years ago What you really should find, is that if you knew about design patterns before when learning programming, you wouldn't need to figure it out later by yourself. 3 △ ▽

  • Reply •

Mark Fox • 4 years ago Nice example. Now, I will be a pedantic moron: class-names that aren't CamelCase look sloppy and weird. 6 △ ▽

  • Reply •

Nathan Daly • 4 years ago Great article :) 3 △ ▽

  • Reply •

Xin Zheng • 4 years ago Nice article! Thanks!!! 3 △ ▽

  • Reply •

Mikle Kozachoc • 3 years ago What about, if we alredy have used sendPayment() and in same time PayPal chenged name of method? 2 △ ▽

  • Reply •

Levent Atan > Mikle Kozachoc • 10 months ago I think that would be a perfect case where adapter pattern can be applied. I mean I should be using a payment solution already and find out another (new) one (instead of a replacement) which doesn't comply with the existing interface. △ ▽

  • Reply •

Avinash > Mikle Kozachoc • 3 years ago In that case we need to replace new method name in pay method of our adapter class only.

public function pay($amount) { // Old Line $this->paypal->sendPayment($amount); // New line with new method name $this->paypal->sendPayment2($amount); }

Hope this is clear △ ▽

  • Reply •

UseMeAbuseMe4Ever > Avinash • 6 months ago I think technically, you would need to change it in two places right? In the Adapter pay method AND in the concrete class. △ ▽

  • Reply •

Mikle Kozachoc > Avinash • 3 years ago ok, but if we did not implement adapter before. △ ▽

  • Reply •

Avinash > Mikle Kozachoc • 3 years ago In that case you will need to change at all places where you have used that method. I have explained the same in problem section of this article. Hope that's clear. △ ▽

  • Reply •

Michael Lawson • 4 years ago Hooray for underutilized design patterns! 2 △ ▽

  • Reply •

Hari krishna • 4 years ago What is the use of social adapter interface? What happens when we implement a class without interface? 1 △ ▽

  • Reply •

Wesam Alalem > Hari krishna • 3 years ago Program to interface, not implementation, check the first answer for SO question regarding this topic: stackoverflow/qu... △ ▽

  • Reply •

Bruce Lampson • 4 years ago Good job Avinash Zala! Very comprehensible. I wrote a simple presentation few weeks ago explaining this pattern. In my example i use a global adapter that should be compatible with many mail clients via dependency injection. I would like to share this example with all of you. smsnica/reve... (Simply press your down arrow to checkout the implementation) 1 △ ▽

  • Reply •

Avinash > Bruce Lampson • 4 years ago Thanks for sharing... :) 1 △ ▽

  • Reply •

D3F > Bruce Lampson • 3 years ago Hi Bruce,

Nice presentation, however you are missing a semicolon in smsnica/reve... at line 2 ;)

En you say $clint i think this must be $client, but you are using it consistent so nothing breaks.

But nice presentation, very straightforward. △ ▽

  • Reply •

Bruce Lampson > D3F • 3 years ago Hey there D3F, Thanks for pointing that out! △ ▽

  • Reply •

Julius Koronci > Bruce Lampson • 4 years ago Nice presentation and very self explanatory :) △ ▽

  • Reply •

Nadeen Nilanka • 4 years ago Understood, great article! 1 △ ▽

  • Reply •

Paweł P. • 4 years ago Patterns – like always in "price". 1 △ ▽

  • Reply •

Shahzaib Khan • 4 months ago Error in code :- While changing "$this->paypal->sendPayment($amount);" to "$this->paypal->payAmount($amount);"

We also need to change "public function sendPayment($amount) {" to "public function payAmount($amount) {"

also in main "PayPal" class. △ ▽

  • Reply •

Fun Zone • 8 months ago very helpful article. △ ▽

  • Reply •

James Watadza • 2 years ago Good tutorial again. Ignore the morons who have never written a single tutorial but spend hours criticizing and shooting down the work of those who cared enough to take the time to teach others. △ ▽

  • Reply •

Mukesh Singh • 3 years ago tutorial is amazing. please add more pattern △ ▽

  • Reply •

Vhin Manansala • 3 years ago Great article, but I have a question.

What's the difference of using interface when using

class paypalAdapter{ private $paypal; public function __construct(PayPal $paypal) { $this->paypal = $paypal; }

public function pay($amount) { $this->paypal->payAmount($amount); } }

Seems to work fine?? △ ▽

  • Reply •

Mohamed Wael • 3 years ago Very good article, thank you. △ ▽

  • Reply •

Avinash > Mohamed Wael • 3 years ago Glad you liked this. △ ▽

  • Reply •

manhee • 3 years ago It'd be better to rename "interface paymentAdapter" to " interface paymentInterface". △ ▽

  • Reply •

gundholmu • 3 years ago Nice article and easy to understand. But I have some question about this pattern.

Why we need to create an interface class (paymentAdapter)? why we not just directly create an instantiate of PayPal class within the construct class of paypalAdapter? △ ▽

  • Reply •

Ankush Thakur > gundholmu • 3 years ago Because by making all classes implement an interface, we can be sure all of them use the same function name for payment. If you were directly wrapping classes, you might mistakenly name one of the adapter functions as pay() and the other as payNow(), causing confusion to yourself. △ ▽

  • Reply •

Yanwar • 3 years ago what adapter can be combined with the facade? △ ▽

  • Reply •

Elfan Nofiari • 4 years ago One umbrella term for this would be single source of authority, or DRY, Don't Repeat Yourself. △ ▽

  • Reply •

amorbytes • 4 years ago Very good explanation design patterns and how we can use it for solve our daily problem. △ ▽

  • Reply •

tormahiri • 4 years ago thanks article and simple △ ▽

✉ Subscribe dAdd Disqus to your site  Disqus' Privacy Policy

 Recommend 10

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Important Update

####### When you log in with Disqus, we process personal data to facilitate your

####### authentication and posting of comments. We also store the comments you

####### post and those comments are immediately viewable and searchable by

####### anyone around the world.

####### I agree to Disqus' Terms of Service

####### I agree to Disqus' processing of email and IP address, and the use of

####### cookies, to facilitate my authentication and posting of comments,

####### explained further in the Privacy Policy

Proceed

by Avinash Zala 8 Nov 2014 Difficulty:Intermediate Length:Short Languages: English

Design Patterns PHP OOP Programming Fundamentals

The Adapter Pattern Web Development

This post is part of a series called Design Patterns in PHP.

 Design Patterns: The Facade Pattern

 Design Patterns: The Decorator Pattern

In the last article, we looked at how the facade design pattern can be
employed to simplify the employment of any large and complex system using
only a simple facade class.
In this article, we will continue our discussion on design patterns by taking a
look at the adapter design pattern. This particular pattern can be used when
your code is dependent on some external API, or any other class that is prone
to change frequently. This pattern falls under the category of "structural
patterns" because it teaches us how our code and our classes should be
structured in order to manage and/or extend them easily.
Again, I'd like to reiterate that design patterns have nothing new over
traditional classes. Instead, they show us a better way to structure our
classes, handle their behavior, and manage their creation.

The Problem

In the above code, you can see that we are utilizing a PayPal class to simply
pay the amount. Here, we are directly creating the object of the PayPal class
and paying via PayPal. You have this code scattered in multiple places. So we
can see that the code is using the $paypal->sendPayment('amount
here'); method to pay.
Some time ago, PayPal changed the API method name from sendPayment to
payAmount. This should clearly indicate a problem for those of us who have
been using the sendPayment method. Specifically, we need to change
all sendPayment method calls to payAmount. Imagine the amount of code we
need to change and the time we need to spend on testing each of the features
once again.

The Solution

One solution to this problem is to use the adapter design pattern.
According to Wikipedia:

In software engineering, the adapter pattern is a

software design pattern that allows the interface of

an existing class to be used from another interface.

It is often used to make existing classes work with

others without modifying their source code.

In this case, we should create one wrapper interface which makes this
possible. We will not make any changes in the external class library because
we do not have control over it and it may change any time.
Let's dig into the code now, which shows the adapter pattern in action:
Study the code above and you should be able to tell that we have not
introduced any changes into the main PayPal class. Instead we have created
one interface for our payment adapter and one adapter class for PayPal.
And so afterward we have made the object of the adapter class instead of the
main PayPal class. While creating an object of adapter class we will pass the
object of the main PayPal class as an argument, so that adapter class can
have a reference to the main class and it can call the required methods of the
main PayPal class.
Let's find out how we can utilize this method directly:
Now imagine PayPal changes its method name from sendPayment to
payAmount. Then we just need to make changes in paypalAdapter. Just have a
look at the revised adapter code, which has just one change.
So just one change and we are there.

Adding a New Adapter

At this point, we've seen how we can use the adapter design patten to
overcome the aforementioned scenarios. Now, it's very easy to add a new
class dependent on the existing adapter. Let's say the MoneyBooker API is
there for payment.
Then instead of using the MoneyBooker class directly, we should be applying
the same adapter pattern we just used for PayPal.
As you can see, the same principles apply. You define a method that's
available to third-party classes and then, if a dependency changes its API, you
simply change the dependent class without exposing its external interface.

Conclusion

A great application is constantly hooked into other libraries and APIs, so I
would propose that we implement the adapter method, so that we do not
experience any trouble when a third-party API or library changes its code
base.
I have tried my best to provide an elementary and yet useful example to
demonstrate the adapter design pattern, but if you have additional comments
or questions, please don't hesitate to add them in the feed below.

Advertisement

Avinash Zala

Avinash is a coder with over six years of experience in web development. With a strong focus on quality and usability, he is interested in delivering cutting edge applications. He is ready to work on responsive applications targeted to various devices. He graduated with a bachelor degree in Information Technology. If you'd like to stay up to date on his activities, refer to his blog or follow him on Twitter and Facebook.

XpertDevelopers

FEED LIKE FOLLOW FOLLOW

Weekly email summary

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

Email Address

Update me weekly

Advertisement

Translations

Envato Tuts+ tutorials are translated

into other languages by our community

members—you can be involved too!

Translate this post

Powered by



01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

<?php class PayPal {

public function __construct() { // Your Code here // }

public function sendPayment($amount) { // Paying via Paypal // echo "Paying via PayPal: ". $amount; } }

$paypal = new PayPal(); $paypal->sendPayment('2629');

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

// Concrete Implementation of PayPal Class class PayPal {

public function __construct() { // Your Code here // }

public function sendPayment($amount) { // Paying via Paypal // echo "Paying via PayPal: ". $amount; } }

// Simple Interface for each Adapter we create interface paymentAdapter { public function pay($amount); }

class paypalAdapter implements paymentAdapter {

private $paypal;

public function __construct(PayPal $paypal) { $this->paypal = $paypal; }

public function pay($amount) { $this->paypal->sendPayment($amount); } }

1 2 3

// Client Code $paypal = new paypalAdapter(new PayPal()); $paypal->pay('2629');

01 02 03 04 05 06 07 08 09 10 11 12

class paypalAdapter implements paymentAdapter {

private $paypal;

public function __construct(PayPal $paypal) { $this->paypal = $paypal; }

public function pay($amount) { $this->paypal->payAmount($amount); } }

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

// Concrete Implementation of MoneyBooker Class class MoneyBooker {

public function __construct() { // Your Code here // }

public function doPayment($amount) { // Paying via MoneyBooker // echo "Paying via MoneyBooker: ". $amount; } }

// MoneyBooker Adapter class moneybookerAdapter implements paymentAdapter {

private $moneybooker;

public function __construct(MoneyBooker $moneybooker) { $this->moneybooker = $moneybooker; }

public function pay($amount) { $this->moneybooker->doPayment($amount); } }

// Client Code $moneybooker = new moneybookerAdapter(new MoneyBooker()); $moneybooker->pay('2629');

WP Translation plugin

Compatible with all Theme & Plugins (incl. WooCommerce). SEO optimized. Join 20,000 users.

   

WordPress Plugins

From $

PHP Scripts

From $

JavaScript

From $

Unlimited Downloads
From $16/month

Get access to over 400,000 creative assets on Envato Elements.

Over 9 Million Digital Assets

Everything you need for your next creative project.

Hire a Freelancer

Choose from 2,000 professionals ready to do the work for you.

  • Explore popular categories
26,

Tutorials

1,

Courses

26,

Translations

© 2018 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.

Envato Our products Careers Sitemap Follow Envato Tuts+

Advertisement

WATCHWATCH ANYANY

COURSE NOWCOURSE NOW

Subscribe to Access 

Was this document helpful?

Design Patterns The Adapter Pattern

Course: Human Computer Interaction (HCI 308)

17 Documents
Students shared 17 documents in this course
Was this document helpful?
QUICK LINKS
ENVATO TUTS+
About Envato Tuts+
Terms of Use
Advertise
JOIN OUR COMMUNITY
Teach at Envato Tuts+
Translate for Envato Tuts+
Forums
HELP
FAQ
Help Center
Envato Elements Envato Studio Community Forum Help Subscribe Sign In
PricingHow-To Tutorials Courses eBooks
Search Envato Tuts+
Advertisement
CODE >DESIGN PATTERNS
Advertisement
Design Patterns: The
Adapter Pattern
LOOKING FOR SOMETHING TO HELP KICK START YOUR NEXT PROJECT?
Envato Market has a range of items for sale to help get you started.
36 Comments Tuts+ Hub
Yusto
Share
Sort by Best
Tuts+ Hub requires you to verify your email address before posting. Send verification email
to omondiy@gmail.com
×
Join the discussion…
Reply
ehelguero 4 years ago
Really nice article! I´m going to try it.
29
Reply
Marcos Borunda 4 years ago
What I find amazing about Design Patterns is how stuff that you might already figure
out by yourself, while coding, actually is a thing and has a name. Knowing the name
for a design pattern and a structured definition make things clear and easy to express
with your team.
20
3
Reply
Mark Fox 4 years ago
Nice example. Now, I will be a pedantic moron: class-names that aren't CamelCase
look sloppy and weird.
6
Reply
Nathan Daly 4 years ago
Great article :)
3
Reply
Xin Zheng 4 years ago
Nice article! Thanks!!!
3
Reply
Mikle Kozachoc 3 years ago
What about, if we alredy have used sendPayment() and in same time PayPal
chenged name of method?
2
public function pay($amount) {
// Old Line
$this->paypal->sendPayment($amount);
// New line with new method name
$this->paypal->sendPayment2($amount);
}
Reply
UseMeAbuseMe4Ever 6 months ago> Avinash
I think technically, you would need to change it in two places right? In
the Adapter pay method AND in the concrete class.
Reply
Mikle Kozachoc 3 years ago> Avinash
ok, but if we did not implement adapter before.
Reply
Avinash 3 years ago> Mikle Kozachoc
In that case you will need to change at all places where you
have used that method. I have explained the same in problem
section of this article. Hope that's clear.
Reply
Michael Lawson 4 years ago
Hooray for underutilized design patterns!
2
Reply
Hari krishna 4 years ago
What is the use of social adapter interface? What happens when we implement a
class without interface?
1
Reply
Bruce Lampson 4 years ago
Good job Avinash Zala! Very comprehensible. I wrote a simple presentation few
weeks ago explaining this pattern. In my example i use a global adapter that should
be compatible with many mail clients via dependency injection. I would like to share
this example with all of you. http://www.smsnica.com/reve... (Simply press your down
arrow to checkout the implementation)
1
1
Reply
Bruce Lampson 3 years ago> D3F
Hey there D3F, Thanks for pointing that out!
Reply
Nadeen Nilanka 4 years ago
Understood, great article!
1
Reply
Paweł P. 4 years ago
Patterns like always in "price".
1
Reply
Shahzaib Khan 4 months ago
Error in code :- While changing
"$this->paypal->sendPayment($amount);" to
"$this->paypal->payAmount($amount);"
We also need to change
"public function sendPayment($amount) {" to
"public function payAmount($amount) {"
also in main "PayPal" class.
Reply
Fun Zone 8 months ago
very helpful article.
Reply
James Watadza 2 years ago
Good tutorial again. Ignore the morons who have never written a single tutorial but
spend hours criticizing and shooting down the work of those who cared enough to
take the time to teach others.
Reply
Mukesh Singh 3 years ago
tutorial is amazing. please add more pattern
Reply
Vhin Manansala 3 years ago
Great article, but I have a question.
What's the difference of using interface when using
class paypalAdapter{
private $paypal;
public function __construct(PayPal $paypal) {
$this->paypal = $paypal;
}
public function pay($amount) {
$this->paypal->payAmount($amount);
}
}
Seems to work fine??
Reply
Mohamed Wael 3 years ago
Very good article, thank you.
Reply
manhee 3 years ago
It'd be better to rename "interface paymentAdapter" to "
interface paymentInterface".
Reply
gundholmu 3 years ago
Nice article and easy to understand. But I have some question about this pattern.
Why we need to create an interface class (paymentAdapter)? why we not just directly
create an instantiate of PayPal class within the construct class of paypalAdapter?
Reply
Yanwar 3 years ago
what adapter can be combined with the facade?
Reply
Elfan Nofiari 4 years ago
One umbrella term for this would be single source of authority, or DRY, Don't Repeat
Yourself.
Reply
amorbytes 4 years ago
Very good explanation design patterns and how we can use it for solve our daily
problem.
Reply
tormahiri 4 years ago
thanks.great article and simple
Subscribe
Add Disqus to your site
dDisqus' Privacy Policy
🔒
Recommend
10
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Important Update
When you log in with Disqus, we process personal data to facilitate your
authentication and posting of comments. We also store the comments you
post and those comments are immediately viewable and searchable by
anyone around the world.
I agree to Disqus' Terms of Service
I agree to Disqus' processing of email and IP address, and the use of
cookies, to facilitate my authentication and posting of comments,
explained further in the Privacy Policy
Proceed
by Avinash Zala 8 Nov 2014 Difficulty: Intermediate Length: Short Languages:
English
Design Patterns PHP OOP Programming Fundamentals
The Adapter Pattern Web Development
This post is part of a series called Design Patterns in PHP.
Design Patterns: The Facade Pattern
Design Patterns: The Decorator Pattern
In the last article, we looked at how the facade design pattern can be
employed to simplify the employment of any large and complex system using
only a simple facade class.
In this article, we will continue our discussion on design patterns by taking a
look at the adapter design pattern. This particular pattern can be used when
your code is dependent on some external API, or any other class that is prone
to change frequently. This pattern falls under the category of "structural
patterns" because it teaches us how our code and our classes should be
structured in order to manage and/or extend them easily.
Again, I'd like to reiterate that design patterns have nothing new over
traditional classes. Instead, they show us a better way to structure our
classes, handle their behavior, and manage their creation.
The Problem
In the above code, you can see that we are utilizing a PayPal class to simply
pay the amount. Here, we are directly creating the object of the PayPal class
and paying via PayPal. You have this code scattered in multiple places. So we
can see that the code is using the $paypal->sendPayment('amount
here'); method to pay.
Some time ago, PayPal changed the API method name from sendPayment to
payAmount . This should clearly indicate a problem for those of us who have
been using the sendPayment method. Specifically, we need to change
all sendPayment method callsto payAmount . Imagine the amount of code we
need to change and the time we need to spend on testing each of the features
once again.
The Solution
One solution to this problem is to use the adapter design pattern.
According to Wikipedia:
In software engineering, the adapter pattern is a
software design pattern that allows the interface of
an existing class to be used from another interface.
It is often used to make existing classes work with
others without modifying their source code.
In this case, we should create one wrapper interface which makes this
possible. We will not make any changes in the external class library because
we do not have control over it and it may change any time.
Let's dig into the code now, which shows the adapter pattern in action:
Study the code above and you should be able to tell that we have not
introduced any changes into the main PayPal class. Instead we have created
one interface for our payment adapter and one adapter class for PayPal.
And so afterward we have made the object of the adapter class instead of the
main PayPal class.While creating an object of adapter class we will pass the
object of the main PayPal class as an argument, so that adapter class can
have a reference to the main class and it can call the required methods of the
main PayPal class.
Let's find out how we can utilize this method directly:
Now imagine PayPal changes its method name from sendPayment to
payAmount. Then we just need to make changes in paypalAdapter . Just have a
look at the revised adapter code, which has just one change.
So just one change and we are there.
Adding a New Adapter
At this point, we've seen how we can use the adapter design patten to
overcome the aforementioned scenarios. Now, it's very easy to add a new
class dependent on the existing adapter. Let's say the MoneyBooker API is
there for payment.
Then instead of using the MoneyBooker class directly, we should be applying
the same adapter pattern we just used for PayPal.
As you can see, the same principles apply. You define a method that's
available to third-party classes and then, if a dependency changes its API, you
simply change the dependent class without exposing its external interface.
Conclusion
A great application is constantly hooked into other libraries and APIs, so I
would propose that we implement the adapter method, so that we do not
experience any trouble when a third-party API or library changes its code
base.
I have tried my best to provide an elementary and yet useful example to
demonstrate the adapter design pattern, but if you have additional comments
or questions, please don't hesitate to add them in the feed below.
Advertisement
Avinash Zala
Avinash is a coder with over six years of experience in web
development. With a strong focus on quality and usability, he is
interested in delivering cutting edge applications. He is ready to work
on responsive applications targeted to various devices. He graduated
with a bachelor degree in Information Technology. If you'd like to stay
up to date on his activities, refer to his blog or follow him on Twitter
and Facebook.
XpertDevelopers
FEED LIKE FOLLOW FOLLOW
Weekly email summary
Subscribe below and we’ll send you a
weekly email summary of all new Code
tutorials. Never miss out on learning
about the next big thing.
Email Address
Update me weekly
Advertisement
Translations
Envato Tuts+ tutorials are translated
into other languages by our community
members—you can be involved too!
Translate this post
Powered by
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<?php
class PayPal {
public function __construct() {
// Your Code here //
}
public function sendPayment($amount) {
// Paying via Paypal //
echo "Paying via PayPal: ". $amount;
}
}
$paypal = new PayPal();
$paypal->sendPayment('2629');
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Concrete Implementation of PayPal Class
class PayPal {
public function __construct() {
// Your Code here //
}
public function sendPayment($amount) {
// Paying via Paypal //
echo "Paying via PayPal: ". $amount;
}
}
// Simple Interface for each Adapter we create
interface paymentAdapter {
public function pay($amount);
}
class paypalAdapter implements paymentAdapter {
private $paypal;
public function __construct(PayPal $paypal) {
$this->paypal = $paypal;
}
public function pay($amount) {
$this->paypal->sendPayment($amount);
}
}
1
2
3
// Client Code
$paypal = new paypalAdapter(new PayPal());
$paypal->pay('2629');
01
02
03
04
05
06
07
08
09
10
11
12
class paypalAdapter implements paymentAdapter {
private $paypal;
public function __construct(PayPal $paypal) {
$this->paypal = $paypal;
}
public function pay($amount) {
$this->paypal->payAmount($amount);
}
}
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Concrete Implementation of MoneyBooker Class
class MoneyBooker {
public function __construct() {
// Your Code here //
}
public function doPayment($amount) {
// Paying via MoneyBooker //
echo "Paying via MoneyBooker: ". $amount;
}
}
// MoneyBooker Adapter
class moneybookerAdapter implements paymentAdapter {
private $moneybooker;
public function __construct(MoneyBooker $moneybooker) {
$this->moneybooker = $moneybooker;
}
public function pay($amount) {
$this->moneybooker->doPayment($amount);
}
}
// Client Code
$moneybooker = new moneybookerAdapter(new MoneyBooker());
$moneybooker->pay('2629');
WP Translation plugin
Compatible with all Theme & Plugins (incl. WooCommerce). SEO optimized. Join
20,000 users.
WordPress Plugins
From $5
PHP Scripts
From $5
JavaScript
From $3
Unlimited Downloads
From $16.50/month
Get access to over 400,000 creative
assets on Envato Elements.
Over 9 Million Digital Assets
Everything you need for your next
creative project.
Hire a Freelancer
Choose from 2,000 professionals
ready to do the work for you.
- Explore popular categories
26,240
Tutorials
1,147
Courses
26,097
Translations
© 2018 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.
Envato.com Our products Careers Sitemap
Follow Envato Tuts+
Advertisement
WATCH
WATCH
ANY
ANY
COURSE NOW
COURSE NOW
Subscribe to Access