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

ScalaによるDDDライクなプロダクト開発

 ScalaによるDDDライクなプロダクト開発

2016/5/23 モバイルファクトリーさんとの勉強会で話したスライド

Yoshinobu Wakamatsu

May 23, 2016
Tweet

More Decks by Yoshinobu Wakamatsu

Other Decks in Technology

Transcript

  1. ҰൠతͳϨΠϠʔԽΞʔΩςΫνϟ Infrastructure Layer UI & Presenta1on Layer Applica1on Layer Domain

    Layer υϝΠϯ૚͕ΠϯϑϥετϥΫνϟ૚ ʢ۩ମతͳ࣮૷ٕज़ʣʹґଘͯ͠͠·͍ͬͯΔ
  2. ࠾༻ͨ͠ΞʔΩςΫνϟ Domain Layer Infrastructure Layer UI & Presenta1on Layer Applica1on

    Layer υϝΠϯ૚Λ࠷ԼҐͷϨΠϠʔ΁ ΠϯϑϥετϥΫνϟ૚ͷར༻͕ඞཁͳՕॴ͸ ͦͷΠϯλʔϑΣΠεΛఏڙ
  3. ࠾༻ͨ͠ΞʔΩςΫνϟ Domain Layer Infrastructure Layer UI & Presenta1on Layer Applica1on

    Layer ΠϯϑϥετϥΫνϟ૚͸࠷্ҐͷϨΠϠʔʹ͠ ԼҐϨΠϠʔͷΠϯλʔϑΣΠεΛ࣮૷
  4. 4DBMBʹΑΔ੩త%* trait RepositoryRegistry { val fooRepository: Repo } // fooRepositoryΛ࢖͏Ϋϥε

    trait BarService { this: RepositoryRegistry => def doSomething = { fooRepository.fetch } } // ࣮૷Λ࣋ͭτϨΠτ trait RepositoryRegistryImpl { // Repo͸࣮૷ val fooRepository = Repo } // ࣮૷Λಋೖ class BerServiceImpl extends RepositoryRegistryImpl 
  5. 4DBMBʹΑΔ੩త%* trait RepositoryRegistry { val fooRepository: Repo } // fooRepositoryΛ࢖͏Ϋϥε

    trait BarService { this: RepositoryRegistry => def doSomething = { fooRepository.fetch } } // ࣮૷Λ࣋ͭτϨΠτ trait RepositoryRegistryImpl { // Repo͸࣮૷ val fooRepository = Repo } // ࣮૷Λಋೖ class BerServiceImpl extends RepositoryRegistryImpl  ͜Ε͸ͨͩͷΠϯλʔϑΣΠεʢUSBJUʣ
  6. 4DBMBʹΑΔ੩త%* trait RepositoryRegistry { val fooRepository: Repo } // fooRepositoryΛ࢖͏Ϋϥε

    trait BarService { this: RepositoryRegistry => def doSomething = { fooRepository.fetch } } // ࣮૷Λ࣋ͭτϨΠτ trait RepositoryRegistryImpl { // Repo͸࣮૷ val fooRepository = Repo } // ࣮૷Λಋೖ class BerServiceImpl extends RepositoryRegistryImpl  ࣗ෼ܕΞϊςʔγϣϯͰUSBJU΁ͷґଘΛએݴ
  7. 4DBMBʹΑΔ੩త%* trait RepositoryRegistry { val fooRepository: Repo } // fooRepositoryΛ࢖͏Ϋϥε

    trait BarService { this: RepositoryRegistry => def doSomething = { fooRepository.fetch } } // ࣮૷Λ࣋ͭτϨΠτ trait RepositoryRegistryImpl { // Repo͸࣮૷ val fooRepository = Repo } // ࣮૷Λಋೖ class BerServiceImpl extends RepositoryRegistryImpl  ͜͜Ͱ۩ମతͳ࣮૷Λ༩͍͑ͯΔ %* 
  8. 4DBMBʹΑΔ੩త%* trait RepositoryRegistry { val fooRepository: Repo } // fooRepositoryΛ࢖͏Ϋϥε

    trait BarService { this: RepositoryRegistry => def doSomething = { fooRepository.fetch } } // ࣮૷Λ࣋ͭτϨΠτ trait RepositoryRegistryImpl { // Repo͸࣮૷ val fooRepository = Repo } // ࣮૷Λಋೖ class BerServiceImpl extends RepositoryRegistryImpl  ࣮૷Λಋೖ͢Δલͷஈ֊Ͱ ґଘΛએݴͨ͠USBJUͷϝιου͕࢖͑Δ
  9. ྫɿ7BMVF0CKFDUͷ࣮૷ •  7BMVF0CKFDU –  ஋Λදݱ͢ΔΦϒδΣΫτ –  ஋ࣗମʹυϝΠϯ্ͷҙຯΛ࣋ͨͤΔ •  ྫɿՁ֨ʢ1SJDFʣ – 

    ʮԁʯʹؚ·ΕΔཁૉ͸ʮʯͱʮԁʯ –  1SJDFͷϞσϧ͸࣍ͷཁૉΛ࣋ͭ •  WBMVF •  DVSSFODZ+1:
  10. +BWBʹΑΔ1SJDFΫϥε public class Price { public int value; // Ձ֨ͷ஋

    public Currency currency; // ௨՟ Price(int value, Currency currency) { this.value = value; this.currency = currency; } } // PriceΦϒδΣΫτͷੜ੒ new Price(100, new Currency(JPY))
  11. 4DBMBʹΑΔ1SJDFΫϥε case class Price(value: Int, currency: Currency) // case class

    ʹΑΓ্هͷҰߦͰදݱग़དྷΔ // ͜Ε͚ͩͰ৽͍͠ΦϒδΣΫτΛੜ੒Մೳ Price(100, JPY)
  12. 4DBMBʹΑΔ1SJDFΫϥε case class Price(value: Int, currency: Currency) // case class

    ʹΑΓ্هͷҰߦͰදݱग़དྷΔ // ͜Ε͚ͩͰ৽͍͠ΦϒδΣΫτΛੜ੒Մೳ Price(100, JPY) ϞσϧԽ͢Δ࣮૷ίετ͕ͱͯ΋খ͍͞
  13. ܕύϥϝʔλʹΑΔදݱ   trait Tax case object IncludingTax extends Tax

    // ੫ࠐ case object ExcludingTax extends Tax // ੫ൈ // ܕύϥϝʔλʢδΣωϦοΫʣʹΑͬͯ΋දݱՄೳ // A͸Taxͷαϒܕ case class Price[A <: Tax](value: Int, currency: Currency) // 100ԁʢ੫ࠐʣͷදݱ Price[IncludingTax](100, JPY)
  14. ܕύϥϝʔλʹΑΔදݱ   trait Tax case object IncludingTax extends Tax

    // ੫ࠐ case object ExcludingTax extends Tax // ੫ൈ // ܕύϥϝʔλʢδΣωϦοΫʣʹΑͬͯ΋දݱՄೳ // A͸Taxͷαϒܕ case class Price[A <: Tax](value: Int, currency: Currency) // 100ԁʢ੫ࠐʣͷදݱ Price[IncludingTax](100, JPY) ੫ൈɾ੫ࠐΛܕύϥϝʔλʹΑͬͯදݱ
  15. ܕύϥϝʔλʹΑΔදݱ   // ঎඼ͷՁ֨͸੫ࠐͰ͋Δ͜ͱ͕ܕͰදݱग़དྷΔ case class Product( name: String,

    price: Price[IncludingTax] ) // ঎඼ͷදݱྫ Product(“ίοϓຊ”, Price[IncludingTax](4968, JPY)) ͜ͷΑ͏ͳܗͰɺϞσϧΛ؆͔ܿͭܕ҆શʹ ίʔυʹམͱ͍ͯ͘͜͠ͱ͕Մೳ