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

オブジェクト指向のこころを学んだ話

tofu511
November 27, 2020

 オブジェクト指向のこころを学んだ話

tofu511

November 27, 2020
Tweet

More Decks by tofu511

Other Decks in Programming

Transcript

  1. ΦϒδΣΫτࢦ޲ҎલͷύϥμΠϜ:ػೳ෼ղ • σʔλϕʔε͔Β঎඼ͷϦετΛදࣔ͢ΔϓϩάϥϜ͕͋Δͱ͠·͢ • ΍Δ΂͖खॱΛચ͍ग़͠ฒ΂Δ 1. σʔλϕʔεʹ͋Δ঎඼Λݕࡧ͢Δ 2. ঎඼ϦετΛऔಘ͢Δ 3.

    ϦετΛԿ͔͠ΒͷϧʔϧͰฒ΂Δ 4. ը໘্ʹ঎඼ΛͦΕͧΕදࣔ͢Δ • ͜ͷΑ͏ʹ໰୊Λখ͞ͳػೳʹϒϨʔΫμ΢ϯͯ͠໰୊Λߏ੒͢ΔػೳΛચ͍ग़͢ ख๏͜ͱΛػೳ෼ղͱݺͿ
  2. ΦϒδΣΫτࢦ޲Ҏલͷ ύϥμΠϜ:มԽ΁ͷରԠ // ScalaͬΆ͍ٙࣅίʔυ object Main extends App { val

    product = getProductFromDatabase() product match { case Book => // දࣔϩδοΫ case Food => // do something // छྨ͕૿͑ͨΒ͜͜Λม͑ͳ͍ͱ ͍͚ͳ͍ } def getProductFromDatabase() = ??? //DB͔Β঎඼Λऔಘ͢Δ def displayProduct(product: Any) = ??? //දࣔ͢Δ } • ػೳ෼ղΞϓϩʔνͰมԽʹରԠ͢Δ৔߹ɺ௿ڽूɾີ݁߹ʹ ΑΔฐ֐ͱ͍͏ผͷ໰୊΋ൃੜ͢Δ • ӈ͸঎඼ΛDB͔Βऔಘ͠දࣔ͢Δ(ۃ୺ͳ)ྫ • ಛఆͷຊ΍৯඼Ͱ͸ผͷॲཧʹ͍ͨ͠ͱ͖ͲΜͲΜෳࡶʹͳͬ ͯ͠·͏
  3. ΦϒδΣΫτࢦ޲ͷ ύϥμΠϜ // ScalaͬΆ͍ٙࣅίʔυ trait Product { def display() =

    ??? // ঎඼Λදࣔ͢Δ } case class Book extends Product { // Bookݻ༗ͷॲཧΛҕৡ override def display() = ??? } case class Food extends Product { // Foodݻ༗ͷॲཧΛҕৡ override def display() = ??? } class ProductDatabase { def getProductFromDatabase(): Product = ??? //DB͔Β঎඼Λऔಘ͢Δ } object Main extends App { val product = new ProductDatabase().getProductsFromDataba se() product.display() } • ઌ΄Ͳͷ঎඼Λදࣔ͢ΔྫΛΦϒδΣΫτʹ෼ղ͠ ͯΈͨྫ • ந৅ΫϥεʹΑͬͯಉ֓͡೦Λแؚ͠ɺެ։Πϯ λʔϑΣʔεΛ௨ͯ͡ॲཧΛݺͼग़͢ • ΦϒδΣΫτݻ༗ͷॲཧ͸ϙϦϞʔϑΟζϜʹΑͬ ͯͦΕͧΕͷΦϒδΣΫτͷ੹຿ͱ࣮ͯ͠ߦ͞ΕΔ
  4. σβΠϯύλʔϯͱChristopher Alexander • 1990೥୅ॳ಄ɺAlexanderͷ੒ՌΛιϑτ΢ΣΞͷઃܭʹ࢖༻͠ɺग़དྷ্͕ͬͨ΋ͷ͕σβΠϯύ λʔϯ • GoFͷσβΠϯύλʔϯͳͲ • GoFͷ Design

    Patterns: Elements of Reusable Object-Oriented Software Ͱ͸ΦϒδΣΫτࢦ޲Λલ ఏͱͯ͠ύλʔϯʹج͍ͮͨΞϓϩʔνΛߦͳ͍ͬͯΔ • GoF͸༏ΕͨΦϒδΣΫτࢦ޲ઃܭΛੜΈग़ͨ͢ΊͷઓུΛ͍ࣔࠦͯ͠Δ • ΠϯλʔϑΣʔεΛ༻͍ͯઃܭ͢Δ • ΫϥεܧঝΑΓ΋ΦϒδΣΫτͷू໿Λଟ༻͢Δ • ྲྀಈతཁૉΛݟ͚ͭग़͠ΧϓηϧԽ͢Δ
  5. ڞ௨ੑ/Մมੑ෼ੳ • GoFͷ Design Patterns: Elements of Reusable Object-Oriented Softwareʹ͸ҎԼͷΑ͏ʹड़΂ΒΕ͍ͯΔ

    • ઃܭมߋΛڧ͍ΔՄೳੑͷ͋Δ΋ͷ͕Կ͔Λߟ͑ΔͷͰ͸ͳ͘ɺ࠶ ઃܭΛͤͣʹԿΛมߋՄೳʹ͢Δͷ͔Λߟ͑ΔͷͰ͢ɻ͜͜Ͱண໨ ͍ͯ͠Δͷ͸ɺྲྀಈత֓೦ͷΧϓηϧԽͰ͋Γɺଟ͘ͷσβΠϯύ λʔϯͷςʔϚͱ΋ͳ͍ͬͯΔ΋ͷͰ͢
  6. ڞ௨ੑ/Մมੑ෼ੳ • Կ͕ྲྀಈతཁૉͰͲͷΑ͏ʹมԽ͢Δ͔Λࣝผ͢ΔͨΊʹ࢖༻Ͱ͖Δͷ͕ڞ௨ੑ/ Մมੑ෼ੳ • James Coplienࢯ͕ఏএ • ڞ௨ੑͱ͸ʮશͯͷϑΝϛϦߏ੒һʹڞ௨͢Δੑ࣭ʯͷ͜ͱ •

    ϑΝϛϦߏ੒һͱ͸͋Δঢ়گ΍࣮ߦػೳʹݱΕΔޓ͍ʹؔ࿈ͷ͋Δཁૉͷ͜ͱ • Մมੑͱ͸ʮϑΝϛϦߏ੒һ͝ͱʹҟͳΔੑ࣭ʯͰՄมੑΛԿΒ͔ͷܗͰύϥ ϝʔλԽͰ͖Δͱ࠶ར༻ੑ͕૿͢
  7. ίʔυαϯϓϧ // Scalaͷίʔυྫ trait Shape { val drawing: Drawing def

    draw(): Unit } class Rectangle(override val drawing: Drawing) extends Shape { override def draw(): Unit = drawing.drawLine() } class Circle(override val drawing: Drawing) extends Shape { override def draw(): Unit = drawing.drawCircle() } trait Drawing { // ࣮ࡍ͸ඳըͷͨΊʹඞཁͳҾ਺ΛऔΔ def drawLine(): Unit def drawCircle(): Unit } class V1Drawing extends Drawing { override def drawLine(): Unit = println("V1 drawLine") override def drawCircle(): Unit = println("V1 drawCircle") } class V2Drawing extends Drawing { override def drawLine(): Unit = println("V2 drawLine") override def drawCircle(): Unit = println("V2 drawCircle") } object Client { def main(args: Array[String]): Unit = { val shapes = Seq( new Rectangle(new V1Drawing()), new Rectangle(new V2Drawing()), new Circle(new V1Drawing()), new Circle(new V2Drawing()) ) shapes.foreach(_.draw()) } }