swift swift代理模式演示。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift swift代理模式演示。相关的知识,希望对你有一定的参考价值。

//
// https://medium.com/@jegnux/safe-collection-subsripting-in-swift-3771f16f883
// https://github.com/ReactiveX/RxSwift/issues/826
//


import Foundation

// we define a fire protocol.
protocol FireProtocol {
  // Type placeholder
  associatedtype FireType
  var fire: FireType { get }
}


// make a fire type.
public class Fire<Base> {
  public let base: Base
  public init(_ base: Base) {
    // base is the conforming type that conform to FireProtocol,
    // in current file.
    self.base = base
  }

  // default behavior
  func fireInTheHole() {
    print("fire !!!!!")
  }
}

extension FireProtocol {
  // Self reference to the conforming type.
  public var fire: Fire<Self> {
    get {
      return Fire(self)
    }
  }
}

// -------------------------------
// Begin the fun part

// Create a dragon.
public class Dragon {}
// give the dragon fire attribute.
extension Dragon: FireProtocol {}

// Instead of extension the Dragon class, we extension `Fire` class
// and give the dragon a property that is a `Fire`. then the dragon can have the
// ability that Fire object have.
let dragon = Dragon()
dragon.fire.fireInTheHole()

extension Fire {
  public func fireAgain() {
    print("fireAgain")
  }
}

// See ?
dragon.fire.fireAgain()

// create another type.
public class Bird: FireProtocol {}
// bird also have the ability that dragon have.
let bird = Bird()
bird.fire.fireInTheHole()
bird.fire.fireAgain()

// This is nothing special to the normal protocol oriented programming way.
// We can use generic constraint to add ability to one type.

// add ability to dragon only.
extension Fire where Base: Dragon {
  public func dragonOnlyMethod() {
    print("dragon only method")
  }
}

dragon.fire.dragonOnlyMethod()

// error:  'Bird' is not a subtype of 'Dragon'
// bird.fire.dragonOnlyMethod()

extension Fire where Base: Bird {
  public func eatWorm() {
    print("bird eat worm")
  }
}

bird.fire.eatWorm()

以上是关于swift swift代理模式演示。的主要内容,如果未能解决你的问题,请参考以下文章

swift设计模式学习 - 代理模式

Swift语言精要 - 浅谈代理模式(Delegate)

哪个是在MVP模式中初始化演示者的最佳方式(swift)

设计模式(Swift) - 单例模式备忘录模式和策略模式

Swift-020无人机在日本进行首次自主飞行演示

Swift-代理