DIP Principle In Java
One of the most
important principle used in java that is OCP (Dependency Inversion Principle) .
At the time of designing software or any java based application there are so
many low level classes which are responsible for performing basic and primary operations and
services such as(disk access, network protocol) and on the other side there are
high level classes which are responsible to perform various business logic at
the client side as well as server side.So the DIP principle states that primary
hierarchy of class of structure should be start from high level module to the
low level module.
High Level Classes
|
Abstraction Layer
|
Low Level Classes
From Wikipedia :- In object-oriented design, the dependency inversion principle refers to a
specific form of decoupling software modules. When following
this principle, the conventional dependency relationships
established from high-level, policy-setting modules to low-level, dependency
modules are reversed, thus rendering high-level modules independent of the
low-level module implementation details. The principle states
A. High-level modules should not depend on
low-level modules. Both should depend on abstractions.
B. Abstractions should not depend on details.
Details should depend on abstractions.
This design principle inverts the way some
people may think about object-oriented programming, dictating that both high- and
low-level objects must depend on the same abstraction.
To understand the ocp principle first we need to understand the
voilation condition of this and then we refactor it according to ocpprinciple
says.So lets move on to the Code Area.
Dip Voilation
1.LogType Enumerator :
public interface
ILogStratergy {
public void
log(String messgae);
}
2.InComeTaxCalculator Class :
public class
IncomeTaxCalculator {
private LogType
logtype;
public
IncomeTaxCalculator(LogType logtype) {
this.logtype = logtype;
}
public double
calculateInterest(int income, int rate) {
int r = 0;
try {
r = income / rate;
} catch
(Exception e) {
// TODO:
handle exception
String errMessage = e.getMessage();
if (logtype ==
LogType.EMAIL) {
EmailLogger elogger = new EmailLogger();
elogger.emailLog(errMessage);
} else if (logtype ==
LogType.FILE) {
FileLogger flogger = new FileLogger();
flogger.logFile(errMessage);
} else if (logtype ==
LogType.REGISTRY) {
RegistryLogger rlogger = new RegistryLogger();
rlogger.logRegistry(errMessage);
}
}
return r;
}
}
3.EmailLogger Class :
public class
EmailLogger {
public void
emailLog(String message)
{
System.out.println("in
the email logger");
}
}
4. FileLogger Class :
public class
FileLogger {
public void
logFile(String messgae) {
System.out.println("in
the file logger");
}
}
5. RegistryLogger Class :
public class
RegistryLogger {
public void
logRegistry(String messgae) {
System.out.println("in
the log registry");
}
}
6. DipTest Class
public class DipTest
{
public static void
main(String[] args) {
IncomeTaxCalculator itc = new IncomeTaxCalculator(new FileLogger());
System.out.println(itc.calculateIntrest(10,
20));
}
}
OUTPUT :
20/10 = 2.0
Dip Refactor
1. ILogStratergy
Interface
public interface
ILogStratergy {
public void
log(String messgae);
}
2.IncomeTaxCalculator
Class :
public class
IncomeTaxCalculator {
ILogStratergy iLogStratergy;
public
IncomeTaxCalculator(ILogStratergy iLogStratergy) {
this.iLogStratergy = iLogStratergy;
}
public double
calculateIntrest(int rate, int amount) {
double tax = 0;
try {
tax = amount / rate;
} catch
(Exception exception) {
String errMessgae = exception.getMessage();
iLogStratergy.log(errMessgae);
}
return tax;
}
}
3.EmailLogger Class :
public class
EmailLogger implements
ILogStratergy {
@Override
public void
log(String messgae) {
// TODO
Auto-generated method stub
System.out.println("In
the email" + messgae);
}
}
4.FileLogger Class :
public class
FileLogger implements
ILogStratergy {
@Override
public void
log(String messgae) {
// TODO
Auto-generated method stub
System.out.println("in
the file logger" + messgae);
}
}
5.RegistryLogger Class :
public class
RegistryLogger implements
ILogStratergy {
@Override
public void
log(String messgae) {
// TODO
Auto-generated method stub
System.out.println("in
the registry logger" + messgae);
}
}
6.Test Class (DipTest)
public class DipTest
{
public static void
main(String[] args) {
IncomeTaxCalculator itc = new IncomeTaxCalculator(new FileLogger());
System.out.println(itc.calculateIntrest(10,
20));
}
}
7.OUTPUT :
OUTPUT :
200/10 = 20.0
No comments:
Post a Comment