責務範囲を意識するというお話

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
October 03, 2017

 責務範囲を意識するというお話

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

October 03, 2017
Tweet

Transcript

  1. ੹຿ൣғΛҙࣝ͢Δͱ͍͏͓࿩ for Զίϯ7PM%BZ

  2. override init() { super.init() emplyedBy = "YUMEMI Inc." job =

    "iOS Developer" twitter = "@lovee" qiita = "lovee" github = "el-hoshino" additionalInfo = "స৬͠·ͨ͠ʂ" class Speaker: Developer { } }
  3. .PEFMɺ7JFXͱ$POUSPMMFS Λ࡞Ε͹੹຿͕෼͚ΒΕͨͬ ͯ͜ͱʹͳΔΜͰ͔͢ʁ

  4. .PEFMɺ7JFXͱ$POUSPMMFSΛ࡞Ε͹੹຿ ͕෼͚ΒΕͨͬͯ͜ͱʹͳΔΜͰ͔͢ʁ ͦ͏ͱ͸ݶΒͳ͍ ͦ΋ͦ΋ʮ࡞ͬͨʯͱ ʮਖ਼͘͠࡞ͬͨʯ͸
 ผ֓೦

  5. ຊ౰ʹ੹຿෼͚ΒΕͯΔͷ͔ʁ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad()

    view.backgroundColor = UIColor(hue: 0.5, saturation: 1, brightness: 0.9, alpha: 1) } } $POUSPMMFS͕ϏδϡΞϧΛ ௚઀ઃఆ͍ͯ͠Δ!
  6. ઃܭͱͦΕΒͷ੹຿ʢ௒ͬ͘͟Γʣ ‣ .7$ ‣ .PEFMɿϏδωεϩδοΫ ‣ 7JFXɿը໘ग़ྗ ‣ $POUSPMMFSɿϢʔβೖྗ ‣

    .77. ‣ .PEFMɿϏδωεϩδοΫ ‣ 7JFXɿը໘ೖग़ྗ ‣ 7JFX.PEFMɿσʔλόΠϯσΟϯά ‣ FUD
  7. ઃܭͱͦΕΒͷ੹຿ʢ௒ͬ͘͟Γʣ ‣ .7$ ‣ 7JFXɿը໘ग़ྗ ‣ .77. ‣ 7JFXɿը໘ೖग़ྗ ۩ମతͳϏδϡΞϧදݱ͸

    7JFXͷ໾ׂ
  8. ຊ౰ʹ੹຿෼͚ΒΕͯΔͷ͔ʁ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad()

    view.backgroundColor = UIColor(hue: 0.5, saturation: 1, brightness: 0.9, alpha: 1) } } $POUSPMMFS͕ϏδϡΞϧΛ ௚઀ઃఆ͍ͯ͠Δ!
  9. ຊ౰͸͜͏͢΂͖͡Όͳ͍ͷ͔ʁ class ViewController: UIViewController { private lazy var _view =

    View() override func loadView() { self.view = _view } override func viewDidLoad() { super.viewDidLoad() _view.setupVisual() } } class View: UIView { func setupVisual() { self.backgroundColor = UIColor(hue: 0.5, saturation: 1, brightness: 0.9, alpha: 1) } } $POUSPMMFS͕ϏδϡΞϧΛ ௚઀ઃఆ͍ͯ͠ͳ͍" ϏδϡΞϧͷઃఆ͸શͯ
 Ϗϡʔʹू໿͞ΕΔ
  10. ੹຿Λҙࣝ͢Δͱݴ͏ͷ͸
 7JFX΍.PEFMͱͯ͠ͷ໾ׂΛ
 ҙࣝ͢Δ͜ͱ

  11. ੹຿Λҙࣝ͢Δͱݴ͏ͷ͸
 7JFX΍.PEFMͱͯ͠ͷ໾ׂΛ
 ҙࣝ͢Δ͜ͱ ͚ͩͰ͸ͳ͍

  12. https://qiita.com/lovee/items/555a5b76097347aa2367

  13. ंͷΨεܽ໰୊

  14. ंͷΨεܽ໰୊ class Person { var car: Car? func buyCar(_ car:

    Car) { self.car = car car.owner = self } } class Car { let model: String var owner: Person? init(_ model: String) { self.model = model } } let maki = Person() let ferrari458Italia = Car("458Italia") maki.buyCar(ferrari458Italia)
  15. ंͷΨεܽ໰୊ Ψεܽ څ༉͠ʹߦ͘ څ༉

  16. ंͷΨεܽ໰୊ class Person { var car: Car? func buyCar(_ car:

    Car) { self.car = car car.owner = self } } class Car { let model: String var owner: Person? init(_ model: String) { self.model = model } func outOfGasoline() { // ??? } } let maki = Person() let ferrari458Italia = Car("458Italia") maki.buyCar(ferrari458Italia) maki.car?.outOfGasoline() // ???
  17. ंͷΨεܽ໰୊ Ψεܽ څ༉͠ʹߦ͘ څ༉ - ࠷دΓͷΨιϦϯελϯυ͕Ͳ͜ʹ͋ͬͯͲ͏ߦ͔͘ʁ - څ༉ελοϑʹͲ͏఻͑Δ͔ʁ - ࢧ෷͍͸Ͳ͏͢Δ͔ʁ

    - FUD ͜ΕΒͷ໰୊͸
 ॴ༗ऀ͕ղܾ͢Δ
 ඞཁ͕͋Δʂ
  18. ंͷΨεܽ໰୊ class Person { var car: Car? func buyCar(_ car:

    Car) { self.car = car car.owner = self } func buyGasoline() { print("Buy some gasoline!") } } class Car { let model: String var owner: Person? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner?.buyGasoline() } } let maki = Person() let ferrari458Italia = Car("458Italia") maki.buyCar(ferrari458Italia) maki.car?.outOfGasoline() // Buy some gasoline!
  19. ंͷΨεܽ໰୊ Ψεܽ ॴ༗ऀʹ
 څ༉ͤ͞Δ څ༉͠ʹߦ͘ څ༉ څ༉͕
 ऴΘΔ Ψεܽղܾ -

    શͯͷॲཧ͸ं͕ߦͳ͍ͬͯΔ - ॴ༗ऀ͸ंʹ৐ͬऔΒΕͯ͠·͏ - ͷͪͷ4LZOFU໰୊ʢେӕʣ ͜ΕΒͷ໰୊͸
 ͋͘·Ͱॴ༗ऀࣗ਎͕
 ղܾ͢΂͖ʂ
  20. ंͷΨεܽ໰୊ class Person { var car: Car? func buyCar(_ car:

    Car) { self.car = car car.owner = self } func buyGasoline() { print("Buy some gasoline!") } } extension Person: CarOwner { func carDidRunOutOfGasoline(_ car: Car) { self.buyGasoline() } } protocol CarOwner: class { func carDidRunOutOfGasoline(_ car: Car) } class Car { let model: String weak var owner: CarOwner? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner? .carDidRunOutOfGasoline(self) } } let maki = Person() let ferrari458Italia = Car("458Italia") maki.buyCar(ferrari458Italia) maki.car?.outOfGasoline() // Buy some gasoline!
  21. ंͷΨεܽ໰୊ Ψεܽ ॴ༗ऀʹ
 ஌ΒͤΔ څ༉͠ʹߦ͘ څ༉ څ༉͕
 ऴΘΔ Ψεܽղܾ -

    શͯͷॲཧ͸ॴ༗ऀ͕ߦͳ͍ͬͯΔ - ॴ༗ऀ͸ंʹ৐ͬऔΒΕͯ͠·Θͳ͍ - ॴ༗ऀͷؾ࣋ͪͰങ͍׵͑Δ͜ͱ΋Մೳʂ
  22. ंͷΨεܽ໰୊ Ψεܽ ॴ༗ऀʹ
 ஌ΒͤΔ σΟʔϥʔʹߦ͘ ങ͍׵͑Δ ৽͍͠ंΛೖख - શͯͷॲཧ͸ॴ༗ऀ͕ߦͳ͍ͬͯΔ -

    ॴ༗ऀ͸ंʹ৐ͬऔΒΕͯ͠·Θͳ͍ - ॴ༗ऀͷؾ࣋ͪͰങ͍׵͑Δ͜ͱ΋Մೳʂ
  23. ंͷΨεܽ໰୊ class Person { var car: Car? func buyCar(_ car:

    Car) { self.car = car car.owner = self } func buyGasoline() { print("Buy some gasoline!") } } extension Person: CarOwner { func carDidRunOutOfGasoline(_ car: Car) { self.buyCar(Car("488GTB")) } } protocol CarOwner: class { func carDidRunOutOfGasoline(_ car: Car) } class Car { let model: String weak var owner: CarOwner? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner? .carDidRunOutOfGasoline(self) } } let maki = Person() let ferrari458Italia = Car("458Italia") maki.buyCar(ferrari458Italia) maki.car?.outOfGasoline() // Buy Ferrari 488 GTB!
  24. ੹຿Λҙࣝ͢Δͱݴ͏ͷ͸
 ෦඼ͷॴ༗ؔ܎ʢ֊૚ؔ܎ʣΛ
 ҙࣝ͢Δ͜ͱͰ΋͋Δ

  25. ंͷΨεܽ໰୊ class Person { var car: Car? func buyCar(_ car:

    Car) { self.car = car car.owner = self } func buyGasoline() { print("Buy some gasoline!") } } extension Person: CarOwner { func carDidRunOutOfGasoline(_ car: Car) { self.buyCar(Car("488GTB")) } } protocol CarOwner: class { func carDidRunOutOfGasoline(_ car: Car) } class Car { let model: String weak var owner: CarOwner? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner? .carDidRunOutOfGasoline(self) } } let maki = Person() let ferrari458Italia = Car("458Italia") maki.buyCar(ferrari458Italia) maki.car?.outOfGasoline() // Buy Ferrari 488 GTB!
  26. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ protocol CarOwner: class { func carDidRunOutOfGasoline(_ car: Car) }

    class Car { let model: String weak var owner: CarOwner? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner? .carDidRunOutOfGasoline(self) } } - ं͕Φʔφʔͷߦಈʹ੹೚Λ࣋ͯ ͳ͍ʹ੹຿͕ͳ͍ - ͔ͩΒΦʔφʔΛॴ༗ͯ͠͸ ͍͚ͳ͍ - ͦΕͰΦʔφʔΛ௚઀ಈ͔͢ ͜ͱ΋Ͱ͖ͳ͍ - Ͱ΋$BS0XOFSͱͯ͠ಈ͚Δ ͜ͱ͸஌͍ͬͯΔ - ඞཁͳ࣌ʹΦʔφʔʹॲཧΛ ҠৡͰ͖Δ
  27. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ protocol CarOwner: class { func carDidRunOutOfGasoline(_ car: Car) }

    class Car { let model: String weak var owner: CarOwner? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner? .carDidRunOutOfGasoline(self) } } - ं͸ΦʔφʔΛॴ༗͍ͯ͠ͳ͍ - ΦʔφʔΛऑࢀর͠ͳ͚Ε͹ͳΒͳ͍ - ڧࢀর࢖͏͔ɺऑࢀর࢖͏͔ͷج४͸ɺ ͜ͷΦϒδΣΫτΛॴ༗͍ͯ͠Δ͔Ͳ͏͔ - ॴ༗ؔ܎͸Ұํ௨ߦͳͷͰɺ॥؀ࢀর ͷ໰୊΋ճආͰ͖Δ
  28. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ protocol CarOwner: class { func carDidRunOutOfGasoline(_ car: Car) }

    class Car { let model: String weak var owner: CarOwner? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner? .carDidRunOutOfGasoline(self) } } - ໋໊িಥͷճආ - ΋͔͢͠Δͱಉ໊͡લͷผͷϝιου͕ ͋Δ͔΋͠Εͳ͍ - Ҡৡݩ͸୭͔Λ൑ผͰ͖ΔΑ͏ʹ - Ҡৡઌ͕ಉ͡Ϋϥεͷෳ਺ΦϒδΣΫτ ࣋ͬͯΔ͔΋͠Εͳ͍
  29. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ protocol CarOwner: class { func carDidRunOutOfGasoline(_ car: Car) }

    class Car { let model: String weak var owner: CarOwner? init(_ model: String) { self.model = model } func outOfGasoline() { self.owner? .carDidRunOutOfGasoline(self) } } - ࣗ෼ࣗ਎Λ๫࿐ͨ͘͠ͳ͍৔߹͸Ͳ͏͠Α͏# - ྫ͑͹Ҡৡઌ͸ࣗ෼ͷॴ༗ऀ͡Όͳ͍͔΋ ͠Εͳ͍৔߹ - యܕతͳύλʔϯ͸7JFXͷ%BUB4PVSDF - ͜͜Ͱ͸ंͷΨϨʔδΛྫʹ
  30. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ protocol CarGarage: class { func carNeedsToPark(_ car: CarGarageUser) }

    protocol CarGarageUser: class { var garageUserModel: String { get } } class Car { let model: String weak var garage: CarGarage? // ... func parkToGarage() { self.garage?.carNeedsToPark(self) } } extension Car: CarGarageUser { var garageUserModel: String { return self.model } } ࣗ෼ࣗ਎Λ$BSͷ୅ΘΓʹ $BS(BSBHF6TFSͱͯ͠๫࿐ $BS(BSBHF͕ඞཁͦ͏ͳ΋ͷ͚ͩΛ $BS(BSBHF6TFSͱͯ͠ఏڙ
  31. ੹຿Λҙࣝ͢Δͱݴ͏ͷ͸
 ඞཁ࠷খݶͷ৘ใΛ஌Γͳ͕Β
 ඞཁ࠷খݶͷ৘ใΛ๫࿐͢ΔͷΛ
 ҙࣝ͢Δ͜ͱͰ΋͋Δ

  32. ·ͱΊ ‣ ੹຿ൣғΛҙࣝ͠Α͏ ‣ ෦඼ʢ7JFX΍.PEFMʣͱͯ͠ͷ໾ׂΛҙࣝ͠Α͏ ‣ ෦඼ͷॴ༗ؔ܎ʢ֊૚ؔ܎ʣΛҙࣝ͠Α͏ ‣ ඞཁ࠷খݶͷ৘ใͷอ࣋ͱ΍ΓऔΓΛҙࣝ͠Α͏

  33. ੹຿ൣғΛҙࣝͯ͠
 ࢓༷มߋʹڧ͍։ൃΛ
 "