Dependency Inversion principle

advertisement
Learning SOLID Principles
Using C#
Dhananjay Kumar
Infragistics Consultant
Microsoft MVP, C# Corner MVP
http://debugmode.net
I am Dhananjay Kumar
6 times Microsoft MVP
Infragistics Consultant
7 times C-SharpCorner MVP
@debug_mode
Blog : http://debugmode.net
Agenda
SOLID Principles
Single
Responsibilit
y principle
OpenClosed
principle
Liskov
Substitution
principle
Interface
Segregation
principle
Dependency
Inversion
principle
SOILD Principles
S
•Single Responsibility principle
O
•Open Closed principle
L
•Liskov Substitution principle
I
•Interface segregation principle
D
•Dependency Inversion principle
SOILD Principles
Single Responsibility principle
• A class should have single responsibility
Open Closed principle
• A class should be open for extension , closed for modification
Liskov Substitution principle
• Object of parent type should be replaceable by the object of subtype
Interface Segregation principle
• many client-specific interfaces are better than one general-purpose interface
Dependency Inversion principle
• one should “Depend upon Abstractions. Do not depend upon concretions
SRP
Class should have
only one
responsibility
Class should have
only one reason to
change
One responsibility
means one reason
to change the class
A class must either
used entirely or
not at all used
SRP
Responsibilities examples
There are more than on functions (reason to
change) in the class. Even if we change one
of the functions, we will have to RETEST the
whole class. Assume we have four
functions(reason to change) in a class.
Change in single function will cause retesting of all four functions, which is not
desirable. SRP avoids it.
 Persistence
 Validation
 Notification
 Error Handling
 Logging
 Class Selection / Instantiation
 Formatting
 Parsing
 Mapping
 Sending mail
 Printing etc.
SRP
Demo
OCP
Class should be designed in such a way that, it
should be
Open for extension
Closed for modification
OCP
Consider a scenario
 There is a class with the responsibility to calculate discount
 At the time of the class designing you have three rules for the discount offer.
 Discounts are calculated on various rules and that can be implemented as shown below ,
if(rule 1 ) then give discount 1
else if(rule 2 ) then give discount 2
else if (rule 3) then give discount 3
and so on ………..
 If a new rule is coming in the system , you will go and add new else if in the above code
 By adding new else if , you will modify the existing class.
 Once modified you have to again test all the rules for the better design
OCP
Demo
LSP
Class should be designed in such a way thatobject of derived class should
able to replace object of the
base class without bringing any
error in the system or modifying
the behavior of the base class.
If S is subset of T then,
object of T could be
replaced by object of S
without impacting the
program and bringing any
error in the system
LSP
Class should be designed in such a way that-
classical :
is- a relationship
Inheritance
LS principle :
is-substitute for
relationship
LSP
Demo
ISP
The ISP says , clients should
not be forced to depend on
the methods they do not use.
ISP splits interfaces which are
very large into smaller and
more specific ones so that
clients will only have to know
about the methods that are of
interest to them
ISP
Avoid- Fat
interface
Create- Role
based
interface
ISP
Demo
DIP
High-level modules should not
depend on low-level modules.
Both should depend on
abstractions.
Abstractions should not
depend on details. Details
should depend on
abstractions.
DIP
Let us understand with the
Demo
Summary
•
•
•
•
Single Responsibility principle
Open-Closed principle
Liskov Substitution principle
Interface Segregation principle
• Dependency Inversion principle
What Infragistics can offer you?
•
We welcome all of you to take advantage of a FREE 30 Day Trial by downloading
the product at: http://www.infragistics.com/products/ultimate/download
•
Please reach out to us at Sales-India@infragistics.com for any follow up questions
you may have. We welcome the opportunity to assist you.
Download