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

Dependency Injectionとデザインパターン / understand_di_and_design_patterns

Dependency Injectionとデザインパターン / understand_di_and_design_patterns

PHPカンファレンス沖縄の発表資料です。
https://phpcon.okinawa.jp/

参考文献URL
・『Dependency Injection Principles, Practices, and Patterns』
https://www.amazon.com/dp/161729473X
・「InversionOfControl」
https://martinfowler.com/bliki/InversionOfControl.html
・「Inversion of Control Containers and the Dependency Injection pattern」
https://martinfowler.com/articles/injection.html
・『オブジェクト指向でなぜつくるのか 第3版』
https://www.nikkeibp.co.jp/atclpubmkt/book/21/S00180/
・『PHPによるデザインパターン入門』
https://www.shuwasystem.co.jp/book/4798015164.html
・『オブジェクト指向のこころ』
https://www.maruzen-publishing.co.jp/item/b294729.html

その他URL
・『Design Patterns 15 Years Later: An Interview with Erich Gamma, Richard Helm, and Ralph Johnson』Oct 22, 2009
https://www.informit.com/articles/article.aspx?p=1404056
・「GoF デザインパターン チートシート」
https://qiita.com/tanakahisateru/items/df03d2558f9499d1a64a
・「48. GoFデザインパターンとDI (前編) w/ twada」
https://open.spotify.com/episode/3WF6xxUOnHAQR0xt3gC2Rm

438799c739f93d00abc30fa2da33c543?s=128

TwitterID:@rukiadia

May 28, 2021
Tweet

Transcript

  1. %FQFOEFODZ*OKFDUJPOͱ σβΠϯύλʔϯ ʰ1)1$0/'0,*/"8"ʱ !SVLJBEJB 4IP:BNBEB

  2. ࣗݾ঺հ • ࢁాɹ঵ʢ΍·ͩɹ͠ΐ͏ʣ • ίωώτגࣜձࣾͰόοΫΤϯυΤϯδχΞΛ͍ͯ͠·͢ɻ • #CakePHP #TypeScript #React

  3. ຊ೔͓࿩͢Δ಺༰͸ͪ͜Β w %FQFOEFODZ*OKFDUJPOͷ֓ཁΛ஌Δ w σβΠϯύλʔϯΛ஌Δ w %FQFOEFODZ*OKFDUJPOͷσβΠϯύλʔϯʹ৮ΕΔ w ˞1)1ݻ༗ͷ࿩͸গͳΊͰ͢ Կ౓΋஻Δʹ͸গ͠௕͍ͷͰɺ

    ʮ%*ʯͱུͭͭ͠ਐΊ·͢ɻ
  4. ͔͜͜Β͕ຊฤ

  5. ʮ%FQFOEFODZ*OKFDUJPOͷ֓ཁʯ

  6. Dependency Injectionͷ֓ཁ ͦ΋ͦ΋%*ͱ͸Կͳͷ͔ʁ

  7. Dependency Injectionͷ֓ཁ %*ͱ͸ํ๏࿦Ͱ͢

  8. “Dependency Injection Principles, Practices, and Patterns” Part 1 Putting Dependency

    Injection on the map “Dependency Injection is a set of software design principles and patterns that enables you to develop loosely coupled code.” 8 Dependency Injectionͷ֓ཁ
  9. “Dependency Injection͸ɺૄ݁߹ͷίʔυΛ։ ൃ͢ΔͨΊͷιϑτ΢ΣΞઃܭݪଇ͓Αͼύ λʔϯͷηοτͰ͢” 9 Dependency Injectionͷ֓ཁ

  10. ͜͜·Ͱͷ·ͱΊ w อकੑͷߴ͍ίʔυͷ࣮ݱΛ໨తͱͨ͠ιϑτ΢ΣΞઃܭݪଇͱύ λʔϯͷηοτɻ w ιϑτ΢ΣΞઃܭͷʮͭͷݪଇʯͷͭʹؚ·ΕΔૄ݁߹ʢগͳ͍݁߹ʣ w ʮૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠σβΠϯύλʔϯʯͱ͍͏ͷ͕ద੾ͳද ݱͱݴ͑Δɻ w

    ˞༨ஊʰ%*1BUUFSOʱͱ͍͏ݺশ΋ଘࡏ͠·͢
  11. ࣍ʹ%*͕ੜ·ΕͨܦҢʹ͍ͭͯ ݟ͍͖ͯ·͠ΐ͏ Dependency Injectionͷ֓ཁ

  12. %*͕ੜ·ΕͨܦҢ

  13. %*ͷىݯ w ىݯ͸+BWBͷίϛϡχςΟ w ೥ʙ೥ࠒʹ.BSUJO'PXMFS͕ఆٛͨ͠ݴ༿ w ࢀߟIUUQTNBSUJOGPXMFSDPNBSUJDMFTJOKFDUJPOIUNM w ʮ*OWFSTJPOPG$POUSPMʢ*P$ʣʯͱ͍͏֓೦͕౰࣌ଘࡏ͓ͯ͠Γɺ ͦͷ֓೦Λ۩ମԽ͠ɺύλʔϯͷͭͱͯ͠ѻ͏ͨΊͷݴ༿͕࡞Γग़

    ͞Εͨɻ
  14. *P$ʢ੍ޚͷٯసʣͱ͸Կͳͷ͔ʁ Dependency Injectionͷ֓ཁ - DI͕ੜ·ΕͨܦҢ

  15. *P$ʢ੍ޚͷٯసʣ w ʰϋϦ΢ουͷݪଇʱͱ΋ݺ͹ΕΔੑ࣭ w ϓϩάϥϜʹ͓͍ͯɺʮݺͼग़͢ଆʯͱʮݺͼग़͞ΕΔଆʯ͕ैདྷ ͷϓϩάϥϜͱٯʹͳΔݱ৅Λࢦ͢ɻ w ޱޠతͳݴ͍ճ͠ʹ͢Δͱʮඞཁͳ࣌͸ͪ͜Β͔Βݺͼ·͢ʯͱ͍ ͏ҙຯ߹͍ʹͳΔɻ

  16. *P$ʢ੍ޚͷٯసʣ w ϑϨʔϜϫʔΫͷੑ࣭Λදݱ͢ΔͨΊʹ΋༻͍ΒΕΔɻ w ։ൃऀ͕ఆٛͨ͠ϝιου͕ݺͼग़͞ΕΔλΠϛϯά͸ϑϨʔϜ ϫʔΫଆʹҕͶΒΕ͍ͯΔɻ w ίϯτϩʔϧͷ࣋ͪओ͕։ൃऀ͔ΒϑϨʔϜϫʔΫʹٯస͍ͯ͠ Δɻ

  17. จষͷΈͩͱ఻ΘΓʹ͍͘ͷͰ ίʔυͰݟͯΈ·͠ΐ͏ Dependency Injectionͷ֓ཁ - DI͕ੜ·ΕͨܦҢ

  18. 1)16OJUͷίʔυྫ

  19. 1)16OJUͷςετίʔυ class ItemsTableTest extends TestCas e { public $fixtures =

    ['app.Items'] ; public function setUp( ) { parent::setUp() ; $this->Items = TableRegistry::getTableLocator()->get('Items') ; } public function tearDown( ) { TableRegistry::clear() ; } }
  20. 1)16OJUͷςετίʔυʢ੍ޚͷٯసʣ class ItemsTableTest extends TestCas e { public $fixtures =

    ['app.Items'] ; public function setUp( ) { parent::setUp() ; $this->Items = TableRegistry::getTableLocator()->get('Items') ; } public function tearDown( ) { TableRegistry::clear() ; } } ४උɺޙ࢝຤ɺςετ࣮ߦͷλΠϛϯάΛ ϑϨʔϜϫʔΫଆ͕Ѳ͍ͬͯΔ
  21. *P$ͱ%*ͷؔ܎ੑ w %*͸ݴ͍׵͑Ε͹ʮґଘؔ܎ʹର͢Δ੍ޚͷٯసʯͱදݱͰ͖Δɻ w ͔͠͠ɺʮ੍ޚͷٯసʯͱ͍͏දݱ͕แׅత͗͢ΔͷͰɺʮ%**P$ʯ ͱ͢Δͷ͸ਖ਼֬Ͱ͸ͳ͍ɻ w ʮґଘؔ܎ʹର͢Δ੍ޚͷٯసʯΛͭͷύλʔϯͱͯ͠ଊ͑Δͨ Ίɺ%FQFOEFODZ*OKFDUJPOͱ͍͏໋໊͕஀ੜͨ͠ɻ

  22. Inversion of Control Dependency Injection Dependency Injectionͷ֓ཁ - DI͕ੜ·ΕͨܦҢ

  23. จষͩͱ఻͑ʹ͍͘ͷͰ ίʔυͰޠΓ·͢ Dependency Injectionͷ֓ཁ

  24. ྫϝʔϧૹ৴ͷॲཧ

  25. class EmailSender { private $mailClient ; private $address ; private

    $text ; public function __construct($address, $text ) { // ʮAmazon Simple Email ServiceʯΛ࢖༻ͯ͠ϝʔϧૹ৴Λѻ͏Ϋϥε $this->mailClient = new AwsSesClient() ; $this->address = $address ; $this->text = $text ; } public function send( ) { $this->mailClient->send($this->address, $this->text) ; } } $emailSender = new EmailSender('test@example.com', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); Ұݟ໰୊ͳͦ͞͏ʹݟ͑Δ͕ɾɾʁ
  26. class EmailSender { private $mailClient ; private $address ; private

    $text ; public function __construct($address, $text ) { // ʮAmazon Simple Email ServiceʯΛ࢖༻ͯ͠ϝʔϧૹ৴Λѻ͏Ϋϥε $this->mailClient = new AwsSesClient() ; $this->address = $address ; $this->text = $text ; } public function send( ) { $this->mailClient->send($this->address, $this->text) ; } } $emailSender = new EmailSender('test@example.com', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); "XT4FT$MJFOUલఏͷ࣮૷ʹͳ͓ͬͯΓ ີ݁߹͕ൃੜ͍ͯ͠Δɻ
  27. &NBJM4FOEFSΫϥε͕࣋ͭ՝୊ w ಛఆͷαʔϏε΁ͷґଘ w αʔϏεΛมߋ͢Δཁ͕݅ग़͖ͯͨ৔߹ɺ&NBJM4FOEFSΛେ͖͘ॻ ͖׵͑Δඞཁ͕ग़ͯ͘Δɻ w ςελϏϦςΟͷ௿͞ w &NBJM4FOEFSΫϥεͷςετʹ"XT4FTΫϥε΋ඞཁʹͳΔɻ

  28. class EmailSender { private $mailClient ; private $address ; private

    $text ; public function __construct($client, $address, $text ) { $this->mailClient = $client; $this->address = $address ; $this->text = $text ; } public function send( ) { $this->mailClient->send($this->address, $this->text) ; } } $emailSender = new EmailSender(new AwsSesClient(), 'test@example.com', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); ࢖༻͢ΔαʔϏεʹؔ͢Δ৘ใΛ ֎͔Βࢦఆʢ֎෦͔Βͷґଘ஫ೖʣ
  29. ૹ৴ॲཧͷΠϯλϑΣʔε interface iEmailClient { public function send($address, $text) ; }

    class AwsSesClient implements iEmailClient { private $mailClient ; public function __construct( ) { $this->mailClient = new AwsSes() ; } public function send($address, $text){ } } &NBJM4FOEFS͕ґଘ͢Δ෦෼Λ JOUFSGBDFͱͯ͠දݱ͠ɺ ґଘ౓ͷ௿ݮΛਤ͍ͬͯΔ
  30. None
  31. None
  32. ※࢖͏αʔϏε͕มΘͬͯ΋ɺ͜͜Λࠩ͠ସ͑Ε͹OK

  33. &NBJM4FOEFSΫϥεʹର͢Δ%*ͷ࣮ફ w Ϋϥεࣗ਎͕ґଘઌΛܾఆͤͣɺ֎෦͔ΒґଘઌΛࢦఆ͍ͯ͠Δɻ w &NBJM4FOEFSࣗ਎͸ʮϝʔϧॲཧΛ࣮ߦ͢Δͷ͕ͲͷαʔϏεͳͷ ͔ʯΛ஌͍ͬͯΔඞཁ͕ͳ͘ͳͬͨɻ w ʢ෭࣍తͳޮՌͰ͸͋Δ͕ʣςετʹඞཁͳϞοΫΛ࡞੒͠΍͘͢ͳ ΓɺςελϏϦςΟ΋޲্͍ͯ͠Δɻ

  34. %*ͷ࿩Λ͜͜Ͱ۠੾Γ σβΠϯύλʔϯͷ࿩Λ͠·͢ Dependency Injectionͷ֓ཁ

  35. ʮσβΠϯύλʔϯΛ஌Δʯ

  36. օ͞Μ͸σβΠϯύλʔϯͱ͍͏ݴ༿Λ ͝ଘ஌Ͱ͠ΐ͏͔ʁ σβΠϯύλʔϯΛ஌Δ

  37. σβΠϯύλʔϯͱ͸ʁ w ػೳ֦ு΍࠶ར༻͕͠΍͍͢ιϑτ΢ΣΞΛ࡞ΔͨΊʹઌਓ͕޻෉͠ ͖ͯͨϊ΢ϋ΢ΛूΊͨ΋ͷɻ w యܕతͳղ๏΋వΊΒΕ͍ͯΔͷͰɺ࣮ࡍͷϓϩάϥϛϯά΍ઃܭ Λ͢Δ্Ͱ໾ʹཱͭखҾ͖ʹͳΔɻ

  38. σβΠϯύλʔϯͱ͍͏ݴ༿ͷ༝དྷ w ݩʑ͸ݐஙͷ෼໺Ͱੜ·Εͨݴ༿ w ݐ෺΍ࢢ֗ͷઃܭɾݐஙʹ͓͚Δ༷ʑͳτϨʔυΦϑ΍໰୊ɻͦͯ͠ɺ ͦΕʹର͢Δయܕతͳղܾํ๏ΛవΊͨॻ੶Ͱ͋Δʰύλϯɾϥϯ ήʔδʕ؀ڥઃܭͷखҾʱʢʣʹͯ࢖༻͞Εͨɻ w ˞ʰ1)1ʹΑΔσβΠϯύλʔϯೖ໳ʱ͔ΒҰ෦ൈਮ w

    ʰ%FTJHO1BUUFSOT&MFNFOUTPG3FVTBCMF0CKFDU0SJFOUFE 4PGUXBSFʱʢ˞௨শ(P'ຊʣʹΑΓɺʮσβΠϯύλʔϯʯͷݴ༿͕޿ ͘஌ΒΕΔ͜ͱʹͳͬͨɻ
  39. (P'ͷσβΠϯύλʔϯͱ͸ʁ w ʰ%FTJHO1BUUFSOT&MFNFOUTPG3FVTBCMF0CKFDU0SJFOUFE 4PGUXBSFʱͷதͰ঺հ͞Ε͍ͯΔͷσβΠϯύλʔϯɻ w ஶऀͰ͋Δ໊͕ʰ(BOHPG'PVSʢ(P'ʣʱͱݺ͹Ε͓ͯΓɺ൴Β ͕ൃදͨ͠σβΠϯύλʔϯ͕ʮ(P'ͷσβΠϯύλʔϯʯͱҰൠ తʹݺ͹ΕΔΑ͏ʹͳͬͨɻ w ͪͳΈʹॳ൛͕ൃߦ͞Εͨͷ͸ɺ೥݄೔ɻ

    w ˞ຊൃදͰ͸ɺͷσβΠϯύλʔϯΛશͯ঺հ͢Δ͜ͱ͸͠·ͤΜɻ
  40. σβΠϯύλʔϯΛ ࢖ͬͯΈΔ

  41. $47ɺ9.-ɺ+40/ϑΝΠϧΛ ಡΈࠐΜͰը໘දࣔ͢ΔॲཧΛ ߟ͑ͯΈ·͢ɻ σβΠϯύλʔϯΛ஌Δ

  42. class FileReader { private $filename ; function __construct($filename) { //

    ※ ֦ுࢠνΣοΫͷྫ֎ॲཧ͸ল͍͍ͯ·͢ $this->filename = $filename ; } function display() { if (stripos($this->filename, '.csv') !== false) { // CSVͷॲཧ } else if (stripos($this->filename, '.xml') !== false) { // XMLͷॲཧ } else if (stripos($this->filename, '.json') !== false) { // JSONͷॲཧ } } } $fileReader = new FileReader('sample.csv') ; $fileReader->display();
  43. ݱ࣌఺ͷ՝୊ w ڞ௨͢ΔՕॴ͸͋Γͭͭ΋গ͠ҟͳΔෳ਺ͷॲཧ͕ɺͭͷϝιου ಺Ͱ෼ذॲཧ͞Ε͍ͯΔɻ w ͔͠΋ɺ͓ޓ͍ͷॲཧʹ͸ؔ࿈ੑ͕ͳ͍ɻ w ʮڽू౓͕௿͍ʯͱදݱ͞ΕΔঢ়ଶɻ w ͍ͭͰʹݴ͏ͱɺEJTQMBZϝιουͷςετ΋͠ʹ͍͘ɻ

  44. (P'ͷσβΠϯύλʔϯ'BDUPSZ.FUIPE w ΦϒδΣΫτੜ੒࣌ͷΠϯλϑΣʔεͷΈΛఆٛ͠ɺͲͷΫϥεΛΠ ϯελϯεͱ͢Δ͔ΛαϒΫϥεʹ೚ͤΔύλʔϯͰ͢ɻ w ʮ'BDUPSZʯͱ͸ʮΫϥεͷΠϯελϯεΛੜ੒͢ΔαϒΫϥεʯ Λࢦ͍ͯ͠·͢ɻ w ࠓճ͸ɺϑΝΠϧܗࣜʹؔ܎ͳ͘ಉ༷ͷػೳΛఏڙ͢ΔΫϥεͷΠ ϯελϯεΛ࡞ΕΔΑ͏ʹ͠·͢ɻ

  45. 'BDUPSZ.FUIPEύλʔϯͷΫϥεਤ

  46. ڞ௨ΠϯλϑΣʔεͷ࣮૷ /* * * ಡΈࠐΈػೳͱදࣔػೳΛදݱ͢ΔΠϯλϑΣʔε * / interface Reader {

    public function read() ; public function display() ; } // ͜͜ʹ͸ϑΝΠϧܗࣜʹؔΘΒͣڞ௨ॲཧͱͳΔϝιουΛఆٛ
  47. class CSVReader extends Reader { private $filename ; private $file

    ; function __construct($filename) { $this->filename = $filename ; } function read() { $file = new SplFileObject($this->filename) ; $file->setFlags(SplFileObject::READ_CSV) ; $this->file = $file ; } function display() { foreach ($this->file as $line) { /* ߦ͝ͱʹॲཧ͍ͯ͘͠ */ } } } $47ϑΝΠϧΛѻ͏ΫϥεͷΠϯλϑΣʔε࣮૷ จࣈίʔυͷΤϯίʔυɺྫ֎ॲཧ͸ল͍͍ͯ·͢ɻ
  48. 'BDUPSZͷ࣮૷ class ReaderFactory { function create($filename) { return $this->createReader($filename) ;

    } /* * * ϑΝΠϧ֦ுࢠΛݩʹReaderΫϥεͷαϒΫϥεΛܾΊΔ * / private function createReader($filename) { if (stripos($this->filename, '.csv') !== false) { return new CSVReader($filename) ; } else if (stripos($this->filename, '.xml') !== false) { return new XMLReader($filename) ; } else if (stripos($this->filename, '.json') !== false) { return new JSONReader($filename) ; } } } ෼ذॲཧͰ൑அ͢Δͷ͸ ʮඞཁͳαϒΫϥε͕ͲΕͳͷ͔ʯͷΈ ݻ༗ͷॲཧ͸ αϒΫϥε಺ʹӅṭ͞Ε͍ͯΔ
  49. ΞϓϦέʔγϣϯଆ͔Βͷར༻ $filename = ‘users.xml’ ; // ͲͷαϒΫϥεΛ࢖༻͢Δ͔ɺFactory͕൑அ $readerFactory = new

    ReaderFactory() ; // FactoryΛݩʹඞཁͳReaderΫϥεͷΠϯελϯεΛੜ੒ $reader = $readerFactory->create($filename) ; // ΞϓϦέʔγϣϯ͸ɺΠϯλϑΣʔεΛݩʹ࣮ͨ͠૷(read, display)Λར༻͢ΔͷΈ $reader->read() ; $reader->display() ;
  50. 'BDUPSZ.FUIPEͰԿ͕վળ͞Εͨʁ w ϑΝΠϧ֦ுࢠ͝ͱͷॲཧΛαϒΫϥεʹవΊΔ͜ͱ͕Ͱ͖ͨɻ w ڽू౓ͷߴ͍ঢ়ଶʹͳ͓ͬͯΓɺςελϏϦςΟ΋޲্ɻ w ΞϓϦέʔγϣϯͰѻ͏֦ுࢠ͕มԽͯ͠΋ɺαϒΫϥεΛ௥Ճͯ͠ 'BDUPSZΛՃච͢Δ͚ͩͰࡁΉɻ

  51. 'BDUPSZ.FUIPEύλʔϯͷΫϥεਤʢ࠶ܝʣ ※ϑΥʔϚοτ͕มΘͬͯ΋ɺ͚ͩ͜͜มߋ͢Ε͹OK

  52. σβΠϯύλʔϯΛ஌ΔϝϦοτ w ઃܭͷࢦ਑ͱͳΔɻ w σβΠϯύλʔϯΛͦͷ··࢖ͬͯ΋ղܾͰ͖ͳ͍৔߹΋͋Δ͕ɺ ղܾʹ׆͔ͤΔՄೳੑ͸े෼͋Δɻ w ڞ௨ݴޠͱͯ͠༻͍Δɻ w ྫɿʮ'BDUPSZ.FUIPE͕ར༻Ͱ͖·ͤΜ͔ʁʯ

    w ύλʔϯ໊ΛೝࣝͰ͖Δਓಉ࢜ͰͳΒɺ͜Ε͚ͩͰ఻ΘΔɻ
  53. ͜͜Ͱ࠶౓ɺ%*ͷ࿩ʹ໭Γ·͢ɻ σβΠϯύλʔϯΛ஌Δ

  54. %FQFOEFODZ*OKFDUJPOͱ σβΠϯύλʔϯͷؔΘΓ

  55. %*͕ओ࣠ͳͷʹɺ σβΠϯύλʔϯʹ৮Εͨͷ͸Կނʁ Dependency InjectionͱσβΠϯύλʔϯͷؔΘΓ

  56. %*΋σβΠϯύλʔϯ w ʰ%FTJHO1BUUFSOTʱͰఆٛ͞ΕͨͷσβΠϯύλʔϯ΋ɺ೥݄͕ ܦͬͨ͜ͱͰҰ෦͕ݟ௚͞Εɺ%*΋ʰ%FTJHO1BUUFSOTʱͷҰ෦ʹ͠ ·ͨ͠ɻ w ʰ%FTJHO1BUUFSOT:FBST-BUFS"O*OUFSWJFXXJUI&SJDI (BNNB 3JDIBSE)FMN BOE3BMQI+PIOTPOʱ0DU

     w IUUQTXXXJOGPSNJUDPNBSUJDMFTBSUJDMFBTQY Q w σβΠϯύλʔϯΛ஌Δ͜ͱ͕ɺ%*΁ͷཧղʹ΋ܨ͕Δɻ
  57. $SFBUJPOBMʢੜ੒ʣΛ໨తͱͨ͠ύλʔϯ w ೥࣌఺ w 'BDUPSZ.FUIPEɺ"CTUSBDU'BDUPSZɺ#VJMEFSɺ1SPUPUZQFɺ 4JOHMFUPO w ೥ʹ࠶ఆٛ͞Εͨޙ w 'BDUPSZɺ#VJMEFSɺ1SPUPUZQFɺ%FQFOEFODZ*OKFDUJPO

    w ˞'BDUPSZ.FUIPE͕'BDUPSZͱ໊લΛม͑ͨɻ
  58. ࠷ޙʹɺ%*ͷύλʔϯʹ৮Ε͍͖ͯ·͢ Dependency InjectionͱσβΠϯύλʔϯͷؔΘΓ

  59. ʮ%FQFOEFODZ*OKFDUJPO ͷσβΠϯύλʔϯʹ৮ΕΔʯ

  60. %*ͷύλʔϯ w $POTUSVDUPS*OKFDUJPO w .FUIPE*OKFDUJPO w 1SPQFSUZ*OKFDUJPO w ˞4FUUFS*OKFDUJPOͱ΋ݺ͹Ε·͢ɻ ࠓճͷൃදͰ͸

    $POUSVDUPS*OKFDUJPOͷΈʹ৮Ε·͢ɻ
  61. $POUSVDUPS*OKFDUJPO w Ϋϥε͕ґଘؔ܎ͷΠϯελϯεΛඞཁͱ͍ͯ͠Δ৔߹ɺΫϥεͷί ϯετϥΫλͰґଘઌΛఏڙ͢Δ͜ͱͰɺকདྷతͳ࢖༻ͷͨΊͷࢀর Λอଘ͓ͯ͘͠ख๏ɻ w %*ͷجຊͱ΋ݴΘΕΔύλʔϯ จষ͚ͩͰ͸఻ΘΓʹ͍͘ͷͰ %*ͷઆ໌ͷ୊ࡐͱͨ͠ϝʔϧૹ৴ॲཧΛ ݩʹઆ໌͍͖ͯ͠·͢ɻ

  62. ઌఔͷϝʔϧૹ৴ॲཧΛ ࢥ͍ग़͍ͯͩ͘͠͞ Dependency InjectionͱσβΠϯύλʔϯͷؔΘΓ

  63. None
  64. ࣮͸ɺ͜͜Ͱ΍͍ͬͯΔͷ͕ ʮ$POTUSVDUPS*OKFDUJPOʯͰ͢

  65. class EmailSender { private $mailClient ; private $address ; private

    $text ; public function __construct($client, $address, $text ) { $this->mailClient = $client; $this->address = $address ; $this->text = $text ; } public function send( ) { $this->mailClient->send($this->address, $this->text) ; } } $emailSender = new EmailSender(new AwsSesClient(), 'test@example.com', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); &NBJM4FOEFSͷґଘΛ ΞϓϦέʔγϣϯଆ͔Β ஫ೖʢ*OKFDUJPOʣ͍ͯ͠Δ
  66. ϝʔϧૹ৴ॲཧͷ$POTUSVDUPS*OKFDUJPO w &NBJM4FOEFSΫϥε͕ඞཁͱ͍ͯ͠ΔґଘઌͷΠϯελϯεΛ֎෦͔ Βࢦఆ͢Δɻ w ґଘؔ܎ͷΠϯελϯε"XT4FT$MJFOUͷΠϯελϯε w &NBJM4FOEFSTFOEϝιουͰ࢖༻͢ΔͨΊʹίϯετϥΫλ಺Ͱࢀ রΛอ࣋͢Δɻ

  67. ࠓճͷ·ͱΊ

  68. ࠓճͷ·ͱΊ w ʮ%FQFOEFODZ*OKFDUJPOʯ͸ૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠ํ๏࿦Ͱ͋ ΓɺσβΠϯύλʔϯͰ͋Δɻ w ʮσβΠϯύλʔϯʯͱ͸ɺػೳ֦ு΍࠶ར༻͕͠΍͍͢ιϑτ΢Σ ΞΛ࡞ΔͨΊʹઌਓ͕޻෉͖ͯͨ͠ϊ΢ϋ΢ͷմͰɺΞϓϦέʔγϣ ϯઃܭͷࢦ਑ͱͳΔɻ w %*ύλʔϯͷجຊ͸ʮ$POTUSVDUPS*OKFDUJPOʯͰ͋Δɻ

  69. /FYU"DUJPO w ʮ(P'ͷσβΠϯύλʔϯʯͬͯ༗໊͚ͩͲɺࠓͰ΋࢖͍͚ͬͯΔ΋ ͷͳͷ͔ʁ w %*ίϯςφʢ-BSBWFMͰ͍͏ͱ͜ΖͷαʔϏείϯςφʣ͸%*Λͦͷ ··࢖͏৔߹ͱൺ΂ͯɺԿ͕ศརʹͳͬͨͷ͔ʁ ͜ͷลΓΛਂງΓͯ͠ΈΔͱ %*ͷߋͳΔཧղʹܨ͕Δ͔΋͠Ε·ͤΜɻ

  70. ༨ஊ w (P'ͷσβΠϯύλʔϯͷ঺հΛ͍ͯ͠Δ͚ͩͰ෼͕ա͗ڈΔͷͰɺ৮ΕΔͷ ͸͋͑ͯ߇͑Ίʹ͠·ͨ͠ɻ w खૣ͘၆ᛌ͍ͨ͠৔߹͸ɺͪ͜ΒΛݟΔͷ͕ྑ͍Ͱ͢ɻ w IUUQTRJJUBDPNUBOBLBIJTBUFSVJUFNTEGEGEBB w (P'σβΠϯύλʔϯͱ%*ͷؔ܎ΛΑΓ஌Γ͍ͨํ͸ͪ͜ΒͷϙουΩϟετ͕

    ͓͢͢ΊͰ͢ɻ w ʮ(P'σβΠϯύλʔϯͱ%*ʢલฤʣʯ w IUUQTPQFOTQPUJGZDPNFQJTPEF8'YY60O)"23YUH$3N
  71. ࢀߟจݙ • ʰDependency Injection Principles, Practices, and Patternsʱ • https://www.amazon.com/dp/161729473X

    • ʮInversionOfControlʯ • https://martinfowler.com/bliki/InversionOfControl.html • ʮInversion of Control Containers and the Dependency Injection patternʯ • https://martinfowler.com/articles/injection.html
  72. ࢀߟจݙ • ʰΦϒδΣΫτࢦ޲Ͱͳͥͭ͘Δͷ͔ ୈ̏൛ʱ • https://www.nikkeibp.co.jp/atclpubmkt/book/21/S00180/ • ʰPHPʹΑΔσβΠϯύλʔϯೖ໳ʱ • https://www.shuwasystem.co.jp/book/4798015164.html

    • ʰΦϒδΣΫτࢦ޲ͷ͜͜Ζʱ • https://www.maruzen-publishing.co.jp/item/b294729.html
  73. ൃදʹର͢ΔϑΟʔυόοΫ͸ େ׻ܴͰ͢ʂ

  74. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ