Sunday 18 June 2017

Solid Principle In Java

ISP Principle In Java

One of the most important principle used in java that is ISP (Interface Segregation Principle). While designing java application we need to take care about how we are going to design abstraction of module and classes in system.By using interface we can abstract the all classes and their subclasses and modules are make easier .But if we want to add another classes and modules to the existing application then it will be difficult to handle.So ISp principle overcome on this problem .

ISP principle states instead of making big/fat interface we can create many dummy interfaces which are appropriate to collection of methods og sub modules and sub classes.So user should not be forced to depend upon interface that they don’t use.

From Wikipedia :- The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them. Such shrunken interfaces are also called role interfaces. ISP is intended to keep a system decoupled and thus easier to refactor, change, and redeploy. ISP is one of the five SOLID principles of object-oriented design, similar to the High Cohesion Principle of GRASP.

To understand the ISP 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.

ISP Voilation
1.     IWorker Interface:
public interface IWorker {
   public void work();
   public void eat();
  
}

2. Developer  Class :

public class Developer implements IWorker {

     @Override
     public void work() {
           // TODO Auto-generated method stub
           System.out.println("Developer working");

     }

     @Override
     public void eat() {
           // TODO Auto-generated method stub
           System.out.println("developer eating");

     }

}
3.Robot  Class :
public class Robot implements IWorker {

     @Override
     public void work() {
           // TODO Auto-generated method stub
           System.out.println("robot is working");

     }

     @Override
     public void eat() {
           // TODO Auto-generated method stub
           throw new UnsupportedOperationException("cannot eat");

     }

}


4. IspTest Class
public class IspTest {
     public static void main(String[] args) {
           Developer developer = new Developer();
           Robot robot = new Robot();
           atCafetera(developer);
           atCafetera(robot);
           atWork(developer);
           atWork(robot);

     }

     private static void atCafetera(IWorker worker) {
           // TODO Auto-generated method stub
           System.out.println("in the cafeteria");
           worker.eat();

     }

     private static void atWork(IWorker iWorker) {
           // TODO Auto-generated method stub
           System.out.println("at the workstation");
           iWorker.work();

     }

}

OUTPUT :
in the cafeteria
developer eating
in the cafeteria
Exception in thread "main" java.lang.UnsupportedOperationException: cannot eat
       at isp.voilation.Robot.eat(Robot.java:15)
       at isp.voilation.test.IspTest.atCafetera(IspTest.java:21)
       at isp.voilation.test.IspTest.main(IspTest.java:12)

ISP Refactor

1.     IEatable  Interface

public interface IEatable {
     public void startEat();

     public void endEat();

}
2.     IWorkable Interface:
public interface IWorkable {
    
     public void startWork();
    
     public void endWork();

}
3.     IWorkableEtable  Interface :
public interface IWorkableIEatable extends IWorkable, IEatable {

}
4.     Worker  Class :

public class Worker implements IWorkable, IEatable {

     @Override
     public void startWork() {
           // TODO Auto-generated method stub
           System.out.println("worker start work");

     }

     @Override
     public void endWork() {
           // TODO Auto-generated method stub
           System.out.println("worker end work");

     }

     @Override
     public void startEat() {
           // TODO Auto-generated method stub
           System.out.println("worker start eating");

     }

     @Override
     public void endEat() {
           // TODO Auto-generated method stub
           System.out.println("worker end eating");

     }

}


5.     Robot   Class :

public class Robot implements IWorkable {

     @Override
     public void startWork() {
           // TODO Auto-generated method stub
           System.out.println("robot start working");

     }

     @Override
     public void endWork() {
           // TODO Auto-generated method stub
           System.out.println("robot end working");

     }

}





6.Test Class (IspRefactor Test)

public class IspRefactorTest {

     public static void main(String[] args) {

           // TODO Auto-generated method stub
           Worker worker = new Worker();
           Robot robot = new Robot();
           atCafetera(worker, worker);
           atWork(robot);

     }

     private static void atCafetera(IWorkable worker, IEatable eat) {
           // TODO Auto-generated method stub
           System.out.println("in the cafeteria");
           worker.startWork();
           worker.endWork();
           eat.startEat();
           eat.endEat();

     }

     private static void atWork(IWorkable iWorker) {
           // TODO Auto-generated method stub
           System.out.println("at the workstation");
           iWorker.startWork();
           iWorker.endWork();

     }
}


7.OUTPUT :


in the cafeteria
worker start work
worker end work
worker start eating
worker end eating
at the workstation
robot start working
robot end working
























No comments:

Post a Comment

Spring-App

Application In Spring Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developi...