The Clean Architecture in php


interface GeocoderInterface { public function



Download 2,26 Mb.
Pdf ko'rish
bet88/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   84   85   86   87   88   89   90   91   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

interface
GeocoderInterface {
public function
geocode
(
$address
);
}
Then, we’ll go ahead and make our controller depend only upon this interface:
class
AddressController
extends
AbstractController {
protected
$geocoder
;
public function
__construct
(GeocoderInterface
$geocoder
) {
$this
->
geocoder
=
$geocoder
;
}
public function
validateAddressAction
() {
$address
=
$this
->
vars
()
->
fromPost
(
'address'
);
$isValid
=
$this
->
geocoder
->
geocode
(
$address
)
!==
false
;
}
}
Finally, we’ll create an adapter to wrap
BillsGeocoder
and make it conform to our
GeocoderInterface
that is required by our
AddressController
class:
class
BillsGeocoderAdapter
implements
GeocoderInterface {
protected
$geocoder
;
public function
__construct
(BillsGeocoder
$geocoder
) {
$this
->
geocoder
=
$geocoder
;
}
public function
geocode
(
$address
) {
return
$this
->
geocoder
->
geocode
(
$address
);
}
}
In our
geocode()
method, we’re simply passing off the processing to our instance of
BillsGeocoder
,
which we take through the constructor.
We can use dependency injection to inject an instance of
BillsGeocoderAdapter
into our
AddressController
, which allows us to use a third party library but makes sure it conforms
to the interface we need.
How does this help?
This method of using adapters with third party libraries allows us to remain decoupled and free
from dependence on those third party libraries. It allows us to freely swap out those dependencies


Abstracting with Adapters
59
without having to rewrite any code that uses them, and it allows us to easily test our application
and its use of those dependences without actually having to test those dependencies ourselves.
We only have to test that we’re properly utilizing them.
We’ll later discuss the importance of External Agency Independence when we discuss The Clean
Architecture.



Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   84   85   86   87   88   89   90   91   ...   179




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish