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

Scala初心者は◯◯◯にメソッドを生やしたいと考えた / Pre-ScalaMatsuri ...

Scala初心者は◯◯◯にメソッドを生やしたいと考えた / Pre-ScalaMatsuri 2020

2020年8月30日に開催されたプレScalaMatsuriのLightning Talk発表資料です
https://scalaconfjp.doorkeeper.jp/events/110045

集約のエンティティ

August 30, 2020
Tweet

More Decks by 集約のエンティティ

Other Decks in Programming

Transcript

  1. 2020.08.30 10 ར༻υϝΠϯ ੥ٻର৅ͱͳΔར༻ظؒΛந৅Խͨ͠ར༻ΦϒδΣΫτ 70 case class Availability ( startAt:

    LocalDate, // ར༻։࢝೔ endAt: LocalDate, // ར༻ऴྃ೔ billingDate: LocalDate, // ੥ٻ೔ status: AvailabilityStatus // εςʔλε )
  2. 2020.08.30 12 ܖ໿υϝΠϯ ܖ໿Λந৅Խͨ͠ܖ໿ΦϒδΣΫτ &OUJUZ ͸ར༻Λෳ਺࣋ͭ case class Contract (

    id: ContractId, // ܖ໿ID availabilities: Seq[Availability], // ར༻ status: ContractStatus, // εςʔλε pricing: …, // ྉۚઃఆ … )
  3. 2020.08.30 13 ະ੥ٻͷར༻ΛٻΊΔ ར༻ͷεςʔλε͕ະ੥ٻͳ΋ͷ case class Contract (…) { def

    unBilledAvailabilities: Seq[Availability] = availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻ }
  4. 2020.08.30 14 ࣍ճͷ੥ٻ೔ΛٻΊΔ ະ੥ٻͷར༻ͷ͏ͪɺ੥ٻ೔͕࠷΋͍ۙ΋ͷ case class Contract (…) { def

    nextBillingDate(today: LocalDate): LocalDate = availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻Ͱ .filter(_.billingDate > today) // ੥ٻ೔͕ࠓ೔ΑΓະདྷͰ .minBy(_.billingDate.toEpochDay) // ੥ٻ೔͕࠷΋աڈͷ΋ͷ .billingDate }
  5. 2020.08.30 15 ࠓ೔੥ٻ͢Δར༻ΛٻΊΔ ະ੥ٻͷར༻ͷ͏ͪɺ੥ٻ೔͕ࠓ೔ͷ΋ͷ case class Contract (…) { def

    shouldCreateBilling(today: LocalDate): Seq[Availability] = availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻Ͱ .filter(_.billingDate = today) // ੥ٻ೔͕ࠓ೔ }
  6. 2020.08.30 18 ϦϑΝΫλ ར༻͕ܖ໿಺ʹ͔͠ొ৔͠ͳ͍ͳΒ͜ΕͰ΋͍͍͕ʜ case class Contract (…) { def

    unBilledAvailabilities: Seq[Availability] = availabilities.filter(_.status = UnBilled) // ະ੥ٻͷར༻ def nextBillingDate(today: Localdate): LocalDate = unBilledAvailabilities .filter(_.billingDate > today) // ੥ٻ೔͕ࠓ೔ΑΓະདྷͰ .minBy(_.billingDate.toEpochDay) // ੥ٻ೔͕࠷΋աڈͷ΋ͷ .billingDate }
  7. 2020.08.30 19 ੥ٻΛൃੜͤ͞Δ ੥ٻίϯςΩετͰར༻Λ৮ͬͨΓ΋͢Δ case class Billing(…) object Billing {

    def createBilling(today: LocalDate, contract: Contract): Billing = { val availabilities = contract.shouldCreateBilling(today) val billing = ??? // TODO: ར༻Λ࢖ͬͯ੥ٻΛ࡞Δ } }
  8. 2020.08.30 32 ར༻ͷίϨΫγϣϯΦϒδΣΫτԽ ܖ໿͸ར༻ίϨΫγϣϯΦϒδΣΫτΛ࣋ͭΑ͏ʹͳΔ case class Availabilities( values: Seq[Availability] )

    case class Contract ( id: ContractId, availabilities: Availabilities, // ίϨΫγϣϯΦϒδΣΫτΛ࣋ͭ status: ContractStatus, pricing: …, )
  9. 2020.08.30 33 ະ੥ٻͷར༻ΛٻΊΔ ར༻ͷυϝΠϯ஌ࣝͱ࣮ͯ͠૷Ͱ͖Δ case class Availabilities (…) { def

    unBilledAvailabilities: Availabilities = Availabilities(availabilities.filter(_.status = UnBilled)) }
  10. 2020.08.30 34 ੥ٻΛൃੜͤ͞Δ ར༻ίϨΫγϣϯܕΛૢ࡞͢Ε͹Α͘ͳΔ case class Billing(…) object Billing {

    def createBilling(today: LocalDate, contract: Contract): Billing = { val availabilities: Availabilities = contract.shouldCreateBilling(today) // AvailabilitesܕΛฦ͢ val billing = ??? // TODO: ར༻Λ࢖ͬͯ੥ٻΛ࡞Δ } }
  11. 2020.08.30 36 5JQT ඪ४తͳૢ࡞͸USBJUͰܧঝͰ͖ΔΑ͏ʹ͓ͯ͘͠ͱศར trait class CustomCollection[A] { val values:

    Seq[A] def isEmpty: Boolean = values.isEmpty def nonEmpty: Boolean = values.nonEmpty def length: Boolean = values.length … }