SOLID Principles part 1

2,968 views

Published on

Of the SOLID principles, made famous by Robert C. Martin, we'll discuss the Single Responsibility Principle and the Open/Closed Principle, two of the presenter's favorite principles. But at the same time the least understood and used principles. That's why 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.

Published in: Technology
  • Be the first to comment

SOLID Principles part 1

  1. 1. SOLIDSingle Responsibility Principle Open/Closed Principle Dennis van der Stelt Pascal de Jonge Software Architect Development Lead Tellus Tellus http://bloggingabout.net/blogs/dennis/ http://www.pazquality.com/ @dvdstelt @pdejonge dvdstelt@tellus.com pdejonge@tellus.com Software Development Network 2013
  2. 2. Software Development Network 2013
  3. 3. AGENDA Software Development Network 2013
  4. 4. SOLIDHistory Software Development Network 2013
  5. 5. Guidance Software Development Network 2013
  6. 6. SOLIDFive principles Single Responsibility SOLID Open/Closed Liskov Substitution Interface Seggregation Dependency Inversion Learn them, love them, live by them… Software Development Network 2013
  7. 7. Software Craftsmanshipis really, really, ridiculously good looking code Software Development Network 2013
  8. 8. SOLIDBooks Software Development Network 2013
  9. 9. SOLIDSingle Responsibility Principle Software Development Network 2013
  10. 10. SINGLE RESPONSIBILITY PRINCIPLE there should never be more than one reason for a class to change Software Development Network 2013
  11. 11. SINGLE RESPONSIBILITY PRINCIPLE just because you can, doesn’t mean you should Software Development Network 2013
  12. 12. SINGLE RESPONSIBILITY PRINCIPLERules that apply to classes rule rule same goes for methods, they should be small which we count by #01 03 lines rule rule they should be smaller how to measure class02 than that 04 size? Responsibility : “a reason for change” Software Development Network 2013
  13. 13. SINGLE RESPONSIBILITY PRINCIPLEWhy would we want to separate responsibilities? Software Development Network 2013
  14. 14. SINGLE RESPONSIBILITY PRINCIPLEWhy would we want to separate responsibilities? Software Development Network 2013
  15. 15. SINGLE RESPONSIBILITY PRINCIPLESometimes hard to see…interface IModem{ void Dial(string phoneNumber); void Hangup(); void Send(char c); char Receive();} Software Development Network 2013
  16. 16. SINGLE RESPONSIBILITY PRINCIPLE public interface IConnection { void Dial(string phoneNumber); void Hangup(); } public interface IDataChannel { void Send(char c); char Receive(); } public class Modem { public Modem(IConnection connection, IDataChannel dataChannel) { // ... } } Software Development Network 2013
  17. 17. SINGLE RESPONSIBILITY PRINCIPLE Rule of thumb If you can’t come up with a name for your class, it’s probably doing too much! Software Development Network 2013
  18. 18. DEMOInvoiceCreator Software Development Network 2013
  19. 19. SINGLE RESPONSIBILITY PRINCIPLE Violation• God class unneeded complexity Don’t create• However public class OrderService { public Order Get(int orderId) { } public Order Get(string name) { } public Order Save(Order order) { } public Order SubmitOrder(Order order) { } public void CancelOrder(Order order) { } public void ProcessOrderReturn(int orderId) { } public void ShipOrder(Order order) { } } Software Development Network 2013
  20. 20. more classes != more complex Software Development Network 2013
  21. 21. Software Development Network 2013
  22. 22. SOLIDOpen/Closed Principle Software Development Network 2013
  23. 23. OPEN/CLOSED PRINCIPLE software entities should be open for extension, but closed for modification Software Development Network 2013
  24. 24. OPEN/CLOSED PRINCIPLENon changed code doesn’t need new… Software Development Network 2013
  25. 25. DEMOUseful patterns Software Development Network 2013
  26. 26. TEMPLATE METHOD PATTERN AbstractBase class Program Initialize SendMessage Finalize Execute EmailSende r SendMessage Software Development Network 2013
  27. 27. Software Development Network 2013
  28. 28. CONCLUSION Single Responsibility SOLID Open/Closed Liskov Substitution Interface Seggregation Dependency Inversion Software Development Network 2013
  29. 29. Thank you questions? Dennis van der Stelt Pascal de Jonge Software Architect Development Lead Tellus Tellushttp://bloggingabout.net/blogs/dennis/ http://www.pazquality.com/ @dvdstelt @pdejonge dvdstelt@tellus.com pdejonge@tellus.com 33 Software Development Network 2013

×