The Clean Architecture in php



Download 2,26 Mb.
Pdf ko'rish
bet45/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   41   42   43   44   45   46   47   48   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

class
CustomerFactory
{
protected
$accountManagerRepo
;
public function
__construct
(AccountManagerRepository
$repo
) {
$this
->
accountManagerRepo
=
$repo
;
}
public function
createCustomer
(
$name
) {
$customer
=
new
Customer();
$customer
->
setName
(
$name
);
$customer
->
setCreditLimit
(
0
);
$customer
->
setStatus
(
'pending'
);
$customer
->
setAccountManager
(
$this
->
accountManagerRepo
->
getRandom
()
);
return
$customer
;
}
}
The business logic for creating new
Customer
objects is encapsulated within the
CustomerFactory
object.
There are several benefits to using factories:
1. Reusable code. Any place you have to create an object, the same logic is applied. No
duplicate logic.
2. Testable code. Factories make creational logic easy to test. If this code were directly within
some other class somewhere, it wouldn’t be possible to test in isolation.
3. Easy to change. If the logic ever changes, the change only needs to occur in one place.
Static Factories
Often, you’ll see factories in the wild setup as “static classes” (i.e.: classes with only static
methods):


Design Patterns, A Primer
20
class
CustomerFactory
{
public static function
createCustomer
(
$name
) {
$customer
=
new
Customer();
$customer
->
setName
(
$name
);
$customer
->
setCreditLimit
(
0
);
$customer
->
setStatus
(
'pending'
);
return
$customer
;
}
}
Which would be called simply:
$customer
=
CustomerFactory
::
createCustomer
(
'ACME Corp'
);
At first, this seems much cleaner and easier to use than instantiating the factory every time
we need it. However, our first
CustomerFactory
had additional dependencies it needed to create
Customer
objects, namely the
AccountRepository
. We could pass this dependency to the method
each time we call it, but that would be a mess to cleanup if we ever changed the name of
AccountRepository
or switched to another paradigm for data management.
I’ll leave it up to you whether you want to use static factories or not. For simple factories, there’s
probably no negatives to doing so. For involved factories, especially those with dependencies, it
can lead to some pretty smelly code.

Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   41   42   43   44   45   46   47   48   ...   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