- High level modules mg~n low level modules Gi Dci depend _vK‡e bv Abyiƒcfv‡e Low level Modules High level modules Gi Dci depend _vK‡e bv| Df‡q abstractions Gi Dci depend _vK‡e A_©vr G‡K Ac‡ii mv‡_ abstractions Gi gva¨‡g connected _vK‡e|
Real World Comparison
·
GKwU desktop
computer Gi wewfbœ parts †hgb RAM, hard disk, CD-ROM BZ¨vw` motherboard Gi mv‡_ loosely connected _v‡K|
·
hw` †Kvb part
bó n‡q hvq Z‡e Lye mn‡RB Bnv‡K byZb GKwUi
mv‡_ replace Kiv hvq|
·
hw` parts
mg~n G‡K Ac‡ii mv‡_ tightly coupled _vK‡Zv Z‡e †h †Kvb GKwU part bó n‡q †M‡j cy‡iv motherboard B cwieZ©b Ki‡Z n‡Zv hvnv Lye e¨vqeûj n‡Zv|
Identify Problem in Programming
·
Look at the following code.
public class CustomerBAL
{
public void Insert(Customer c)
{
try
{
//Insert logic
}
catch (Exception e)
{
FileLogger f = new FileLogger();
f.LogError(e);
}
}
}
public class FileLogger
{
public void LogError(Exception e)
{
//Log Error in a physical file
}
}
·
Dc‡iv³ code G
CustomerBAL
mivmwi
FileLogger
class Gi Dci dependent (FileLogger
class mKj exception mg~n‡K physical
file G log Ki‡e)
·
Now let’s assume tomorrow management decides to
log exceptions in the Event Viewer. Now what?
·
cieZ©x‡Z
hw` management decide K‡i Event
Viewer Gi g‡a¨ exception mg~n log Ki‡e
Z‡e existing code change Ki‡Z n‡e| †m‡ÿ‡Î new error / bug
introduce n‡Z cv‡i|
Solution that will not violate DIP
public interface ILogger
{
void LogError(Exception e);
}
public class FileLogger:ILogger
{
public void LogError(Exception e)
{
//Log Error in a physical file
}
}
public class EventViewerLogger :
ILogger
{
public void LogError(Exception e)
{
//Log Error in a physical file
}
}
public class CustomerBAL
{
private ILogger _objLogger;
public CustomerBAL(ILogger objLogger)
{
_objLogger = objLogger;
}
public void Insert(Customer c)
{
try
{
//Insert logic
}
catch (Exception e)
{
_objLogger.LogError(e);
}
}
}
- Dc‡iv³ example
G Avgiv †`wL †h client
abtraction Gi Dci depend i.e.
ILogger
‡K Bnv Øviv
derived †h †Kvb class Gi instance wnmv‡e set Kiv hvq|
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন