Interface Isolation Principle (ISP)
Definition: Using multiple dedicated interfaces is better than using a single total interface. That is, don't put all the eggs in one basket.
Benefits: It is more flexible and convenient. What you don’t want to implement or do not need to be implemented can be implemented without implementation.
Explanation:
Most people like to use one interface to declare all the methods needed, but ISP suggests that using multiple special interfaces is better than using a single total interface. That is, if there are more methods in one interface, it will not be very convenient to implement.
Example 1:
using System; using ; using ; using ; using ; namespace Interface isolation principle { /// <summary> /// Define an interface for learning computers /// </summary> public interface ILearnComputer { } /// <summary> /// Define an interface for surfing the Internet /// </summary> public interface INetComputer { } /// <summary> /// Define a student computer class to realize the interface of learning computers and the interface of surfing the Internet computer /// </summary> public class StudentComputer : ILearnComputer, INetComputer { public void Learn() { ("study"); } public void NetPlay() { ("On the Internet"); } } }
In the above example code, two interfaces are defined, and a specific computer class implements two interfaces. If you only want to access the Internet, you can only implement the Internet interface; if you only want to learn, you can only implement the learning interface.
Example 2:
using System; using ; using ; using ; using ; namespace Interface isolation principle { /// <summary> /// Define an interface for learning computers /// </summary> public interface ILearnComputer { } /// <summary> /// Define an interface for surfing the Internet /// </summary> public interface INetComputer { } /// <summary> /// Define an abstract class for student computers to realize the interface of learning computers and the interface of Internet computers /// </summary> public abstract class StudentComputer : ILearnComputer, INetComputer { public abstract void Learn(); public abstract void NetPlay(); } }
During specific operations, abstract classes and interfaces are used in conjunction with each other. Use abstract classes to implement interfaces, and use abstract classes to declare variables when calling.
Code download link:Click here to download
This is what this article about the interface isolation principle of C# object-oriented design principle. I hope it will be helpful to everyone's learning and I hope everyone will support me more.