Interface Segregation Principle- Program to an interface

前端 未结 6 677
无人及你
无人及你 2020-12-01 01:05

I was reading about SOLID and other design principles. I thought ISP was the same as \"Program to an interface, not an implementation\". But it looks like these are differen

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 01:42

    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");
      
           }
      
      }
      

    For a more complete example go here.

提交回复
热议问题