Upgrade to Pro — share decks privately, control downloads, hide ads and more …

There is an I in SOLID

There is an I in SOLID

Presentation focuses on the "interface seggregation" principle of the SOLID patterns

9227671c02701253b64e469823671635?s=128

Tom Van Herreweghe

October 03, 2017
Tweet

Transcript

  1. THERE IS AN I IN SOLID

  2. Waddis SOLID? Efkes over de I Voorbeeldjes Nog bitje meer

    over de I
  3. Waddis SOLID? Efkes over de I Voorbeeldjes Nog bitje meer

    over de I
  4. WADDIS SOLID? Acroniem

  5. ACRONIEM? “Een letterwoord” “Woord gevormd door de eerste letters van

    andere woorden”
  6. TAFKAP “The Artist Formerly Known As Prince”

  7. LOL “Laughing Out Loud”

  8. RTFM “Read The Fucking Manual”

  9. YAGNI “You Ain't Gonna Need It”

  10. DRY “Don't Repeat Yourself”

  11. S Single Responsability Principle O Open/Closed Principle L Liskov Substitution

    Principle I Interface Segregation Principle D Dependency Inversion Principle
  12. SOLID - BETEKENIS? 5 design principes Uncle Bob Martin Doel

    Code flexibeler, begrijpbaarder, onderhoudbaar(der..)
  13. THROWBACK

  14. THROWBACK

  15. Waddis SOLID? Efkes over de I Voorbeeldjes Nog bitje meer

    over de I
  16. INTERFACE SEGREGATION PRINCIPLE

  17. INTERFACE

  18. “Definitie van [public] methods die objecten moeten implementeren”

  19. “Afspraak / Contract dat toelaat om 2 objecten met elkaar

    te laten communiceren”
  20. “Creëert een verwachting waaraan moet voldaan worden”

  21. SEGREGATION “SCHEIDING”

  22. INTERFACE SEGREGATION

  23. “No object should be forced to depend on methods it

    doesn't use”
  24. “Opsplitsen van [grote] interfaces in kleinere, meer specifieke”

  25. Herbruikbaarheid++ Specifiekere dependencies

  26. Waddis SOLID? Efkes over de I Voorbeeldjes Nog bitje meer

    over de I
  27. SETUP - BLOGPOST CLASS class BlogPost implements BlogPostInterface { public

    function getTitle() { /* ... */ } public function getContent() { /* ... */ } public function getAuthor() { /* ... */ } public function getSlug() { /* ... */ } }
  28. SETUP - BLOGPOST INTERFACE interface BlogPostInterface { public function getTitle();

    public function getContent(); public function getAuthor(); public function getSlug(); }
  29. SETUP - USAGE $blog = new BlogPost($title, $content, $author); $blog->setSlug(

    $slugifier->createSlug($blog) ); /* ... */
  30. SETUP - SLUGIFIER class Slugifier { /* ... */ public

    function createSlug(BlogPostInterface $blog) { $content = $blog->getSlugableContent(); /* ... do some magic ... */ return $slug; } /* ... */ }
  31. UITBREIDING - BLOGPOSTINTERFACE interface BlogPostInterface { public function getTitle(); public

    function getContent(); public function getAuthor(); public function getSlug(); public function getSlugableContent(); }
  32. UITBREIDING - AUTHOR CLASS class Author implements AuthorInterface { public

    function getFirstname() { /* ... */ } public function getLastname() { /* ... */ } public function getFullname() { /* ... */ } public function getSlug() { /* ... */ } public function getSlugableContent() { /* ... */ } }
  33. UITBREIDING - AUTHOR INTERFACE interface AuthorInterface { public function getFirstname();

    public function getLastname(); public function getFullname(); public function getSlug(); public function getSlugableContent(); }
  34. SLUGIFIER - PROBLEEM! class Slugifier { /* ... */ public

    function createSlug(BlogPostInterface $blog) { $content = $blog->getSlugableContent(); /* ... do some magic ... */ return $slug; } /* ... */ }
  35. SLUGIFIER - PROBLEEM! (PART 2) class Slugifier { /* ...

    */ public function createSlug($item) { $content = $item->getSlugableContent(); /* ... do some magic ... */ return $slug; } /* ... */ }
  36. None
  37. THEORETISCH... $item = Weather(); $slug = $slugifier->createSlug($weather); /* ... */

  38. OPLOSSING: INTERFACE SEGREGATION

  39. SUGGABLE - INTERFACE interface Slugable { public function getSlugableContent(); public

    function getSlug(); }
  40. SLUGABLE - SLUGIFIER class Slugifier { /* ... */ public

    function createSlug(Slugable $item) { $content = $item->getSlugableContent(); /* ... do some magic ... */ return $slug; } /* ... */ }
  41. OPLOSSING - BLOGPOST class BlogPost implements BlogPostInterface, Slugable { public

    function getTitle() { /* ... */ } public function getContent() { /* ... */ } public function getAuthor() { /* ... */ } public function getSlug() { /* ... */ } public function getSlugableContent() { /* ... */ } }
  42. OPLOSSING - BLOGPOST INTERFACE interface BlogPostInterface { public function getTitle();

    public function getContent(); public function getAuthor(); }
  43. DONE!

  44. NOT

  45. UITBREIDING - URL'S // /2017/09/this-is-my-title $blogUrl = $urlGenerator->generateBlogUrl($blog); // /auhtor/tom-van-herreweghe

    $authorUrl = $urlGenerator->generateAuthorUrl($author);
  46. UITBREIDING - URL'S // /2017/09/{slug} $blogUrl = $urlGenerator->generateBlogUrl($blog); // /auhtor/{slug}

    $authorUrl = $urlGenerator->generateAuthorUrl($author);
  47. UITBREIDING - URLGENERATOR class UrlGenerator { public function generateBlogUrl(??? $item)

    { /* ... do some magic ... */ } }
  48. PROBLEEM! Welke Visual Debt constraint toevoegen? public function generateBlogUrl(BlogPostInterface $item)

    public function generateBlogUrl(Slugable $item)
  49. PROBLEEM! Ofwel geen zekerheid over slug-functionaliteit Ofwel geen zekerheid over

    Blog-item
  50. OORZAAK class BlogPost implements BlogPostInterface, Slugable { public function getTitle()

    { /* ... */ } public function getContent() { /* ... */ } public function getAuthor() { /* ... */ } public function getSlug() { /* ... */ } public function getSlugableContent() { /* ... */ } }
  51. OPLOSSING??? Komt sebiet!

  52. Waddis SOLID? Efkes over de I Voorbeeldjes Nog bitje meer

    over de I
  53. INHERITANCE Overerving

  54. OBJECT CONTEXT class Circle extends Shape { /* ... */

    }
  55. QUIZ - KAN DIT? class Circle extends Shape, Oval {

    /* ... */ }
  56. MULTIPLE INHERITANCE Kan niet in PHP

  57. QUIZ - KAN DIT? interface Circle extends Shape { /*

    ... */ }
  58. QUIZ - KAN DIT? interface Circle extends Shape, Oval {

    /* ... */ }
  59. MULTIPLE INHERITANCE Kan niet in PHP in object context Kan

    wel in PHP: Multiple Interface Inheritance
  60. class BlogPost implements BlogPostInterface { /* ... */ } interface

    BlogPostInterface extends Slugable { /* ... */ }
  61. class UrlGenerator { public function generateBlogUrl(BlogPostInterface $item) { /* ...

    do some magic ... */ } }
  62. None
  63. PROBLEEM? NOPE!

  64. INTERFACE SEGREGATION PRINCIPLE Opsplitsen interfaces in meer specifiekere Dependen op

    specifiekste interface Meerdere interfaces implementeren = een oplossing Multiple interface inheritance = betere oplossing
  65. VRAGEN?

  66. HTTPS://JOIND.IN/TALK/22707