SOLID Principles part 2

1,315 views

Published on

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.

  • I see your presentation on SOLID is incomplete, missing the Liskov Substitution Principle
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Can you please send me SOLID Principles part 1 & 2 ppt to aashaikh55@gmail.com.

    Thanks in advance.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

SOLID Principles part 2

  1. 1. Dennis van der SteltInterface Segregation PrincipleDependency Inversion PrincipleDennis van der SteltSoftware ArchitectTellushttp://bloggingabout.net/blogs/dennis/@dvdsteltdvdstelt@tellus.comPascal de JongeDevelopment LeadTellushttp://www.pazquality.com/@pdejongepdejonge@tellus.comSOLID
  2. 2. Dennis van der Stelt
  3. 3. Dennis van der SteltAGENDA
  4. 4. Dennis van der SteltSOLIDHistory
  5. 5. Dennis van der SteltGuidance
  6. 6. Dennis van der SteltSOLIDFive principlesSingle Responsibility SOLID Open/ClosedLiskov Substitution Interface SegregationLearn them, love them, live by them…Dependency Inversion
  7. 7. Dennis van der SteltSOLIDBooks
  8. 8. Dennis van der SteltSOLIDInterface Segregation Principle
  9. 9. Dennis van der SteltINTERFACE SEGREGATION PRINCIPLEclients should not be forcedto depend on methodsthey do not use
  10. 10. Dennis van der SteltINTERFACE SEGREGATION PRINCIPLEtailor interfaces to individual client’s needs
  11. 11. Dennis van der SteltDEMOMembership Provider
  12. 12. Dennis van der SteltINTERFACE SEGREGATION PRINCIPLEpublic 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. 13. Dennis van der SteltINTERFACE SEGREGATION PRINCIPLEWhy is violation of this principle a problem?introduces very fat interfacesdifficult to extend functionalityviolates single responsibility principleforced dependencies02030401
  14. 14. Dennis van der Steltpublic interface IDoor{bool IsDoorOpen { get; set; }void Lock();void Unlock();}INTERFACE SEGREGATION PRINCIPLEpublic class SimpleDoor : IDoor{public bool IsDoorOpen { get; set; }public void Lock(){// Implementation here...}public void Unlock(){// Implementation here...}}
  15. 15. Dennis van der Steltpublic interface ITimerFunction{void TimerFunction();}INTERFACE SEGREGATION PRINCIPLEclass AdvancedDoor : IDoor, ITimerFunction{public bool IsDoorOpen { get; set; }public void Lock() { }public void Unlock() { }public void TimerFunction(){// Implementation here...}}
  16. 16. Dennis van der SteltList<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. 17. Dennis van der SteltSOLIDDependency Inversion Principle
  18. 18. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEhigh level modules should notdepend upon low levelmodules, both should depend onabstractions.abstractions should notdepend upon details.details should depend onabstractions
  19. 19. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEInversion of controlComponent AComponent BComponent C
  20. 20. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEInversion of controlComponent AComponent AService<<interface>>Component BComponent AServiceComponent C<<interface>>
  21. 21. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEInversion of controlComponent A Client Service<<interface>>Component BComponent AServiceComponent C<<interface>>Component D
  22. 22. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEValar Morghulis
  23. 23. Dennis van der SteltDEMOGame on!!!
  24. 24. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEViolationspublic class SecurityService{public static User GetCurrentUser(){return Context.User;}}Dependency in static method
  25. 25. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEViolationspublic class OrderProcessing{private IList<Product> FindProduct(int category){var repository = new ProductRepository();return repository.FindByCategory(category);}}Instantiating your own dependencies
  26. 26. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEViolationspublic class OrderProcessing{private IList<Product> FindProduct(int category){var repository = ServiceLocator.GetInstance<IProductRepository>();return repository.FindByCategory(category);}}Asking for instantiating your dependenciesonly at the composition root
  27. 27. Dennis van der SteltCOMPOSITION ROOTApplication entry pointFactory PatternLibraries & Frameworks
  28. 28. Dennis van der SteltCOMPOSITION ROOTApplication entry pointFactory PatternLibraries & Frameworks
  29. 29. Dennis van der SteltCOMPOSITION ROOTApplication entry pointFactory PatternLibraries & Frameworks
  30. 30. Dennis van der SteltDEPENDENCY INVERSION PRINCIPLEInversion of controlHomeControllerICustomerRegistration<<interface>>CustomerRegistrationIMessageSenderFactoryMessageSenderFactory<<interface>>ICustomerRepository<<interface>>CustomerRepositoryMessageSenderBase
  31. 31. Dennis van der SteltDEMODependency Injection in MVC
  32. 32. Dennis van der Stelt
  33. 33. Dennis van der SteltCONCLUSIONSingle Responsibility SOLID Open/ClosedLiskov Substitution Interface Seggregation Dependency Inversion
  34. 34. Dennis van der Stelt34Thank youquestions?Dennis van der SteltSoftware ArchitectTellushttp://bloggingabout.net/blogs/dennis/@dvdsteltdvdstelt@tellus.comPascal de JongeDevelopment LeadTellushttp://www.pazquality.com/@pdejongepdejonge@tellus.com

×