Pro Yearly is on sale from $80 to $50! »

Plataformatec Hack Evening: SOLID Series - Interface Segregation Principle

712a8a8a2ac5145874ab33c328a17f05?s=47 Lucas Santos
September 25, 2018

Plataformatec Hack Evening: SOLID Series - Interface Segregation Principle

In this presentation, we will understand how the Interface Segregation Principle can be used to reduce the interface fatness.

712a8a8a2ac5145874ab33c328a17f05?s=128

Lucas Santos

September 25, 2018
Tweet

Transcript

  1. HACKEVENING Lucas Santos Plataformatec

  2. S.O.L.I.D.

  3. S.O.L.I.D.

  4. S.O.L.I.D. Interface Segregation Principle

  5. Interface Segregation Principle is all about…

  6. Interface Segregation Principle is all about… INTERFACES

  7. • "Clients should not be forced to depend on methods

    that they don’t use” 
 -Robert C. Martin; Prentice Hall, 2003
  8. An use case…

  9. Consider a printer software…

  10. None
  11. None
  12. None
  13. There’s a problem…

  14. The interface is getting too fat…

  15. Solution:

  16. Solution: Segregate!

  17. None
  18. Ruby + interface = ?

  19. We can follow (or break) the ISP using the methods

    signatures in Ruby
  20. An example…

  21. None
  22. class UserRepository def get_all_by_ids(ids) @entity.where(id: ids) end private def entity

    User end end class UserController def index UserRepository.new.get_all_by_ids(params[ids]) end end
  23. None
  24. class UserRepository def get_all_by_ids(ids, sort) users = @entity.where(ids: ids) if

    sort users.order(created_at: :asc) end users end private def entity User end end class UserController def index UserRepository.new.get_all_by_ids(params[ids]) end end
  25. Yes, we are breaking the ISP even with Ruby…

  26. To solve this problem we can segregate the ordering functionality

    in another method.
  27. None
  28. class UserRepository def get_all_by_ids(ids) @entity.where(ids: ids) end def get_all_by_ids_sorted(ids) get_all_by_ids.order(created_at:

    :asc) end private def entity User end end class UserController def index UserRepository.new.get_all_by_id_sorted(params[ids]) end end
  29. Let’s debate…

  30. Thank you!