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

TwitterID:@rukiadia

May 28, 2021
Tweet

More Decks by TwitterID:@rukiadia

Other Decks in Programming

Transcript

  1. “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ͷ֓ཁ
  2. 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() ; } }
  3. 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() ; } } ४උɺޙ࢝຤ɺςετ࣮ߦͷλΠϛϯάΛ ϑϨʔϜϫʔΫଆ͕Ѳ͍ͬͯΔ
  4. 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('[email protected]', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); Ұݟ໰୊ͳͦ͞͏ʹݟ͑Δ͕ɾɾʁ
  5. 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('[email protected]', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); "XT4FT$MJFOUલఏͷ࣮૷ʹͳ͓ͬͯΓ ີ݁߹͕ൃੜ͍ͯ͠Δɻ
  6. 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(), '[email protected]', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); ࢖༻͢ΔαʔϏεʹؔ͢Δ৘ใΛ ֎͔Βࢦఆʢ֎෦͔Βͷґଘ஫ೖʣ
  7. ૹ৴ॲཧͷΠϯλϑΣʔε 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ͱͯ͠දݱ͠ɺ ґଘ౓ͷ௿ݮΛਤ͍ͬͯΔ
  8. 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();
  9. ڞ௨ΠϯλϑΣʔεͷ࣮૷ /* * * ಡΈࠐΈػೳͱදࣔػೳΛදݱ͢ΔΠϯλϑΣʔε * / interface Reader {

    public function read() ; public function display() ; } // ͜͜ʹ͸ϑΝΠϧܗࣜʹؔΘΒͣڞ௨ॲཧͱͳΔϝιουΛఆٛ
  10. 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ϑΝΠϧΛѻ͏ΫϥεͷΠϯλϑΣʔε࣮૷ จࣈίʔυͷΤϯίʔυɺྫ֎ॲཧ͸ল͍͍ͯ·͢ɻ
  11. '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) ; } } } ෼ذॲཧͰ൑அ͢Δͷ͸ ʮඞཁͳαϒΫϥε͕ͲΕͳͷ͔ʯͷΈ ݻ༗ͷॲཧ͸ αϒΫϥε಺ʹӅṭ͞Ε͍ͯΔ
  12. ΞϓϦέʔγϣϯଆ͔Βͷར༻ $filename = ‘users.xml’ ; // ͲͷαϒΫϥεΛ࢖༻͢Δ͔ɺFactory͕൑அ $readerFactory = new

    ReaderFactory() ; // FactoryΛݩʹඞཁͳReaderΫϥεͷΠϯελϯεΛੜ੒ $reader = $readerFactory->create($filename) ; // ΞϓϦέʔγϣϯ͸ɺΠϯλϑΣʔεΛݩʹ࣮ͨ͠૷(read, display)Λར༻͢ΔͷΈ $reader->read() ; $reader->display() ;
  13. 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(), '[email protected]', 'ςετϝʔϧͰ͢Ͷ') ; $emailSender->send(); &NBJM4FOEFSͷґଘΛ ΞϓϦέʔγϣϯଆ͔Β ஫ೖʢ*OKFDUJPOʣ͍ͯ͠Δ
  14. ࢀߟจݙ • ʰ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