Clean Code



Download 3,58 Mb.
Pdf ko'rish
bet161/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   157   158   159   160   161   162   163   164   ...   384
Bog'liq
Clean Code

Chapter 11: Systems
We defined an interface 
Bank
, which will be 
wrapped 
by the proxy, and a 
Plain-Old
Java Object
(POJO), 
BankImpl
, that implements the business logic. (We will revisit POJOs
shortly.)
The Proxy API requires an 
InvocationHandler
object that it calls to implement any
Bank
method calls made to the proxy. Our 
BankProxyHandler
uses the Java reflection
API to map the generic method invocations to the corresponding methods in 
BankImpl
,
and so on.
There is a 
lot
of code here and it is relatively complicated, even for this simple case.
10
Using one of the byte-manipulation libraries is similarly challenging. This code “volume”
// “InvocationHandler” required by the proxy API.
public class BankProxyHandler implements InvocationHandler {
private Bank bank;
public BankHandler (Bank bank) {
this.bank = bank;
}
// Method defined in InvocationHandler
public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable {
String methodName = method.getName();
if (methodName.equals("getAccounts")) {
bank.setAccounts(getAccountsFromDatabase());
return bank.getAccounts();
} else if (methodName.equals("setAccounts")) {
bank.setAccounts((Collection) args[0]);
setAccountsToDatabase(bank.getAccounts());
return null;
} else {
...
}
}
// Lots of details here:
protected Collection getAccountsFromDatabase() { ... }
protected void setAccountsToDatabase(Collection accounts) { ... }
}
// Somewhere else...
Bank bank = (Bank) Proxy.newProxyInstance(
Bank.class.getClassLoader(), 
new Class[] { Bank.class },
new BankProxyHandler(new BankImpl()));
10. For more detailed examples of the Proxy API and examples of its use, see, for example, [Goetz].

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   157   158   159   160   161   162   163   164   ...   384




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