Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Interface Segregation Principle

Suggested Videos
SOLID Design Principles Introduction | Text | Slides
Single Responsibility Principle | Text | Slides

In this session we will discuss  
  • Interface Segregation Principle
  • Will look at a Case Study of Interface Segregation Principle
  • And will implement Interface Segregation Principle with a simple example

In the first session of SOLID Introduction we have understood that "I" in SOLID is an acronym for Interface Segregation Principle
  • The interface-segregation principle (ISP) states that "no client should be forced to depend on methods it does not use".
  • This means, instead of one fat interface many small interfaces are preferred based on groups of methods with each one serving one sub-module.
  • The ISP was first used and formulated by Robert C. Martin while consulting for Xerox. 

Let us now understand how the ISP was evolved with a case study.

Case Study

Problem
  • As we all know Xerox Corporation manufactures printer systems. In their development process of new systems Xerox had created a new printer system that could perform a variety of tasks such as stapling and faxing along with the regular printing task. 
  • The software for this system was created from the ground up. 
  • As the software grew for Xerox, making modifications became more and more difficult so that even the smallest change would take a redeployment cycle of an hour, which made development nearly impossible.
  • The design problem was that a single Job class was used by almost all of the tasks. Whenever a print job or a stapling job needed to be performed, a call was made to the Job class. 
  • This resulted in a 'fat' class with multitudes of methods specific to a variety of different clients. 
Because of this design, a staple job would know about all the methods of the print job, even though there was no use for them.

Solution
  • To overcome this problem Robert C Martin suggested a solution which is called the Interface Segregation Principle. 
  • Which means, Instead of one fat interface many small interfaces are preferred based on groups of methods with each one serving one sub-module.
Below example demonstrates how we can achieve Single Responsibility Principle

Code before Interface Segregation Principle.

namespace ISPDemoConsole
{
    public interface IPrintTasks
    {
        bool PrintContent(string content);
        bool ScanContent(string content);
        bool FaxContent(string content);
        bool PhotoCopyContent(string content);
        bool PrintDuplexContent(string content);
    }
}

namespace ISPDemoConsole.Client
{
    class HPLaserJet : IPrintTasks
    {
        public bool FaxContent(string content)
        {
            Console.WriteLine("Fax Done"); return true;
        }
        public bool PhotoCopyContent(string content)
        {
            Console.WriteLine("PhotoCopy Done"); return true;
        }
        public bool PrintContent(string content)
        {
            Console.WriteLine("Print Done"); return true;
        }
        public bool PrintDuplexContent(string content)
        {
            Console.WriteLine("Print Duplex Done"); return true;
        }
        public bool ScanContent(string content)
        {
            Console.WriteLine("Scan Done"); return true;
        }
    }
}

namespace ISPDemoConsole.Client
{
    class CannonMG2470 : IPrintTasks
    {
        public bool PhotoCopyContent(string content)
        {
            Console.WriteLine("PhotoCopy Done"); return true;
        }
        public bool PrintContent(string content)
        {
            Console.WriteLine("Print Done"); return true;
        }
        public bool ScanContent(string content)
        {
            Console.WriteLine("Scan Done"); return true;
        }
        public bool PrintDuplexContent(string content)
        {
            return false;
        }
        public bool FaxContent(string content)
        {
            return false;
        }
     }
}

Code after Interface Segregation Principle

namespace ISPDemoConsole
{
    interface IPrintScanContent
    {
        bool PrintContent(string content);
        bool ScanContent(string content);
        bool PhotoCopyContent(string content);
    }

    interface IFaxContent
    {
        bool FaxContent(string content);
    }

    interface IPrintDuplex
    {
        bool PrintDuplexContent(string content);
    }
} 

namespace ISPDemoConsole.Client
{
    class HPLaserJet : IPrintScanContent, IFaxContent, IPrintDuplex
    {
        public bool FaxContent(string content)
        {
            Console.WriteLine("Fax Done"); return true;
        }
        public bool PhotoCopyContent(string content)
        {
            Console.WriteLine("PhotoCopy Done"); return true;
        }
        public bool PrintContent(string content)
        {
            Console.WriteLine("Print Done"); return true;
        }
        public bool PrintDuplexContent(string content)
        {
            Console.WriteLine("Print Duplex Done"); return true;
        }
        public bool ScanContent(string content)
        {
            Console.WriteLine("Scan Done"); return true;
        }
    }
}

namespace ISPDemoConsole.Client
{
    class CannonMG2470 : IPrintScanContent
    {
        public bool PhotoCopyContent(string content)
        {
            Console.WriteLine("PhotoCopy Done");
            return true;
        }
        public bool PrintContent(string content)
        {
            Console.WriteLine("Print Done");
            return true;
        }
        public bool ScanContent(string content)
        {
            Console.WriteLine("Scan Done");
            return true;
        }
    }
}

Also, we strongly recommend you to refer to our design pattern tutorial for more details on creational design patterns 

I believe this session has given you a good idea on how we can implement Interface segregation principle. 

In our next video we will focus on Open closed principle.

solid design principles c#

No comments:

Post a Comment

It would be great if you can help share these free resources