多播委托模式

多播委托模式(Multicast Delegate Pattern)

多播委托模式一种行为模式,是代理模式的变体。它能让你创建一对多的代理关系,而普通的代理模式只有一对一的关系。它包含以下四个类型:

  1. object needing a delegate(delegating object):持有一个或多个代理的对象
  2. delegate protocol:定义了一个代理需要实现的方法
  3. delegate(s):实现了delegate protocol的多个对象
  4. multicast delegate:持有多个代理的辅助类,能在有事件发生时通知每个代理

代理模式和多播委托模式做大的区别就在multicast delegate的存在。

使用举例

创建multicast delegate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class MulticastDelegate<ProtocolType> {
// MARK: - DelegateWrapper
private class DelegateWrapper {
weak var delegate: AnyObject?
init(_ delegate: AnyObject) {
self.delegate = delegate
}
}
// MARK: - Instance Properties
private var delegateWrappers: [DelegateWrapper]
public var delegates: [ProtocolType] {
delegateWrappers = delegateWrappers
.filter { $0.delegate != nil }
return delegateWrappers.map
{ $0.delegate! } as! [ProtocolType]
}
// MARK: - Object Lifecycle
public init(delegates: [ProtocolType] = []) {
delegateWrappers = delegates.map {
DelegateWrapper($0 as AnyObject)
}
}
// MARK: - Delegate Management
public func addDelegate(_ delegate: ProtocolType) {
let wrapper = DelegateWrapper(delegate as AnyObject)
delegateWrappers.append(wrapper)
}
public func removeDelegate(_ delegate: ProtocolType) {
guard let index = delegateWrappers.index(where: {
$0.delegate === (delegate as AnyObject)
}) else {
return
}
delegateWrappers.remove(at: index)
}
public func invokeDelegates(_ closure: (ProtocolType) -> ()) {
delegates.forEach { closure($0) }
}
}

创建delegate protocol

1
2
3
4
5
// MARK: - Delegate Protocol
public protocol EmergencyResponding {
func notifyFire(at location: String)
func notifyCarCrash(at location: String)
}

创建delegates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// MARK: - Delegates
public class FireStation: EmergencyResponding {
public func notifyFire(at location: String) {
print("Firefighters were notified about a fire at "
+ location)
}
public func notifyCarCrash(at location: String) {
print("Firefighters were notified about a car crash at "
+ location)
}
}
public class PoliceStation: EmergencyResponding {
public func notifyFire(at location: String) {
print("Police were notified about a fire at "
+ location)
}
public func notifyCarCrash(at location: String) {
print("Police were notified about a car crash at "
+ location)
}
}

创建delegating object

1
2
3
4
5
// MARK: - Delegating Object
public class DispatchSystem {
let multicastDelegate =
MulticastDelegate<EmergencyResponding>()
}

使用多播委托模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// MARK: - Example
let dispatch = DispatchSystem()
var policeStation: PoliceStation! = PoliceStation()
var fireStation: FireStation! = FireStation()
dispatch.multicastDelegate.addDelegate(policeStation)
dispatch.multicastDelegate.addDelegate(fireStation)
dispatch.multicastDelegate.invokeDelegates {
$0.notifyFire(at: "Ray's house!")
}
print("")
fireStation = nil
dispatch.multicastDelegate.invokeDelegates {
$0.notifyCarCrash(at: "Ray's garage!")
}

其他例子

  1. 使用多播委托模式管理页面主题颜色:https://juejin.im/post/5bd6842f6fb9a05d0045f925
  2. OC中构造多播代理:https://www.jianshu.com/p/8f2b9d6b9c85