Press "Enter" to skip to content

Interface Segregation Principle in C# – ISP

SOLID principles now ending with this article! Todays topic is Interface Segregation Principle with C# sample code.

Let’s start with the general definition of the ISP: “metot objects should not be forced to be connected to the Interface’s where the methods they do not need ’”. I think so clear, right?

Lets have an daily life example for this definition:

Nowadays, there is a concept that we call smart phones!. So, calling phone and chatting and sending SMS, devices that do a lot more work. You can take photos and videos, connect to social networks, and control your e-mails. But these abilities are completely isolated from each other, right? They certainly don’t interfere! So you’ve got a great picture of you walking down the road; Receive your smartphone to take pictures. But what is that! When you’re about to take photos, your smart device asks for the number that needs to be translated! It wouldn’t be a nice thing.

As you can see in this example, the current module of the phone (for example the camera) will only stay in communication with the corresponding Interface. In this way, you have created a more developed architecture.

Lets write some code:

Interface Segregation Principle with C# Sample Code

Let code like a social messaging platform. Members of this platform can send voice, video or just text messages to each other. You have decided that it is appropriate to design an interface in terms of architecture and you have created an interface as follows:

public interface IMessage
        {
            string From { get; set; }
            string To { get; set; }
            string MessageBody { get; set; }
            void AttachImage(byte[] stream);
            int ImageWidth { get; set; }
            int ImageHeight { get; set; }
            void AttachMovie(byte[] stream);
            int MovieDuration { get; set; }
        }

Then you created your Message class by implementing this Interface:

ISP Sample code in C#

Problem:

Again, everything seems to be fine. But will the message object always have a picture or video? So do you think I need to use features like ImageWidth, MovieDuration if this object, which is going to be in traffic between client-server, only contains text messages? Yeah, I don’t think so! I think we can solve this architectural problem by simplifying the IMessage interface a little more.

Solution:

IMessage doing lots of work. So we can seperate some of functionality.

  • All message must include from, To and MessageBody features.
  • You have to move the image-containing messages, the AttachImage method and the properties of ImageWidth, ImageHeight.
  • You have to include messages that contain videos, the AttachMovie method and the MovieDuration feature.
Interface Segregation Principle

Thus; I only have a chance to develop objects that depend on the interfaces needed.

ISP Article Image

Now you have a more developed design in your hand! Separate interfaces and cleaner architecture.

Thanks for reading. We’ve finished SOLID principles. I am going to start Design Patterns immediately!

See you soon.

Comments are closed.