Interface Segregation Principle
Summary
TLDRThis tutorial, part three of the SOLID design principles series, delves into the Interface Segregation Principle (ISP). It explains ISP's core idea of preferring multiple small interfaces to one large one, using the case study of Xerox's printer system to illustrate the principle. The video demonstrates how Robert C. Martin's solution to Xerox's software complexity involved creating specific interfaces for different functionalities, instead of a single, monolithic interface. It also highlights the problems of enforcing unnecessary implementation and the need for adaptability with technology advancements, advocating for ISP to address these issues effectively.
Takeaways
- π Interface Segregation Principle (ISP) is a part of SOLID design principles, emphasizing that no client should be forced to depend on methods it does not use.
- π¨βπ« ISP was formulated by Robert C. Martin, who used it to address issues at Xerox Corporation, where a single job class was being used for various tasks, leading to bloated and inflexible code.
- π¨οΈ Xerox's problem was that a single job class was handling multiple tasks like printing, stapling, and faxing, which resulted in a large class with many unused methods for different tasks.
- π ISP suggests creating multiple, smaller interfaces that are client-specific, rather than one large interface that clients must implement in full, even if they don't use all the methods.
- π οΈ The case study of Xerox led to the creation of separate interfaces for different printer functionalities, such as stapling and faxing, which were initially part of a single class.
- π ISP helps in reducing the impact of changes in the system by segregating the interface methods based on their usage, thus improving maintainability and flexibility.
- π The tutorial recommends referring to the previous session on Single Responsibility Principle, which is closely related to ISP, as both aim to reduce complexity and improve modularity.
- π In the provided example, a basic printer interface 'IPrint' was created with methods for printing, scanning, faxing, and photocopying, but not all printers need to implement all these methods.
- πΌοΈ The example of the Canon MG 2470 printer, which cannot fax, illustrates the problem of being forced to implement methods it does not use, highlighting the need for ISP.
- π ISP can also address the issue of new functionalities being added to an interface, which would then require all implementing clients to adapt to the changes, even if they are irrelevant to them.
- π§ The solution involves creating more granular interfaces that group related methods together, allowing clients to implement only the interfaces that are relevant to their functionality.
Q & A
What is the main topic of this tutorial session?
-The main topic of this tutorial session is the Interface Segregation Principle and Interface Aggregation Principle from the SOLID design principles.
What does the Interface Segregation Principle state?
-The Interface Segregation Principle states that no client should be forced to depend on methods it does not use. It advocates for many small, client-specific interfaces rather than one large, general-purpose interface.
Who formulated the Interface Segregation Principle?
-Robert C. Martin formulated the Interface Segregation Principle.
What was the problem with the software design at Xerox Corporation as described in the script?
-The problem at Xerox was that a single job class was used for all tasks, including printing, stapling, and faxing, which made the class very large and difficult to modify. This design resulted in classes knowing about methods they did not need to.
What solution did Robert C. Martin suggest to Xerox to address the software design problem?
-Robert C. Martin suggested segregating the large job class into multiple smaller interfaces, each tailored to specific tasks, thus implementing the Interface Segregation Principle.
How does the Interface Aggregation Principle relate to the Interface Segregation Principle?
-The Interface Aggregation Principle is a way to implement the Interface Segregation Principle by creating multiple smaller, relevant interfaces that clients can implement based on their specific needs, rather than being forced to implement a large, all-encompassing interface.
What is an example of a method that was added to the IPrint interface in the script?
-An example of a method added to the IPrint interface is 'printDuplexContent', which was introduced to handle the new duplex printing capability.
Why is it problematic for a printer that cannot fax to implement the IPrint interface as it was originally designed?
-It is problematic because the original IPrint interface included a fax method, and printers that cannot fax would still be forced to implement this method, even though they do not use it.
How does the script illustrate the application of the Interface Segregation Principle in practice?
-The script illustrates this by showing how interfaces for different printer capabilities (like faxing and duplex printing) are segregated into smaller, more specific interfaces, allowing clients to implement only the interfaces that are relevant to their functionality.
What is the next principle in the SOLID series that the tutorial will cover after discussing the Interface Segregation Principle?
-The next principle in the SOLID series that the tutorial will cover is the Open/Closed Principle.
Outlines
π Introduction to Interface Segregation Principle
This paragraph introduces the third part of a SOLID design principles tutorial, focusing on the Interface Segregation Principle (ISP). It explains that according to ISP, no client should be forced to depend on methods it does not use, advocating for many small, client-specific interfaces over one large, general-purpose interface. The principle is illustrated with a historical case study from Xerox, where Robert C. Martin helped address software development challenges by implementing the ISP, leading to a more maintainable and scalable design. The tutorial also encourages revisiting the Single Responsibility Principle for a deeper understanding of decoupling and interface design.
π Implementing Interface Segregation in Practice
This paragraph delves deeper into the practical application of the Interface Segregation Principle through a coding example. It describes creating an 'IPrint' interface with various printer-related methods and implementing this interface in an 'HPPrinter' class. The example highlights a problem when a 'CanonMG2470' printer, which cannot fax, is forced to implement a fax method, leading to unnecessary complexity. The paragraph then discusses the issue of new methods being added to the interface, which all clients must implement, even if they are not relevant to their functionality. The solution involves segregating the interface into smaller, more relevant ones, such as separating basic printing tasks from advanced features like faxing or duplex printing. This approach aligns with the Interface Segregation Principle, resulting in a more flexible and maintainable codebase.
Mindmap
Keywords
π‘SOLID Design Principles
π‘Interface Segregation Principle (ISP)
π‘Interface Aggregation Principle
π‘Xerox
π‘Robert C. Martin
π‘Single Responsibility Principle
π‘Decoupling
π‘Client Classes
π‘Method Implementation
π‘Duplex Printing
π‘Maintainability
Highlights
Introduction to the Interface Segregation Principle (ISP), a key concept in SOLID design principles.
Explanation of the acronym SOLID, where 'I' stands for Interface Segregation Principle.
Interface Segregation Principle (ISP) states that no client should be forced to depend on methods it doesn't use, promoting smaller, more focused interfaces.
Robert C. Martin formulated the Interface Segregation Principle while consulting for Xerox.
Case study on Xerox Corporation's printer systems, highlighting the design issues they faced with a monolithic job class.
Discussion of how Xerox's software development challenges led to the creation of a 'big fat class' with many methods irrelevant to specific tasks.
Robert C. Martin's solution to Xerox's problem: segregating one large class into multiple, smaller interfaces based on specific tasks.
Practical example of implementing ISP using a console application in Visual Studio.
Creation of a basic 'IPrintTasks' interface in the example, showcasing how it includes multiple methods like 'PrintContent,' 'ScanContent,' and 'FaxContent.'
Demonstration of the issue when a Canon printer that cannot perform faxing is forced to implement all methods in the 'IPrintTasks' interface.
Introduction of a new method 'PrintDuplexContent' to the interface and the problems it creates by forcing all clients to implement it.
Solution to the problem by segregating the 'IPrintTasks' interface into smaller, more relevant interfaces tailored to different printer capabilities.
Explanation of how segregating interfaces according to specific client needs adheres to the Interface Segregation Principle.
Summary of the benefits of implementing ISP: reduced complexity, improved maintainability, and more flexible code.
Teaser for the next session, which will focus on the Open/Closed Principle (OCP), another SOLID design principle.
Transcripts
hello everyone welcome back this is part
three of the solid design principles
tutorial in this session we will discuss
interface aggregation principle and
we'll take a look at their case study of
interface segregation principle and
we'll implement interface aggression
principle with a simple example please
refer to the previous parts of the
tutorial before proceeding in the first
session of solid introduction we have
understood that I in the solid is
acronym for interface creation principle
the interface segregation principle
states that no plane should be forced to
depend on methods
it doesn't use which means instead of
one big fat interface many small
interfaces are preferred based on groups
of methods with each one serving one sub
module the interface segregation
principle was first used and formulated
by Robert C Martin while consulting for
Xerox let us now understand how the
interface segregation principle was
evolved with the case studying as we all
know that Xerox Corporation manufactures
printer systems in their development
process of new systems Xerox has created
a new printer system that could perform
a variety of tasks such as stapling and
faxing along with a regular printing
task the software for this system was
created from the ground up and as the
software grew for Xerox making
modifications became more difficult so
that even the smallest change would take
a redeployment cycle of an R 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 need to be
performed a call was made to the the big
job class this resulted in a fad class
with multitudes of method specific to
variety of different lines because of
this design
a staple job would know about all the
methods of the print job even though
there was no use for it
to know about these different interface
methods of a print job to work on this
problem Robert C Martin suggested a
solution to Xerox which is called
interface segregation principle instead
of having one large job class a steeple
job interface or a print job interface
was created that would be used by the
steeple or print classes respectively
this solution has evolved into a C
interface segregation principle which
states that one large job class is
segregated to multiple interfaces
depending on the requirement now if you
recollect the previous session we have
also covered the interface segregation
principle along with the single
responsibilities of the class we
strongly recommend you to refer to the
single responsibility session before
proceeding in the previous session we
have decoupled the user class to achieve
single responsibility of handling login
and registration also we have moved the
error logging and send email methods to
their respective classes or interfaces
after those changes we have achieved
single responsibility from the
respective classes perspective but if
you take a closer look at the same
example from the perspective of
interface segregation we have as well
separated or segregated one big
interface which handles login
registration email and error logging to
multiple interfaces adapting to
interface segregation principle now
let's understand the interface
segregation principle in greater details
with one more example in the beginning
of this session we have discussed that
mr. Martin has solved the issue at Xerox
by segregating the interfaces based on
the usage and not by enforcing the
clients to implement the interface
methods
let's take the same case study example
and see how we can achieve the interface
aggregation principle let's now open the
visual studio and bring up a console
application let's now create an
interface that performs printer related
tasks
right-click on this project a new item
choose an interface and let's name this
interface as I print thus let's make
this interface as public let's add the
task that a printer can perform let's
add a method full print content which
acts EPS string content as input
parameter let's create some more methods
let's just save some time let's copy
this method and change it to scan
content let's also create another method
fax content and the last one which is
photocopy the content these are some of
the typical methods or the tasks that a
printer can perform now let's say in my
office I have HP printer that need to
implement these methods to save some
time I have already created the HP
printer client class that implements the
created interface if you take a look at
this class I have implemented the I
print tasks and doing a simple console
dot write line and returning true have
done the same implementation for all
these methods to make this example very
simple now it's all fine till this
moment the actual problem starts then I
have a Canon mg 2 4 7 0 printer that can
only perform print scan and photocopy
operations as it doesn't have the
ability to fax the content in that
scenario we are stuck implementing all
the interface
methods and we need explicitly and
gracefully handle the method that cannot
be implemented let's right-click on this
client folder add a new item choose
class and name this class as Kannan mg 2
4 7 0 and let's implement the I print us
let's implement the interface now please
notice that even though my can and home
printer cannot perform the fax operation
still it is enforced to implement all
the methods of the I print ask interface
this is one of the major problems that
we encounter in the day-to-day
programming and this is only one part of
the problem which we have discussed here
now let's say there are some
advancements in the printing technology
where it can print a duplex content as
well let's say this I printers can print
the duplex content as well let's
represent it as print duplex content and
let's space the content as the string
parameter again with this new methods
addition all the clients are mandatory
forced to implement the newly added
method as well this is the second part
of the problem both of these problems
can be addressed by segregating one big
fat interface to smaller relevant
interfaces and that solution is called
interface segregation principle let's
see how we can do that to save some time
I have already segregated the related
methods to smaller interfaces that are
relevant to different clients for
example a minimum basic printer can
perform print scan and copy operations
and advance of printers can perform fax
or duplex content printing
based on the level of the printer hence
those methods are separated into
different interfaces I believe now you
have a good idea on how we can implement
interface a creation principle in the
next session we will focus on walk/run
amine solid which is open closed
principle till then thank you for
listening and have a great day
Browse More Related Video
SOLID Design Principles Introduction
Single Responsibility Principle
1. SOLID Principles with Easy Examples | OOPs SOLID Principles Interview Question - Low Level Design
Low Level Design 105 | Single Responsibility Principle in SOLID | 2022 | System Design
#67 More on Interfaces in Java
Lambda Expressions in Java - Full Simple Tutorial
5.0 / 5 (0 votes)