Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the principle of C # Interface isolation

Shulou Source: shulou.com Published: 2022-06-01 10:36:30 09月11日 Update

Today, the editor will share with you the relevant knowledge points about how to implement the principle of C# interface isolation. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

Interface isolation principle (ISP)

Definition: using multiple specialized interfaces is better than using a single master interface. That is, don't put all your eggs in one basket.

Benefits: more flexible, convenient, do not want to achieve or do not need to achieve can not be achieved.

Explanation:

Most people like to declare all the methods they need with one interface, but ISP suggests that it is better to use multiple specialized interfaces than to use a single master interface, that is, if there are more methods in one interface, it is not very convenient to implement.

Sample 1:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace interface isolation principle {/ define an interface for learning computers / public interface ILearnComputer {} / define an interface for Internet computers / public interface INetComputer {} / define a student computer class Realize the interface of learning computer and Internet computer / public class StudentComputer: ILearnComputer, INetComputer {public void Learn () {Console.WriteLine ("learning") } public void NetPlay () {Console.WriteLine ("online");}

In the sample code above, two interfaces are defined, and a specific computer class implements two interfaces. If you only want to surf the Internet, you can only implement the interface for surfing the Internet; if you only want to learn, you can only implement the interface for learning.

Sample 2:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace interface isolation principle {/ define an interface for learning computers / public interface ILearnComputer {} / define an interface for Internet computers / public interface INetComputer {} / define an abstract class for a student computer Realize the interface of learning computer and Internet computer / public abstract class StudentComputer: ILearnComputer, INetComputer {public abstract void Learn () Public abstract void NetPlay ();}}

For concrete operations, abstract classes and interfaces are used in combination. Use the abstract class to implement the interface and use the abstract class to declare variables when called.

These are all the contents of the article "how to implement the principle of C# Interface isolation". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Interfaces computers learning principles isolation knowledge articles examples Centers two content multiple most students methods computer classes good faceted different big Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB Redmi Linux Xiaomi