Interface Segregation Principle

kudvenkat
4 Jan 201809:23

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

00:00

πŸ“š 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.

05:02

πŸ›  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

SOLID is an acronym for five design principles intended to make object-oriented designs more understandable, flexible, and maintainable. In the context of the video, SOLID principles are the central theme, with a focus on the Interface Segregation Principle (ISP) and Interface Aggregation Principle as part of the tutorial.

πŸ’‘Interface Segregation Principle (ISP)

ISP is one of the SOLID principles that states no client should be forced to depend on methods it does not use. In the video, it is explained through the evolution of a software design problem at Xerox, where a single job class was causing difficulties in maintenance and scalability, leading to the creation of smaller, more specific interfaces.

πŸ’‘Interface Aggregation Principle

Although not the main focus of the video, the Interface Aggregation Principle is mentioned as a related concept. It suggests that interfaces should be kept small and clients should only be forced to implement the methods they need, which is a solution to the problems presented in the video.

πŸ’‘Xerox

Xerox is used in the video as a case study to illustrate the practical application of the Interface Segregation Principle. The company faced design problems with their printer systems, which were resolved by Robert C. Martin's suggestion to segregate the large job class into smaller, more specific interfaces.

πŸ’‘Robert C. Martin

Robert C. Martin, also known as Uncle Bob, is a software developer and author who formulated the SOLID principles. In the video, he is credited with solving the design problem at Xerox by suggesting the use of the Interface Segregation Principle.

πŸ’‘Single Responsibility Principle

While not the main focus, the Single Responsibility Principle is mentioned as a related SOLID principle that emphasizes a class should have only one reason to change. The video script uses it as a basis for understanding the Interface Segregation Principle by showing how classes were decoupled for single responsibilities.

πŸ’‘Decoupling

Decoupling refers to the practice of making a system more modular by reducing the dependencies between its components. In the video, decoupling is achieved by segregating interfaces, which allows for more maintainable and flexible code, as demonstrated by the Xerox case study.

πŸ’‘Client Classes

Client classes in the video are the implementations of the interfaces. They represent different types of printers with varying capabilities. The script uses client classes to demonstrate the problems of a 'one-size-fits-all' interface and how segregating interfaces can solve these issues.

πŸ’‘Method Implementation

Method implementation in the video refers to the process of defining how an interface's methods are executed in a client class. The script discusses the problems that arise when client classes are forced to implement methods they do not need, such as the fax method in the Canon printer example.

πŸ’‘Duplex Printing

Duplex printing is a feature that allows printers to print on both sides of a page. In the video, it is used as an example of how new requirements can force all client classes to implement additional methods, even if they are not relevant to their capabilities, highlighting the need for the Interface Segregation Principle.

πŸ’‘Maintainability

Maintainability is the ease with which a software system can be modified to correct faults, improve performance, or add functionality. The video emphasizes the importance of maintainability through the use of the Interface Segregation Principle, which helps in creating more manageable and adaptable code by reducing the impact of changes.

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

play00:00

hello everyone welcome back this is part

play00:03

three of the solid design principles

play00:04

tutorial in this session we will discuss

play00:07

interface aggregation principle and

play00:10

we'll take a look at their case study of

play00:12

interface segregation principle and

play00:14

we'll implement interface aggression

play00:16

principle with a simple example please

play00:20

refer to the previous parts of the

play00:21

tutorial before proceeding in the first

play00:25

session of solid introduction we have

play00:27

understood that I in the solid is

play00:30

acronym for interface creation principle

play00:33

the interface segregation principle

play00:35

states that no plane should be forced to

play00:38

depend on methods

play00:39

it doesn't use which means instead of

play00:43

one big fat interface many small

play00:45

interfaces are preferred based on groups

play00:48

of methods with each one serving one sub

play00:51

module the interface segregation

play00:54

principle was first used and formulated

play00:56

by Robert C Martin while consulting for

play01:00

Xerox let us now understand how the

play01:02

interface segregation principle was

play01:05

evolved with the case studying as we all

play01:08

know that Xerox Corporation manufactures

play01:12

printer systems in their development

play01:15

process of new systems Xerox has created

play01:19

a new printer system that could perform

play01:21

a variety of tasks such as stapling and

play01:24

faxing along with a regular printing

play01:26

task the software for this system was

play01:30

created from the ground up and as the

play01:32

software grew for Xerox making

play01:35

modifications became more difficult so

play01:38

that even the smallest change would take

play01:41

a redeployment cycle of an R which made

play01:44

development nearly impossible the design

play01:49

problem was that a single job class was

play01:51

used by almost all of the tasks whenever

play01:55

a print job or a stapling job need to be

play01:58

performed a call was made to the the big

play02:01

job class this resulted in a fad class

play02:05

with multitudes of method specific to

play02:08

variety of different lines because of

play02:12

this design

play02:13

a staple job would know about all the

play02:16

methods of the print job even though

play02:19

there was no use for it

play02:21

to know about these different interface

play02:24

methods of a print job to work on this

play02:26

problem Robert C Martin suggested a

play02:29

solution to Xerox which is called

play02:31

interface segregation principle instead

play02:34

of having one large job class a steeple

play02:37

job interface or a print job interface

play02:39

was created that would be used by the

play02:41

steeple or print classes respectively

play02:45

this solution has evolved into a C

play02:48

interface segregation principle which

play02:50

states that one large job class is

play02:52

segregated to multiple interfaces

play02:54

depending on the requirement now if you

play02:57

recollect the previous session we have

play03:00

also covered the interface segregation

play03:02

principle along with the single

play03:04

responsibilities of the class we

play03:06

strongly recommend you to refer to the

play03:08

single responsibility session before

play03:11

proceeding in the previous session we

play03:14

have decoupled the user class to achieve

play03:18

single responsibility of handling login

play03:21

and registration also we have moved the

play03:25

error logging and send email methods to

play03:28

their respective classes or interfaces

play03:32

after those changes we have achieved

play03:35

single responsibility from the

play03:38

respective classes perspective but if

play03:41

you take a closer look at the same

play03:44

example from the perspective of

play03:46

interface segregation we have as well

play03:49

separated or segregated one big

play03:53

interface which handles login

play03:55

registration email and error logging to

play03:59

multiple interfaces adapting to

play04:01

interface segregation principle now

play04:05

let's understand the interface

play04:06

segregation principle in greater details

play04:09

with one more example in the beginning

play04:12

of this session we have discussed that

play04:14

mr. Martin has solved the issue at Xerox

play04:17

by segregating the interfaces based on

play04:20

the usage and not by enforcing the

play04:23

clients to implement the interface

play04:26

methods

play04:27

let's take the same case study example

play04:29

and see how we can achieve the interface

play04:32

aggregation principle let's now open the

play04:34

visual studio and bring up a console

play04:36

application let's now create an

play04:39

interface that performs printer related

play04:42

tasks

play04:42

right-click on this project a new item

play04:46

choose an interface and let's name this

play04:49

interface as I print thus let's make

play04:57

this interface as public let's add the

play05:02

task that a printer can perform let's

play05:05

add a method full print content which

play05:10

acts EPS string content as input

play05:14

parameter let's create some more methods

play05:18

let's just save some time let's copy

play05:20

this method and change it to scan

play05:24

content let's also create another method

play05:28

fax content and the last one which is

play05:36

photocopy the content these are some of

play05:41

the typical methods or the tasks that a

play05:43

printer can perform now let's say in my

play05:47

office I have HP printer that need to

play05:50

implement these methods to save some

play05:53

time I have already created the HP

play05:56

printer client class that implements the

play05:59

created interface if you take a look at

play06:01

this class I have implemented the I

play06:04

print tasks and doing a simple console

play06:07

dot write line and returning true have

play06:10

done the same implementation for all

play06:12

these methods to make this example very

play06:15

simple now it's all fine till this

play06:19

moment the actual problem starts then I

play06:23

have a Canon mg 2 4 7 0 printer that can

play06:27

only perform print scan and photocopy

play06:30

operations as it doesn't have the

play06:33

ability to fax the content in that

play06:37

scenario we are stuck implementing all

play06:40

the interface

play06:41

methods and we need explicitly and

play06:43

gracefully handle the method that cannot

play06:47

be implemented let's right-click on this

play06:49

client folder add a new item choose

play06:53

class and name this class as Kannan mg 2

play06:58

4 7 0 and let's implement the I print us

play07:07

let's implement the interface now please

play07:11

notice that even though my can and home

play07:14

printer cannot perform the fax operation

play07:17

still it is enforced to implement all

play07:21

the methods of the I print ask interface

play07:24

this is one of the major problems that

play07:28

we encounter in the day-to-day

play07:30

programming and this is only one part of

play07:34

the problem which we have discussed here

play07:37

now let's say there are some

play07:39

advancements in the printing technology

play07:41

where it can print a duplex content as

play07:44

well let's say this I printers can print

play07:48

the duplex content as well let's

play07:51

represent it as print duplex content and

play07:55

let's space the content as the string

play08:00

parameter again with this new methods

play08:03

addition all the clients are mandatory

play08:06

forced to implement the newly added

play08:09

method as well this is the second part

play08:12

of the problem both of these problems

play08:16

can be addressed by segregating one big

play08:19

fat interface to smaller relevant

play08:23

interfaces and that solution is called

play08:26

interface segregation principle let's

play08:29

see how we can do that to save some time

play08:33

I have already segregated the related

play08:36

methods to smaller interfaces that are

play08:39

relevant to different clients for

play08:42

example a minimum basic printer can

play08:45

perform print scan and copy operations

play08:49

and advance of printers can perform fax

play08:52

or duplex content printing

play08:55

based on the level of the printer hence

play08:58

those methods are separated into

play09:00

different interfaces I believe now you

play09:05

have a good idea on how we can implement

play09:07

interface a creation principle in the

play09:11

next session we will focus on walk/run

play09:13

amine solid which is open closed

play09:16

principle till then thank you for

play09:19

listening and have a great day

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
SOLID PrinciplesInterface SegregationSoftware DesignXerox CaseRobert C MartinProgramming TutorialOOP ConceptsDesign PatternsInterface AggregationCoding Best Practices