The Clean Architecture in php


class InvoiceDeliveryStrategyFactory



Download 2,26 Mb.
Pdf ko'rish
bet57/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   53   54   55   56   57   58   59   60   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

class
InvoiceDeliveryStrategyFactory
{
public function
create
(Customer
$customer
) {
switch
(
$customer
->
getDeliveryMethod
()) {
case
'email'
:
return new
EmailDeliveryStrategy();
break
;
case
'print'
:
default
:
return new
PrintDeliveryStrategy();
break
;
}
}
}
The actual logic within the
InvoiceDeliveryStrategyFactory
class isn’t any different than what
we had in the
invoiceCustomers
method, but now it’s reusable (if that were even necessary in
this case), and it’s independently testable. It’s a great use of a factory.


Design Patterns, A Primer
30
This simple code example now shows the usage of repositories, factories, strategies, and, if you
followed our tip, maybe even adapters!
public function
invoiceCustomers
(
array
$customers
) {
foreach
(
$customers
as
$customer
) {
$invoice
=
$this
->
invoiceFactory
->
create
(
$customer
,
$this
->
orderRepository
->
getByCustomer
(
$customer
)
);
$strategy
=
$this
->
deliveryMethodFactory
->
create
(
$customer
);
$strategy
->
send
(
$invoice
);
}
}
Learning More Design Patterns
Design patterns help us write clean, understandable, concise code that makes refactoring, testing,
and maintainability possible. It also gives us a common language to use when discussing ideas
with other developers.
This chapter has only scratched the surface of the design patterns presented, and only presented
a handful of the design patterns in the wild. Even beyond those introduced by the Gang of Four
book, others have defined their own design patterns, and some of those have gained traction.
I highly recommend you pick up at least one of the two books mentioned in the beginning of
this chapter:
1.
Design Patterns: Elements of Reusable Object-Oriented Software¹¹
2.
Head First Design Patterns¹²
Design patterns are a great, tried-and-true way to solve common coding problems. We’ll use
these patterns throughout the rest of this book.
¹¹
http://amzn.to/XO8DB4
¹²
http://www.headfirstlabs.com/books/hfdp/



Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   53   54   55   56   57   58   59   60   ...   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