Of the SOLID principles, made famous by Robert C. Martin, we'll discuss the Interface Segregation Principle and the Dependency Inversion Principle. This session will explain them thoroughly and give real life examples instead of the regular customer & order examples. You'll walk away knowing what the benefits are and how to use them properly.
1. Dennis van der Stelt
Interface Segregation Principle
Dependency Inversion Principle
Dennis van der Stelt
Software Architect
Tellus
http://bloggingabout.net/blogs/dennis/
@dvdstelt
dvdstelt@tellus.com
Pascal de Jonge
Development Lead
Tellus
http://www.pazquality.com/
@pdejonge
pdejonge@tellus.com
SOLID
6. Dennis van der Stelt
SOLID
Five principles
Single Responsibility SOLID Open/Closed
Liskov Substitution Interface Segregation
Learn them, love them, live by them…
Dependency Inversion
12. Dennis van der Stelt
INTERFACE SEGREGATION PRINCIPLE
public interface IVerifyCredentials
{
int MaxInvalidPasswordAttempts { get; }
bool ValidateUser(string username, string password);
}
public interface IManageCredentials
{
bool ChangePassword(string oldPassword, string newPassword);
int MinRequiredNonAlphanumericCharacters { get; }
int MinRequiredPasswordLength { get; }
string ResetPassword(string username);
}
public interface IManageUsers
{
MembershipUser CreateUser(string username, string password);
bool DeleteUser(string username, bool deleteAllRelatedData);
}
13. Dennis van der Stelt
INTERFACE SEGREGATION PRINCIPLE
Why is violation of this principle a problem?
introduces very fat interfaces
difficult to extend functionality
violates single responsibility principle
forced dependencies
02
03
04
01
14. Dennis van der Stelt
public interface IDoor
{
bool IsDoorOpen { get; set; }
void Lock();
void Unlock();
}
INTERFACE SEGREGATION PRINCIPLE
public class SimpleDoor : IDoor
{
public bool IsDoorOpen { get; set; }
public void Lock()
{
// Implementation here...
}
public void Unlock()
{
// Implementation here...
}
}
15. Dennis van der Stelt
public interface ITimerFunction
{
void TimerFunction();
}
INTERFACE SEGREGATION PRINCIPLE
class AdvancedDoor : IDoor, ITimerFunction
{
public bool IsDoorOpen { get; set; }
public void Lock() { }
public void Unlock() { }
public void TimerFunction()
{
// Implementation here...
}
}
16. Dennis van der Stelt
List<IDoor> doors = new List<IDoor>()
{
new SimpleDoor(),
new AdvancedDoor()
};
foreach (var door in doors)
{
if (door is ITimerFunction)
((ITimerFunction)door).TimerFunction();
}
INTERFACE SEGREGATION PRINCIPLE
17. Dennis van der Stelt
SOLID
Dependency Inversion Principle
18. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
high level modules should not
depend upon low level
modules, both should depend on
abstractions.
abstractions should not
depend upon details.
details should depend on
abstractions
19. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Inversion of control
Component A
Component B
Component C
20. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Inversion of control
Component A
Component A
Service
<<interface>>
Component B
Component A
Service
Component C
<<interface>>
21. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Inversion of control
Component A Client Service
<<interface>>
Component B
Component A
Service
Component C
<<interface>>
Component D
22. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Valar Morghulis
24. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Violations
public class SecurityService
{
public static User GetCurrentUser()
{
return Context.User;
}
}
Dependency in static method
25. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Violations
public class OrderProcessing
{
private IList<Product> FindProduct(int category)
{
var repository = new ProductRepository();
return repository.FindByCategory(category);
}
}
Instantiating your own dependencies
26. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Violations
public class OrderProcessing
{
private IList<Product> FindProduct(int category)
{
var repository = ServiceLocator.GetInstance<IProductRepository>();
return repository.FindByCategory(category);
}
}
Asking for instantiating your dependencies
only at the composition root
27. Dennis van der Stelt
COMPOSITION ROOT
Application entry point
Factory Pattern
Libraries & Frameworks
28. Dennis van der Stelt
COMPOSITION ROOT
Application entry point
Factory Pattern
Libraries & Frameworks
29. Dennis van der Stelt
COMPOSITION ROOT
Application entry point
Factory Pattern
Libraries & Frameworks
30. Dennis van der Stelt
DEPENDENCY INVERSION PRINCIPLE
Inversion of control
HomeController
ICustomer
Registration
<<interface>>
Customer
Registration
IMessageSender
Factory
MessageSender
Factory
<<interface>>
ICustomer
Repository
<<interface>>
Customer
Repository
MessageSender
Base
33. Dennis van der Stelt
CONCLUSION
Single Responsibility SOLID Open/Closed
Liskov Substitution Interface Seggregation Dependency Inversion
34. Dennis van der Stelt34
Thank you
questions?
Dennis van der Stelt
Software Architect
Tellus
http://bloggingabout.net/blogs/dennis/
@dvdstelt
dvdstelt@tellus.com
Pascal de Jonge
Development Lead
Tellus
http://www.pazquality.com/
@pdejonge
pdejonge@tellus.com
Hinweis der Redaktion
There are other talks that will go into Big Data and Hadoop so we’ll only do a quick overview of that right now. We’ll spend most of our time on Hive.
5 basic principles of OOPDIntroduced in 2000By Robert C. Martin (Uncle Bob)Whenapplied, more likelyto have a bettermaintainableandextendable system over time.
Guidelines, not hard rules, not a dogmaUse common senseDon’tjustthrowthemaway
Add “System.Web.Applications” and implement “MembershipProvider”
Also doesn’t violate SRP
NotImplementedException
Also doesn’t violate SRP
Also doesn’t violate SRP
Also doesn’t violate SRP
Interfaces are packaged with higher-level components. They define higher-level components needs, not lower-level components behavior.
Tight coupling- When 2 classes depend on each other