ISP -- Interface Segregation Principle, segregation

Source: Internet
Author: User

ISP -- Interface Segregation Principle, segregation

1. ISP Overview (ISP -- Interface Segregation Principle ):

 

It is better to use multiple dedicated interfaces than to use a single total interface.

 

The dependence of a class on another class should be based on the smallest interface.

 

An interface represents a role and should not assign different roles to an interface. Unrelated interfaces are merged to form a bloated large interface, which is a pollution to roles and interfaces.

 

"Customers should not be forced to rely on methods they do not need. An interface belongs to a customer and does not belong to its class hierarchy ." This is quite clear. In other words, do not force customers to use the methods they do not use. If you force users to use the methods they do not use, then these customers will face the changes caused by the changes to these unused methods.

 

  Ii. Example:

 

In use cases, provide the method needed by the caller to shield unnecessary methods and meet the interface isolation principle. For example, e-commerce systems, such as orders, are used in three places,

 

One is a portal, and only query methods are available,

 

One is an external system, and there is a way to add orders,

 

One is the management background, which is used to add, delete, modify, and query.

 

According to the interface isolation principle (ISP), the dependence of a class on another class should be established on the minimum interface.

 

That is to say, for a portal, it can only rely on an interface with a query method.

The following is the implementation code.

 

// -- Interface inheritance is not required here, because the parent interface may be modified, which affects the sub-interface.

 

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 implementsIOrderForPortal, IOrderForOtherSys, IOrderForAdmin {

Private Order (){

// -- Do nothing, just to prevent direct new, to prevent the client from direct New, and then access the method it does not need.

}

// Return to the Portal

Static IOrderForPortal getOrderForPortal (){

Return (IOrderForPortal) new Order ();

}

// Return to OtherSys

Public static IOrderForOtherSys getOrderForOtherSys (){

Return (IOrderForOtherSys) newOrder ();

}

// Return to Admin

Public static IOrderForAdmin getOrderForAdmin (){

Return (IOrderForAdmin) new Order ();

}

// -- The following is the implementation of the interface method. Only a String is returned for demonstration.

Public String getOrder (){

Return "implemented getOrder ";

}

Public String insertOrder (){

Return implementedinsertOrder ";

}

Public String updateOrder (){

Return "implementedupdateOrder ";

}

Public String deleteOrder (){

Return "implementeddeleteOrder ";

}

}

Public clss TestCreateLimit {

Public static void main (String [] args ){

IOrderForPortal orderForPortal = Order. getOrderForPortal ();

IOrderForOtherSys orderForOtherSys = Order. getOrderForOtherSys ();

IOrderForAdmin orderForAdmin = Order. getOrderForAdmin ();

System. out. println ("Portal call method:" + orderForPortal. getOrder ());

System. out. println ("OtherSys external System call method:" + orderForOtherSys. insertOrder ());

System. out. println ("Admin management background call method:" + orderForAdmin. getOrder () + ";" + orderForAdmin. insertOrder () + ";" + orderForAdmin. updateOrder () + ";" + orderForAdmin. deleteOrder ());

}

}

In this way, the interface isolation principle can be well met. The caller can only access its own method, but cannot access the method that should not be accessed.

Welcome to the discussion... the purpose of writing this article is to make common progress and make any mistakes or deficiencies ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.