Thursday, November 24, 2011

Dependency Inversion Design Principle


If high level classes are not working directly with low level classes and they are using interfaces as an abstract layer then we will implement this principle with creational design pattern.

Bad Scenario :
Robot
public class Robot {
      public void work (){
            System.out.println("Robot's work.");
      }
}
Worker
public class Worker {
      public void work(){
            System.out.println("Worker's Work.");
      }
}
WorkManager
public class WorkManager {
      public void manage(Worker worker){
            worker.work();
      }
     
      public void manager(Robot robot){
            robot.work();
      }
}

Good Scenario :
IWork
public interface IWork {
      public void work();
}
Robot
public class Robot implements IWork {
      public void work(){
            System.out.println("Robot's work.");
      }
}
Worker
public class Worker implements IWork {
      public void work(){
            System.out.println("Worker's Work.");
      }
}
WorkManager
public class WorkManager {
      public void manage(IWork work){
            work.work();
      }
}

1 comment:

  1. Here, classes are categorized like family and it is IWorker family.

    ReplyDelete