「SOLIDの原則って何ですか?」って質問に答えたかった / What's SOLID principle

「SOLIDの原則って何ですか?」って質問に答えたかった / What's SOLID principle

書籍を読んでもわりと唐突に始まるSOLIDの原則
いざ自分が説明しようとすると結構難儀する代物だと思いますので、それっぽく説明ができるように調べたみたお話です

B989cc865d53d8e26fdadac99727113c?s=128

Satoshi Kawashima

January 30, 2019
Tweet

Transcript

  1. ઒ౡܛ(.01FQBCP *OD 1)1ษڧձ ʮ40-*%ͷݪଇͬͯԿͰ͔͢ʁʯ ࣭ͬͯ໰ʹ౴͔͑ͨͬͨ

  2. ΞϓϦέʔγϣϯΤϯδχΞ ઒ౡܛ!OB[POPIJUP 4)ࣄۀ෦+6(&.νʔϜ IUUQOB[POPIJUPKVHFNKQ

  3. 40-*%ͷݪଇ

  4. Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation

    principle Dependency inversion principle ୯Ұ੹೚ͷݪଇ ։์ด࠯ͷݪଇ Ϧείϑͷஔ׵ݪଇ ΠϯλʔϑΣʔε෼཭ͷݪଇ ґଘੑٯసͷݪଇ 40-*%ͷݪଇ
  5. %FWFMPQFS3PBENBQ  IUUQTHJUIVCDPNLBNSBOBINFETFEFWFMPQFSSPBENBQ

  6. %FWFMPQFS3PBENBQ  IUUQTHJUIVCDPNLBNSBOBINFETFEFWFMPQFSSPBENBQ

  7. Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation

    principle Dependency inversion principle ୯Ұ੹೚ͷݪଇ ։์ด࠯ͷݪଇ Ϧείϑͷஔ׵ݪଇ ΠϯλʔϑΣʔε෼཭ͷݪଇ ґଘੑٯసͷݪଇ 40-*%ͷݪଇ
  8. 40-*%ͷݪଇ Single responsibility principle Open/closed principle Liskov substitution principle Interface

    segregation principle Dependency inversion principle ୯Ұ੹೚ͷݪଇ ։์ด࠯ͷݪଇ Ϧείϑͷஔ׵ݪଇ ΠϯλʔϑΣʔε෼཭ͷݪଇ ґଘੑٯసͷݪଇ ͬͪ͜͡Όͳͯ͘
  9. Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation

    principle Dependency inversion principle ୯Ұ੹೚ͷݪଇ ։์ด࠯ͷݪଇ Ϧείϑͷஔ׵ݪଇ ΠϯλʔϑΣʔε෼཭ͷݪଇ ґଘੑٯసͷݪଇ 40-*%ͷݪଇ ͬͪ͜ʂʂʂʂ
  10. ͭͷݪଇηοτͷBDSPOZNʢ಄ࣈޠʣͰ͋Δ͜ͱҎ֎ Կ΋஌Βͳ͍

  11. ΘΓͱ౜ಥʹઆ໌͕࢝·Δ

  12. ಥવฉ͔Εͨ࣌ʹઆ໌͠Α͏ͱ͢Δͱ Ή͍ͣ

  13. ௐ΂ͨ ˞ॻ੶ΛಡΊ͹෼͔Δൣғ͸ࠓճѻ͍·ͤΜ

  14. ྺ࢙తܦҢʹ͍ͭͯ

  15. ࡞ͬͨʢൃදͨ͠ʣਓ 3PCFSU$.BSUJO ΞϝϦΧͷιϑτ΢ΣΞΤϯδχΞ 6ODMF#PCʢϘϒ͓͡͞ΜʣͷѪশͰ஌ΒΕΔ ͍͔ͭ͘ͷιϑτ΢ΣΞઃܭݪଇΛ։ൃͨ͠ IUUQTFOXJLJQFEJBPSHXJLJ3PCFSU@$@.BSUJO

  16. IUUQTHSPVQTHPPHMFDPNGPSVN IMFOUPQJDDPNQPCKFDU8*$1%D9".( ॳग़

  17. 5FO$PNNBOENFOUT ү໺ʂ001ͷ ेռ࡞Ζ͏ͥʂ λΠτϧ༁ɿ

  18.  ΫϥεϞδϡʔϧ͸֦ுʹରͯ͠։͖ɺमਖ਼ʹରͯ͠͸ด͡ͳ͚Ε͹ͳΒͳ͍ʢ#FSUSBOE.FZFSͷΦʔϓϯΫϩʔζυͷݪଇʣ  ೿ੜΫϥε͸جຊΫϥεͱަ׵ՄೳͰͳ͚Ε͹ͳΒͳ͍ʢ-JTLPWͷஔ׵ݪଇʣ  ৄࡉ͸ந৅ʹґଘ͠ͳ͚Ε͹ͳΒͣɺந৅͸ৄࡉʹґଘͯ͠͸ͳΒͳ͍ʢґଘੑٯసͷݪଇʣ  ࠶ར༻ͷཻ౓ͱϦϦʔεͷཻ౓͸ಉ͡ʹ͢΂͖  ಉ͡ཧ༝Ͱมߋ͞ΕΔΫϥε܈ΛҰͭͷίϯϙʔωϯτʹ·ͱΊΔ͜ͱ

     ίϯϙʔωϯτͷϦϦʔε͞ΕΔ୯Ґͱ࠶ར༻ͷ୯Ґ͸ಉ͡Ͱ͋Δ΂͖  ϦϦʔε͞Εͨίϯϙʔωϯτؒͷґଘؔ܎͸॥؀ͯ͠͸ͳΒͳ͍  ϦϦʔε͞Εͨίϯϙʔωϯτؒͷґଘͷํ޲͸ɺΑΓ҆ఆͨ͠ํ޲ʹ޲͍ͯͳ͚Ε͹ͳΒͳ͍  ΑΓ҆ఆͨ͠ίϯϙʔωϯτ͸ΑΓଟ͘ͷந৅ΫϥεͰߏ੒͞ΕΔ΂͖ ՄೳͳݶΓ࣮ূࡁΈͷύλʔϯΛ࢖༻ͯ͠ઃܭ্ͷ໰୊Λղܾ͢Δ ͭͷύϥμΠϜΛލ͙࣌͸ඞͣ྆ऀΛ෼͚ΔΠϯλʔϑΣʔεϨΠϠʔΛ੾Δ͜ͱ طʹ40-*%ͷݪଇͷݪܕ͕͋ͬͨ 3PCFSU$.BSUJOͷ౤ߘ ˢʂʁ
  19. ͳΔ΄Ͳɺ40-*%ͷݪଇ͸ 3PCFSU$.BSUJOݸਓͷ੒Ռ෺ͳΜͩͳʁ

  20. ͜ΕΒͷݪଇ͸ɺԿे೥ʹ΋ٴͿઌਓͷܦݧ ͷ஝ੵ͔Βੜ·Εͨιϑτ΢ΣΞ޻ֶͷ੒Ռ Ͱ͋Δɻ͜ΕΒͷ੒Ռ͸Ұਓͷ಄೴͔Βੜ· Εͨ΋ͷͰ͸ͳ͍ɻඇৗʹଟ͘ͷιϑτ΢Σ Ξ։ൃऀ΍ݚڀऀͷࢥࡧ΍࿦จΛूେ੒ͨ͠ ΋ͷͳͷͩɻ ΞδϟΠϧιϑτ΢ΣΞ։ൃͷԞٛ ୈ̎෦ɹΞδϟΠϧઃܭ

  21. ୯Ұ੹೚ͷݪଇ ։์ด࠯ͷݪଇ Ϧείϑͷஔ׵ݪଇ ΠϯλʔϑΣʔε෼཭ͷݪଇ ґଘੑٯసͷݪଇ ൃҊऀɿ#BSCBSB-JTLPW ൃҊऀɿ#FSUSBOE.FZFS ൃҊऀɿ3PCFSU$.BSUJO ൃҊऀɿ3PCFSU$.BSUJO ൃҊऀɿ3PCFSU$.BSUJO

    ˞5PN%F.BSDPͱ.FJMJS1BHF+POFTͷஶॻʹج͍͍ͮͯΔʁ αϒηοτ͸࡞͚ͬͨͲɺݸݸ͸ҧ͏
  22. ʮݪଇʯͬͯͳΜͩʁ

  23. ݪଇ IUUQTEJDUJPOBSZDBNCSJEHFPSHEJDUJPOBSZFOHMJTIQSJODJQMF zಓಙతͳϧʔϧ·ͨ͸ྑ͍ߦಈͷج४z 1SJODJQMF BNPSBMSVMFPSTUBOEBSEPGHPPECFIBWJPVS

  24. ྑ͍Ҿ༻ݩ͕ݟ͔ͭΒͳ͍

  25. ಠࣗղऍͰ͕͢ ݪଇͱ͸ ༗ҙͳ۩৅ͷཪʹ ීวతʹྲྀΕΔ๏ଇੑ Λநग़ͨ͠΋ͷ ͱɺଊ͓͑ͯΓ·͢

  26. ۩৅" ۩৅' ۩৅# ۩৅$ ۩৅% ۩৅& ᶃ๏ଇੑΛநग़ ᶄ๏ଇੑʹैͬͯ࡞੒ ݪଇ

  27. ۩৅" ۩৅' ۩৅# ۩৅$ ۩৅% ۩৅& ᶃ๏ଇੑΛநग़ ᶄ๏ଇੑʹैͬͯ࡞੒ ݪଇ ಉ༷ͷੑ࣭Λ͍࣋ͬͯΔ͜ͱ͕ظ଴Ͱ͖Δ

  28. ྑ͍ઃܭͷίʔυ" ʢͨͿΜྑ͍ઃܭͷʣ ίʔυ' ˞ྑ͍Ҿ༻ݩ͕ݟ͔ͭΒͳ͔ͬͨͷͰࢲݸਓͷਪଌͱͳΓ·͢ ྑ͍ઃܭͷίʔυ# ྑ͍ઃܭͷίʔυ$ ྑ͍ઃܭͷίʔυ% ྑ͍ઃܭͷίʔυ& ᶃ๏ଇੑΛநग़ ᶄ๏ଇੑʹଇͬͨίʔυͷ࡞੒

    40-*%ͷݪଇ
  29. ʮྑ͍ઃܭʯJT $MFBO"SDIJUFDUVSF ୈ***෦ɹઃܭͷݪଇ มߋʹڧ͍͜ͱ ཧղ͠΍͍͢͜ͱ ίϯϙʔωϯτͷج൫ͱͯ͠ɺଟ͘ͷιϑτ΢ΣΞγεςϜͰར༻Ͱ͖Δ͜ͱ

  30. 40-*%ͷݪଇ͸ զʑϓϩάϥϚ͕ ९क͢΂͖ϧʔϧͳͷ͔ʁ

  31. IUUQTTJUFTHPPHMFDPNTJUFVODMFCPCDPOTVMUJOHMMDHFUUJOHBTPMJETUBSU

  32. 40-*%ͷݪଇ͸ϧʔϧͰ͸͋Γ·ͤΜ͠ɺ ๏Ͱ͸͋Γ·ͤΜ͠ɺ׬શͳਅ࣮Ͱ΋͋Γ·ͤΜɻ ʮϦϯΰ೔ݸͰҩऀ஌Βͣʯͱ͍͏ݵ͕͋Γ·͢ɻ ͜Ε͸ྑ͍ݪଇͰ͋Γɺྑ͍ΞυόΠεͰ͕͢ɺ ͦΕ͸७ਮͳਅ࣮Ͱ͸ͳ͘ɺ·ͨϧʔϧͰ΋͋Γ·ͤΜɻ ʢ˞40-*%ͷݪଇ΋ͦ͏͍͏΋ͷͰ͋Δͱݴ͍͍ͨΜͩͱࢥ͏ʣ 5IF40-*%QSJODJQMFTBSFOPUSVMFT5IFZBSFOPUMBXT5IFZBSFOPUQFSGFDUUSVUIT5IFBSFTUBUFNFOUTPOUIFPSEFSPG l"OBQQMFBEBZLFFQTUIFEPDUPSBXBZz5IJTJTBHPPEQSJODJQMF JUJTHPPEBEWJDF CVUJU`TOPUBQVSFUSVUI

    OPSJTJUB SVMF
  33. cubby-holes 5IFQSJODJQMFTBSFNFOUBMDVCCZIPMFT5IFZHJWFBOBNFUPBDPODFQUTPUIBUZPVDBOUBMLBOESFBTPOBCPVUUIBU DPODFQU5IFZQSPWJEFBQMBDFUPIBOHUIFGFFMJOHTXFIBWFBCPVUHPPEBOECBEDPEF ͜ͷݪଇͨͪ͸৺ͷ੔ཧ୨ͳͷͰ͢ɻ ͜ΕΒ͸֓೦ʹର໊ͯ͠લ෇͚ͯ͘͠ΕΔͷͰɺ ͋ͳͨ͸֓೦ʹ͍ͭͯ࿩ͨ͠Γߟ͑ͨΓ͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΓ·͢ɻ ͜ΕΒ͸ࢲ͕ͨͪྑ͍ίʔυͱѱ͍ίʔυʹ͍͍ͭͯ࣋ͬͯΔ ײ৘ͷ੔ཧ৔ॴΛఏڙ͠·͢ɻ

  34. 5IFTFQSJODJQMFTBSFIFVSJTUJDT5IFZBSFDPNNPOTFOTFTPMVUJPOTUPDPNNPOQSPCMFNT5IFZBSFDPNNPOTFOTF EJTDJQMJOFTUIBUDBOIFMQZPVTUBZPVUPGUSPVCMF#VUMJLFBOZIFVSJTUJD UIFZBSFFNQJSJDBMJOOBUVSF5IFZIBWFCFFO PCTFSWFEUPXPSLJONBOZDBTFTCVUUIFSFJTOPQSPPGUIBUUIFZBMXBZTXPSL OPSBOZQSPPGUIBUUIFZTIPVMEBMXBZT CFGPMMPXFE ͜ΕΒͷݪଇ͸ώϡʔϦεςΟοΫͰ͢ɻ ͜ΕΒ͸Ұൠతͳ໰୊ʹର͢ΔৗࣝతͳղܾࡦͰ͋Γɺ ͋ͳ͕ͨτϥϒϧ͔Βൈ͚ग़͢ͷΛॿ͚Δ͜ͱ͕Ͱ͖ΔৗࣝతͳڭཆͰ͢ɻ ͔͠͠ɺώϡʔϦεςΟοΫͱಉ͡Α͏ʹɺ

    ͜ΕΒ͸ຊ࣭తʹܦݧଇʹجͮ͘΋ͷͰ͢ɻ ͜ΕΒͷݪଇ͕ଟ͘ͷ৔߹ʹػೳ͢Δ͜ͱ͕෼͔͍ͬͯ·͕͢ɺ ৗʹػೳ͢Δͱ͍͏ূڌ΋ɺ ͜ΕΒʹৗʹै͏΂͖Ͱ͋Δͱ͍͏ূڌ΋͋Γ·ͤΜɻ
  35. ϧʔϧͱ͍͏ΑΓɺ ࢦ਑ ͬͯ දݱͯ͠͠·ͬͨํ͕ޡղ͸গͳͦ͏ ΨΠυϥΠϯ

  36. ·ͱΊ w40-*%ͷݪଇ͸3PCFSU$.BSUJO͕·ͱΊͨ΋ͷ wྑ͍ઃܭͷ๏ଇੑΛநग़ͨ͠΋ͷͰɺݪଇΛҙࣝͨ͠ίʔυΛॻ͘͜ ͱʹΑͬͯྑ͍ઃܭͷίʔυʹͳΔ͜ͱ͕ظ଴Ͱ͖Δ wࣗ෼ͷߟ͑Λ੔ཧͨ͠Γɺઃܭͷྑ͠ѱ͠Λ൑அͨ͠ΓɺνʔϜͷڞ ௨ݴޠج൫ʹͳΔͳͲͷޮՌ΋ظ଴Ͱ͖Δ w໡໨తʹै͏΂͖ϧʔϧͱ͍͏΋ͷͰ͸ͳ͘ɺࢀߟʹ͢Δͱ޾ͤʹͳ ΔΨΠυϥΠϯఔ౓ͷظ଴ײͰࢀর͢Δͱྑͦ͞͏

  37. ·ͱΊ w40-*%ͷݪଇ͸3PCFSU$.BSUJO͕·ͱΊͨ΋ͷ wྑ͍ઃܭͷ๏ଇੑΛநग़ͨ͠΋ͷͰɺݪଇΛҙࣝͨ͠ίʔυΛॻ͘͜ ͱʹΑͬͯྑ͍ઃܭͷίʔυʹͳΔ͜ͱ͕ظ଴Ͱ͖Δ wࣗ෼ͷߟ͑Λ੔ཧͨ͠Γɺઃܭͷྑ͠ѱ͠Λ൑அͨ͠ΓɺνʔϜͷڞ ௨ݴޠج൫ʹͳΔͳͲͷޮՌ΋ظ଴Ͱ͖Δ w໡໨తʹै͏΂͖ϧʔϧͱ͍͏΋ͷͰ͸ͳ͘ɺࢀߟʹ͢Δͱ޾ͤʹͳ ΔΨΠυϥΠϯఔ౓ͷظ଴ײͰࢀর͢Δͱྑͦ͞͏ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠