·
IoC GKwU
abstract principle hvnv
KwZcq software architecture design describe K‡i hvnv‡Z GKwU systemGi flow of control procedural programming Gi flow of control ‡_‡K Dëv
nq|
An example
·
g‡bKwi
Avgv‡`i `ywU class Av‡Q X I Y.
·
KwZcq KvR
m¤úbœKivi Rb¨ Y class ‡K X
class Gi cÖ‡qvRb|
·
G‡ÿ‡Î
mvavibZ Avgiv X class G Avgiv Y
class Gi instance create Kwi
Z‡eB X class Y class
‡K use Ki‡Z
cvi‡e|
·
wKš‘ cÖkœ
n‡jv X class Gi wK really Rvbv `iKvi †h †m Y class use Ki‡Q?
·
Gi e`‡j hw` X
class ïaygvÎ Rv‡b †h †m Ggb wKQz use
Ki‡Q hvi behavior, methods,
properties etc Av‡Q Z‡e Zv wK h‡_ô bq?
·
Bnv Ki‡j
DcKvixZv wK?
·
DcKvixZv
n‡jv X class Y class Gi Dci dependent _v‡K bv|
·
wKfv‡e Avgiv
Zv Ki‡Z cvwi?
·
Y class Gi behavior
mg~n‡K GKwU abstract class /
Interface G define Kwi
Ges Yclass G D³ abstract
class / Interface implement Kwi Then X class G D³ abstract class / Interface Gi instance use Kwi Z‡eB Zv m¤¢e|
·
Dc‡iv³ diagram G Avgiv †`L‡Z cvB Y class interface I ‡K implement K‡i Ges X class I Gi GKwU
instance use K‡i|
·
G‡ÿ‡Î interesting
e¨vcvi n‡jv †h hw`I X class Yclass ‡K use K‡i wKš‘X class Zv m¤ú‡K© AeMZ _v‡K bv| Bnv ïaygvÎ Rv‡b †h Bnv hv use Ki‡Q Zv I interface ‡K implement K‡i‡Q|
·
I interface ‡K
implement Kvix classwU †h Y B n‡e Ggb ‡Kvb
K_v †bB A, B wKsev C ‡h‡Kvb class n‡Z cv‡i| G‡ÿ‡Î kZ© ïaygvÎ GKwUB Avi Zv n‡jv- class hvnvB nDK bv †Kb Zv Aek¨B interface I ‡K implement Ki‡e|
·
myZivs Avgiv
†`L‡Z cvB †h X class Y class Gi Dci dependent bq|
Benefits of Inversion of Control
- IoC use Gi d‡j X class Y class Gi Dci †Kvb dependency bvB| myZivs Y class G †Kvb change Avm‡j Dnvi †Kvb cÖfve X class G co‡e bv|
- Unit test Gi mgq codemg~n‡K mn‡RB isolateKiv hvq|
- SystemGi component I layermg~n Decouple K‡i d‡j modular system development easier nq|
- DifferentenvironmentG dependency implementationSwapK‡i|
- Recompile bv K‡i componentmg~n‡K replaceKiv hvq|
- ApplicationGi gva¨‡g resource mg~n sharingmechanismprovide K‡i|
- Alleviates a component from being responsible for managing it's dependencies
Applying Inversion of Control – A sample scenario
·
GLb Avgiv IoC Gi real
scenario ‡`L‡ev-
·
Avgv‡`i GKwU
class Av‡Q hvi name
OrderService . Bnv order mg~n processing Ki‡Z use nq|
·
GB OrderService
class G GKwU method Av‡Q hvi name AcceptOrder. GB method wU
Order object receive K‡i,validate
K‡i (check
K‡i Bnv business rule Gi mv‡_ valid wK bv) then hw` valid
nq Z‡e OrderDatabase class use K‡i database G save K‡i|
·
OrderService class Gi code wb¤œiƒc-
publicclassOrderService
{
publicvoidAcceptOrder(Order order)
{
//Domain logic such as validation
newOrderDatabase().SaveOrder(order);
}
}
·
Dc‡iv³ code
Abyhvqx SaveOrder method call Kivi Rb¨ OrderService class
GiAcceptOrder method Gi g‡a¨ OrderDatabase class Gi GKwU new instance create Kiv n‡q‡Q|
·
Gfv‡e OrderDatabase Gi
SaveOrder() method call Ki‡j OrderService class OrderDatabase class Gi Dci depend
n‡q co‡e| d‡j OrderDatabase
class G †Kvb change Avm‡j
Dnvi cÖfve OrderService class G co‡e Ges Unit
Testing complex n‡q hv‡e|
·
Inversion of Control (IoC) use K‡i Avgiv GB RwUjZv mg~n `~i Ki‡Z cvi‡ev|
·
GB scenario ‡Z Inversion of Control (IoC) use Ki‡Z n‡j Avgv‡`i‡K GKwU interface define Ki‡Z n‡e hvnv‡Z OrderDatabase class G used
method mg~‡ni Signature _vK‡e Ges OrderDatabase class ‡K GB interface Øviv inherit
Ki‡ev|
·
g‡bKwi D³ interface
Gi name
IOrderSaver. GB interface Øviv OrderDatabase class ‡K inherit
Ki‡ev |
·
Then
OrderService class G
D³ IOrderSaver interface Gi GKwU instance create Ki‡ev| d‡j OrderService class
Gi AcceptOrder() method OrderDatabase
class Gi
instance Gi e`‡j IOrderSaver interface Gi instance use Ki‡e|
·
d‡j OrderService
class OrderDatabaseclass m¤ú‡K© wKQzB Rvb‡e
bv|
·
Bnv ïaygvÎ IOrderSaver
interface Gi GKwU instance use Ki‡e (nq‡Zv OrderDatabase classwU IOrderSaver interface Gi A‡bK¸‡jv implementation Gi g‡a¨ GKwU implementation)
·
GLb Lye
mn‡RB OrderService class order mg~n‡K
OrderDatabase class use
K‡i Database G Save bv K‡i XmlOrderSaver
class use K‡i XML G Save Ki‡Z cvi‡ev| G‡ÿ‡Î OrderService classGi code G †Kvb change
Ki‡Z n‡e bv|
·
wKsev database
I XML G
GK‡Î save Ki‡Z cvi‡e|
·
GQvov Av‡iv variation Avbv hv‡e... ...
·
wKš‘ cÖkœ
n‡jv OrderService class wKfv‡e IOrderSaver
interface Gi instance cv‡e?
·
GRb¨ KwZcq way
Av‡Q| Zvi GKwU way
n‡”Q Dependency Injection.
Avgv‡`i cieZ©x Post G Avgiv Dependency Injection wb‡q Av‡jvPbv Ki‡ev|
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন