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

Mobile Programming Protocol

Mobile Programming Protocol

Mobile Programming の授業で使うスライドです

yuichiro_takahashi

January 21, 2019
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. ςΩετ PROTOCOL(ϓϩτίϧ)ͱ͸ʁ ▸ Protocol͸࣮૷ͷͳ͍ClassͷΑ͏ͳ΋ͷͰ͢ ▸ Class ΍ Struct ͱಉ͘͡ ϝιου΍ϓϩύςΟΛఆٛ͢Δ͜ͱ͕Ͱ͖·͕͢۩ମత

    ͳ࣮૷͸͠·ͤΜ ▸ ଞͷClass΍Structʹܧঝͤͯ͞ɺϝιου΍ϓϩύςΟͷ࣮૷Λ͠·͢ ▸ جຊతʹProtocolʹఆٛ͞Ε͍ͯΔϝιου΍ϓϩύςΟ͸શ࣮ͯ૷͢Δඞཁ͕͋Γ ·͢ ▸ ProtocolΛClass΍Structʹܧঝͤ͞Δ͜ͱΛϓϩτίϧΛ࠾༻(adopt)ͤ͞Δͱݴ͍ ·͢ ▸ Ұൠ໊ࢺͱͯ͠௚༁͢Δͱ ن໿ ͱ͍͏ҙຯʹͳΓ·͢
  2. ςΩετ ࣮ࡍͷίʔυ protocol Pokemon { var name: String { get

    } var ability: Ability { get } var level: Int { get set } var health: Int { get set } func displayStatus() func doAbility<T: Pokemon>(to pokemon: T) } Protocol Ωʔϫʔυ͔Β࢝·Δ
  3. ςΩετ ࣮ࡍͷίʔυ protocol Pokemon { var name: String { get

    } var ability: Ability { get } var level: Int { get set } var health: Int { get set } func displayStatus() func doAbility<T: Pokemon>(to pokemon: T) } Protocol Ωʔϫʔυ͔Β࢝·Δ ͜͜͸ඞͣvarͰఆٛ͢Δ
  4. ςΩετ ࣮ࡍͷίʔυ protocol Pokemon { var name: String { get

    } var ability: Ability { get } var level: Int { get set } var health: Int { get set } func displayStatus() func doAbility<T: Pokemon>(to pokemon: T) } Protocol Ωʔϫʔυ͔Β࢝·Δ ͜͜͸ඞͣvarͰఆٛ͢Δ ۩ମతͳ஋Λࢦఆ͢Δ୅ΘΓʹ ಡΈॻ͖ͷઃఆΛ͢Δ ͜Ε͸ get ͚ͩͳͷͰಡΈࠐΈઐ༻
  5. ςΩετ ࣮ࡍͷίʔυ protocol Pokemon { var name: String { get

    } var ability: Ability { get } var level: Int { get set } var health: Int { get set } func displayStatus() func doAbility<T: Pokemon>(to pokemon: T) } Protocol Ωʔϫʔυ͔Β࢝·Δ ͜͜͸ඞͣvarͰఆٛ͢Δ ۩ମతͳ஋Λࢦఆ͢Δ୅ΘΓʹ ಡΈॻ͖ͷઃఆΛ͢Δ ͜Ε͸ get ͚ͩͳͷͰಡΈࠐΈઐ༻ ͪ͜Β͸ get set ྆ํͷهड़͕͋ΔͷͰ ಡΈॻ͖Մೳ
  6. ςΩετ ࣮ࡍͷίʔυ(ఆٛ) protocol Pokemon { var name: String { get

    } var ability: Ability { get } var level: Int { get set } var health: Int { get set } func displayStatus() func doAbility<T: Pokemon>(to pokemon: T) } Protocol Ωʔϫʔυ͔Β࢝·Δ ͜͜͸ඞͣvarͰఆٛ͢Δ ۩ମతͳ஋Λࢦఆ͢Δ୅ΘΓʹ ಡΈॻ͖ͷઃఆΛ͢Δ ͜Ε͸ get ͚ͩͳͷͰಡΈࠐΈઐ༻ ͪ͜Β͸ get set ྆ํͷهड़͕͋ΔͷͰ ಡΈॻ͖Մೳ ϝιου͸໊લͱҾ਺ɺ໭Γ஋͚ͩΛهड़͠۩ମతͳ࣮૷͸ॻ͔ͳ͍
  7. ςΩετ ࣮ࡍͷίʔυ(࠾༻) struct Pikachu: Pokemon { let name: String let

    ability: Ability var level: Int var health: Int func displayStatus() { print("name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } func doAbility<T>(to pokemon: T) where T : Pokemon { // to be continue... } } ࠾༻͍ͨ͠ProtocolΛࢦఆ͢Δ
  8. ςΩετ ࣮ࡍͷίʔυ(࠾༻) struct Pikachu: Pokemon { let name: String let

    ability: Ability var level: Int var health: Int func displayStatus() { print("name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } func doAbility<T>(to pokemon: T) where T : Pokemon { // to be continue... } } ࠾༻͍ͨ͠ProtocolΛࢦఆ͢Δ ͜ͷೋͭ͸ఆٛͷ࣌ʹ get ͚ͩͩͬͨͷͰ let Ͱఆ਺ͱͯ͠ఆٛ(Read only)
  9. ςΩετ ࣮ࡍͷίʔυ(࠾༻) struct Pikachu: Pokemon { let name: String let

    ability: Ability var level: Int var health: Int func displayStatus() { print("name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } func doAbility<T>(to pokemon: T) where T : Pokemon { // to be continue... } } ࠾༻͍ͨ͠ProtocolΛࢦఆ͢Δ ͜ͷೋͭ͸ఆٛͷ࣌ʹ get ͚ͩͩͬͨͷͰ let Ͱఆ਺ͱͯ͠ఆٛ(Read only) ͜ͷೋͭ͸ఆٛͷ࣌ʹ get set ྆ํࢦఆͨ͠ͷͰ var Ͱม਺ͱͯ͠ఆٛ(Read/Write)
  10. ςΩετ ࣮ࡍͷίʔυ(࠾༻) struct Pikachu: Pokemon { let name: String let

    ability: Ability var level: Int var health: Int func displayStatus() { print("name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } func doAbility<T>(to pokemon: T) where T : Pokemon { // to be continue... } } ࠾༻͍ͨ͠ProtocolΛࢦఆ͢Δ ͜ͷೋͭ͸ఆٛͷ࣌ʹ get ͚ͩͩͬͨͷͰ let Ͱఆ਺ͱͯ͠ఆٛ(Read only) ͜ͷೋͭ͸ఆٛͷ࣌ʹ get set ྆ํࢦఆͨ͠ͷͰ var Ͱม਺ͱͯ͠ఆٛ(Read/Write) ϝιου͸ఆٛͯ͋͠ΔҾ਺໭Γ஋Λهड़ͯ͠த਎Λ࣮૷͢Δ
  11. ςΩετ ࣮ࡍͷίʔυ(·ͱΊ) // Protocolఆٛ protocol Pokemon { var name: String

    { get } var ability: Ability { get } var level: Int { get set } var health: Int { get set } func displayStatus() func doAbility<T: Pokemon>(to pokemon: T) } // Protocol࠾༻ struct Pikachu: Pokemon { let name: String let ability: Ability var level: Int var health: Int func displayStatus() { print("name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } func doAbility<T>(to pokemon: T) where T : Pokemon { // to be continue... } }
  12. ςΩετ ͍ͭ࢖͏Μʁ ▸ ͬ͘͟Γ෼͚ͯ ▸ 1. ಛఆͷػೳΛఏڙͤ͞ΔͨΊ ▸ 2. Delegate

    PatternΛ࣮૷͢ΔͨΊ ▸ 3. ॲཧͷڞ௨ԽΛߦ͏ͨΊ ▸ ͷࡾͭʹͳΔͱࢥ͍·͢
  13. ςΩετ ػೳΛఏڙͤ͞Δʁ ▸ ͜͜Ͱݴ͏ػೳͱ͸ ▸ printؔ਺Ͱ಺༰Λग़ྗ͢Δ͜ͱ͕Ͱ͖Δͱ͔ ▸ ൺֱΛ͢Δ͜ͱ͕Ͱ͖Δ ▸ ͱݴ͏ҙຯʹͳΓ·͢

    ▸ લऀ͸ Printable, ޙऀ͸ Comparableͱ͍͏໊લͷProtocolͱͯ͠ఆٛ͞Ε ͍ͯ·͢ ▸ ͭ·Γ࠾༻͍ͯ͠ΔProtocolΛݟΕ͹ͲΜͳػೳΛ͍࣋ͬͯΔ͔͕Θ͔Γ ·͢
  14. ςΩετ DELEGATE PATTERNʁ ▸ Delegate Pattern ͸ϓϩάϥϜͰ࢖༻͢ΔσβΠϯύλʔϯ ͷҰͭͰ͢ ▸ ୺తʹݴ͏ͱ͋ΔObject͕ߦ͏΂͖ॲཧΛผͷObjectʹݞ

    ୅ΘΓͤ͞Δ͜ͱͰ͢ ▸ ݞ୅ΘΓ͢ΔΦϒδΣΫτΛ Delegate ͱݺͼ·͢ ▸ ͪͳΈʹ Delegate ͸Ұൠ໊ࢺΛ௚༁͢Δͱ ҕৡ ͱͳΓ· ͢
  15. ςΩετ PROTOCOL·ͱΊ ▸ Protocol͸࣮૷ͷͳ͍ClassͷΑ͏ͳ΋ͷ ▸ Class ΍ Struct ͱಉ͘͡ ϝιου΍ϓϩύςΟΛఆٛ͢Δ͜ͱ͕Ͱ

    ͖Δ͕۩ମతͳ࣮૷͸ͤͣɺ࠾༻ͤͨ͞ Class ΍ Struct Ͱ࣮૷͢Δ ▸ جຊతʹProtocolʹఆٛ͞Ε͍ͯΔϝιου΍ϓϩύςΟ͸શ࣮ͯ ૷͢Δඞཁ͕͋Δ ▸ Protocol͸ಛఆͷػೳΛఏڙͤͨ͞Γɺ Delegate PatternΛ࣮૷͢ ΔͨΊʹ࢖͏