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

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

Elvis Shi
October 03, 2017

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

Elvis Shi

October 03, 2017
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

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

    "iOS Developer" twitter = "@lovee" qiita = "lovee" github = "el-hoshino" additionalInfo = "స৬͠·ͨ͠ʂ" class Speaker: Developer { } }
  2. ຊ౰ʹ੹຿෼͚ΒΕͯΔͷ͔ʁ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad()

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

    .77. ‣ .PEFMɿϏδωεϩδοΫ ‣ 7JFXɿը໘ೖग़ྗ ‣ 7JFX.PEFMɿσʔλόΠϯσΟϯά ‣ FUD
  4. ຊ౰ʹ੹຿෼͚ΒΕͯΔͷ͔ʁ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad()

    view.backgroundColor = UIColor(hue: 0.5, saturation: 1, brightness: 0.9, alpha: 1) } } $POUSPMMFS͕ϏδϡΞϧΛ ௚઀ઃఆ͍ͯ͠Δ!
  5. ຊ౰͸͜͏͢΂͖͡Όͳ͍ͷ͔ʁ 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͕ϏδϡΞϧΛ ௚઀ઃఆ͍ͯ͠ͳ͍" ϏδϡΞϧͷઃఆ͸શͯ
 Ϗϡʔʹू໿͞ΕΔ
  6. ंͷΨεܽ໰୊ 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)
  7. ंͷΨεܽ໰୊ 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() // ???
  8. ंͷΨεܽ໰୊ 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!
  9. ंͷΨεܽ໰୊ Ψεܽ ॴ༗ऀʹ
 څ༉ͤ͞Δ څ༉͠ʹߦ͘ څ༉ څ༉͕
 ऴΘΔ Ψεܽղܾ -

    શͯͷॲཧ͸ं͕ߦͳ͍ͬͯΔ - ॴ༗ऀ͸ंʹ৐ͬऔΒΕͯ͠·͏ - ͷͪͷ4LZOFU໰୊ʢେӕʣ ͜ΕΒͷ໰୊͸
 ͋͘·Ͱॴ༗ऀࣗ਎͕
 ղܾ͢΂͖ʂ
  10. ंͷΨεܽ໰୊ 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!
  11. ंͷΨεܽ໰୊ Ψεܽ ॴ༗ऀʹ
 ஌ΒͤΔ څ༉͠ʹߦ͘ څ༉ څ༉͕
 ऴΘΔ Ψεܽղܾ -

    શͯͷॲཧ͸ॴ༗ऀ͕ߦͳ͍ͬͯΔ - ॴ༗ऀ͸ंʹ৐ͬऔΒΕͯ͠·Θͳ͍ - ॴ༗ऀͷؾ࣋ͪͰങ͍׵͑Δ͜ͱ΋Մೳʂ
  12. ंͷΨεܽ໰୊ 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!
  13. ंͷΨεܽ໰୊ 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!
  14. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ 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ͱͯ͠ಈ͚Δ ͜ͱ͸஌͍ͬͯΔ - ඞཁͳ࣌ʹΦʔφʔʹॲཧΛ ҠৡͰ͖Δ
  15. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ 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) } } - ं͸ΦʔφʔΛॴ༗͍ͯ͠ͳ͍ - ΦʔφʔΛऑࢀর͠ͳ͚Ε͹ͳΒͳ͍ - ڧࢀর࢖͏͔ɺऑࢀর࢖͏͔ͷج४͸ɺ ͜ͷΦϒδΣΫτΛॴ༗͍ͯ͠Δ͔Ͳ͏͔ - ॴ༗ؔ܎͸Ұํ௨ߦͳͷͰɺ॥؀ࢀর ͷ໰୊΋ճආͰ͖Δ
  16. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ 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) } } - ໋໊িಥͷճආ - ΋͔͢͠Δͱಉ໊͡લͷผͷϝιου͕ ͋Δ͔΋͠Εͳ͍ - Ҡৡݩ͸୭͔Λ൑ผͰ͖ΔΑ͏ʹ - Ҡৡઌ͕ಉ͡Ϋϥεͷෳ਺ΦϒδΣΫτ ࣋ͬͯΔ͔΋͠Εͳ͍
  17. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ 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 - ͜͜Ͱ͸ंͷΨϨʔδΛྫʹ
  18. ඞཁ࠷খݶͷ͜ͱ͚ͩΛ஌Ζ͏ 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ͱͯ͠ఏڙ