Six principles of design mode (4)-six principles of interface isolation and Design Mode

Source: Internet
Author: User
Tags class manager

Six principles of design mode (4)-six principles of interface isolation and Design Mode

Definition:

The client should not rely on interfaces that it does not need; the dependency between classes should be built on the smallest interface. The Principle of Interface isolation is called Interface Segregation Principle (ISP for short.

Personal Understanding:

In general, the interface should not be bloated, but be refined as much as possible based on specific needs. As few methods as possible in the interface. An interface is a contract designed for external users. Multiple Interfaces can be defined in a distributed manner to prevent future changes from spreading, making the real system more stable and maintainability.

Problem:

Class A relies on Class B through interface I, and class C relies on Class D through interface I. If interface I is not the smallest interface for Class A and Class C, therefore, classes C and D must have methods not required in the corresponding interface I. As shown in:

/// <Summary> /// interface I, which defines five methods // </summary> public interface I {void Method1 (); void Method2 (); void Method3 (); void Method4 (); void Method5 ();} /// <summary> /// class A /// </summary> public class A {public void Depend1 (I I) {I. method1 ();} public void Depend2 (I I) {I. method2 ();} public void Depend3 (I I) {I. method3 () ;}} public class B: I {public void Method1 () {Console. writeLine ("Class B implements interface I Method 1 ");} public void Method2 () {Console. writeLine ("method 2 for Class B implementing interface I");} public void Method3 () {Console. writeLine ("method 3 for Class B to implement interface I");} // For Class B, the Method4 and Method5 methods are not required. However, since the interface I is inherited, // even if the method body is empty, you must implement these two methods public void Method4 () {} public void Method5 () {}}/// <summary> /// class C // </summary> public class C {public void Depend1 (I I) {I. method1 ();} public void Depend2 (I I) {I. method4 ();} public void Depend3 (I I) {I. method5 () ;}} public class D: I {public void Method1 () {Console. writeLine ("method 1 for Class D to implement interface I");} // For Class B, the Method4 and Method5 methods are not required. However, since the interface I is inherited, // even if the method body is empty, you must implement these two methods: public void Method2 () {} public void Method3 () {} public void Method4 () {Console. writeLine ("method 4 for Class D to implement interface I");} public void Method5 () {Console. writeLine ("method of class D implementing interface I 5 ");}}

Solution:

The bloated interface I is subdivided into several interfaces. Class A and Class C establish dependencies with their corresponding interfaces respectively. The core definition of the Interface isolation principle is that the Interface should be as small as possible, and there should be no bloated Interface (Fat Interface), but the "small" is limited. The first is that it cannot violate the single responsibility principle. If an interface is too bloated, the instance that inherits the interface must implement all methods of the interface no matter it requires any methods of the interface. Therefore, the solution above is to divide interface I into three interfaces, and implement the Class A and Class C respectively. The class diagram structure is as follows:

/// <Summary >/// interface I1, defines method 1 /// </summary> public interface I1 {void Method1 ();} /// <summary> /// interface I2, defines methods 2 and 3 /// </summary> public interface I2 {void Method2 (); void Method3 ();} /// <summary> /// interface I3, defines methods 4 and 5 /// </summary> public interface I3 {void Method4 (); void Method5 ();} /// <summary> /// class A /// </summary> public class A {public void Depend1 (I1 I) {I. method1 ();} public void Depend2 (I2 I) {I. method2 ();} public void Depend3 (I2 I) {I. method3 () ;}} public class B: I1, I2 {public void Method1 () {Console. writeLine ("method 1 for Class B to implement interface I");} public void Method2 () {Console. writeLine ("method 2 for Class B implementing interface I");} public void Method3 () {Console. writeLine ("method 3 for Class B implementing interface I ");}} /// <summary> /// class C /// </summary> public class C {public void Depend1 (I1 I) {I. method1 ();} public void Depend2 (I3 I) {I. method4 ();} public void Depend3 (I3 I) {I. method5 () ;}} public class D: I1, I3 {public void Method1 () {Console. writeLine ("method 1 for Class D to implement interface I");} public void Method4 () {Console. writeLine ("method 4 for Class D to implement interface I");} public void Method5 () {Console. writeLine ("method of class D implementing interface I 5 ");}}

The code structure is as follows:

You will understand the figure!

The concept of the interface isolation principle is: to create a single interface, do not create a large and bloated interface, and refine the interface as much as possible, as few methods as possible in the interface. That is to say, we need to create a dedicated interface for each class, instead of trying to create a very large interface for all the classes dependent on it to call. In this example, the interface isolation principle is used to change a large interface to three dedicated interfaces. In program design, it is more flexible to rely on several dedicated interfaces than to rely on a comprehensive interface. An interface is a "contract" set for external users during design. Multiple Interfaces can be defined discretely to prevent the spread of external changes and improve the flexibility and maintainability of the system.

By the way, there is a difference between the single responsibility principle and the interface isolation principle. The single responsibility principle focuses on classes, followed by interfaces and methods. The key point is "responsibility", which corresponds to a "single" implementation part "; the interface isolation principle is aimed at the abstraction level. It focuses on the isolation between abstraction and specific implementation, and corresponds to the isolation between Abstract interfaces.

Note the following three points when using the interface isolation principle:

1. the interface should be as small as possible, and the method should be as few as possible, which complies with the interface isolation principle definition, but should be moderate in a specific project, it is impossible to create a lot of interfaces, therefore, the application interface isolation principle should also be analyzed in detail.

2. customized services: customized services provide excellent services for each individual.

3. improve cohesion (high cohesion is to improve the processing capabilities of interfaces, classes, modules, and reduce external interactions), reduce external interactions, and use the fewest methods to accomplish as many things as possible.


C # What is the interface isolation principle?

The interface isolation principle indicates that the client should not be forced to implement some interfaces that they do not use. Instead, the method in the fat interface should be grouped and replaced by multiple interfaces. Each interface serves a submodule.
Interface isolation principle
Clients should not be forced to rely on interfaces they do not use.
Instance
The following is an example that violates the interface isolation principle. We use the Manager class to represent the Manager of a management worker. There are two types of workers: normal and efficient, both of which need lunch. Now a group of robots are working for the company, but they don't need lunch. On the one hand, the Robot class needs to implement the IWoker interface, because they need to work, on the other hand, they do not need to implement the IWorker interface, because they do not need to eat.
In this case, IWorker is considered a contaminated interface.
If we keep the current design, the Robot class will be forced to implement the eat () method. We can write a dumb class and do nothing (for example, it takes only one second for lunch ), however, this results in unexpected results for the Program (for example, the manager sees the report showing that the lunch being taken away is more than the actual number of people ).
According to the interface isolation principle, a flexible design should not contain contaminated interfaces. For our example, we should separate IWorker into two interfaces.
3. interface IWorker {
4. public void work ();
5.
6. public void eat ();
7 .}
8.
9. class Worker implements IWorker {
10. public void work (){
11. //... working
12 .}
13.
14. public void eat (){
15. //... eating in launch break
16 .}
17 .}
18.
19. class SuperWorker implements IWorker {
20. public void work (){
21. //... working much more
22 .}
23.
24. public void eat (){
25. //... eating in launch break
26 .}
27 .}
28.
29. class Manager {
30. IWorker worker;
31.
32. public void setWorker (IWorker w ){
33. worker = w;
34 .}
35.
36. public void manage (){
37. worker. work ();
38 .}
39 .}

// Interface segregation principle-bad ...... remaining full text>

What is the interface isolation principle?

Some ports do not need to be used, but if you do not isolate them, it may affect other ports, such as high resistance or grounding.

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.