客戶端不應該依賴它不需要的接口;一個類對另一個類的依賴應該建立在最小的接口上。
基本介紹
- 中文名:接口隔離原則
- 外文名:Interface Segregation Principle
- 英文簡稱:ISP
- 性質:名詞
- 領域:計算機
簡介
舉例說明
interface IOrderForPortal{ String getOrder();}interface IOrderForOtherSys{ String insertOrder(); String getOrder();}interface IOrderForAdmin{ //extendsIOrderForPortal,IOrderForOtherSys String deleteOrder(); String updateOrder(); String insertOrder(); String getOrder();}/*interface IOrderForPortal{ String getOrder();}interface IOrderForOtherSys{ String insertOrder();}interface IOrderForAdmin extendsIOrderForPortal,IOrderForOtherSys{ String updateOrder(); String deleteOrder();}*/class Order implements IOrderForPortal,IOrderForOtherSys,IOrderForAdmin{ private Order(){ //--什麼都不乾,就是為了不讓直接 new,防止客戶端直接New,然後訪問它不需要的方法. } //返回給Portal public static IOrderForPortal getOrderForPortal(){ return (IOrderForPortal)new Order(); } //返回給OtherSys public static IOrderForOtherSys getOrderForOtherSys(){ return (IOrderForOtherSys)new Order(); } //返回給Admin public static IOrderForAdmin getOrderForAdmin(){ return (IOrderForAdmin)new Order(); } //--下面是接口方法的實現.只是返回了一個String用於演示 public String getOrder(){ return "implemented getOrder"; } public String insertOrder(){ return "implemented insertOrder"; } public String updateOrder(){ return "implemented updateOrder"; } public String deleteOrder(){ return "implemented deleteOrder"; }}public class TestCreateLimit{ public static void main(String[] args){ IOrderForPortal orderForPortal =Order.getOrderForPortal(); IOrderForOtherSys orderForOtherSys =Order.getOrderForOtherSys(); IOrderForAdmin orderForAdmin = Order.getOrderForAdmin(); System.out.println("Portal門戶調用方法:"+orderForPortal.getOrder()); System.out.println("OtherSys外部系統調用方法:"+orderForOtherSys.insertOrder()); System.out.println("Admin管理後台調用方法:"+orderForAdmin.getOrder()+";"+orderForAdmin.insertOrder()+";"+orderForAdmin.updateOrder()+";"+orderForAdmin.deleteOrder()); }}