接口隔離原則
阿新 • • 發佈:2019-01-01
好的 alt 技術 pri 設計思想 技術分享 很好 spa div
1.定義:客戶端不要依賴它不需要的接口,一個類對另一個類的依賴應該建立在最小的接口上。
2.註意適度原則,一定要適度
3.優點:符合我們常說的高內聚低耦合的設計思想,從而使得類具有很好的可讀性、可擴展性和可維護性。
4.實例目錄package
5.實例UML類圖
6.代碼
1 package com.geely.design.principle.interfacesegregation; 2 3 public interface IAnimalAction { 4 void eat(); 5 void fly(); 6 void swim(); 7 }
1package com.geely.design.principle.interfacesegregation; 2 3 public class Bird implements IAnimalAction{ 4 public void eat() { 5 6 } 7 8 public void fly() { 9 10 } 11 12 public void swim() { 13 14 } 15 }
1 package com.geely.design.principle.interfacesegregation;2 3 public interface IEatAnimalAction { 4 void eat(); 5 }
1 package com.geely.design.principle.interfacesegregation; 2 3 public interface ISwimAnimalAction { 4 void swim(); 5 }
1 package com.geely.design.principle.interfacesegregation; 2 3 public interface IFlyAnimalAction { 4 voidfly(); 5 }
1 package com.geely.design.principle.interfacesegregation; 2 3 public class Dog implements ISwimAnimalAction,IEatAnimalAction { 4 public void eat() { 5 6 } 7 8 public void swim() { 9 10 } 11 }
接口隔離原則