-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSearchMethodObserver.swift
67 lines (62 loc) · 1.85 KB
/
SearchMethodObserver.swift
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Author: Alex Cummaudo
// Student ID: 1744070
// Program: A1 - PuzzleProblem
// Unit: COS30019 - Intro to AI
// Date: 18/03/2016
//
///
/// Observes search methods that are currently traversing and notifies all subscribers
///
struct SearchMethodObservationCenter {
///
/// Singleton shared instance
///
static var sharedCenter = SearchMethodObservationCenter()
///
/// List of subscribers that the observer notifies
///
private var observers = [SearchMethodObserver]()
///
/// Add a observer
/// - Parameter observer: Subscriber to add
///
mutating func addObserver(observer: SearchMethodObserver) {
if !self.observers.contains({ ob -> Bool in observer === ob }) {
self.observers.append(observer)
}
}
///
/// Add a observer
/// - Parameter observer: Subscriber to add
///
mutating func removeObserver(observer: SearchMethodObserver) {
guard let index = self.observers.indexOf({ ob -> Bool in
observer === ob
}) else {
return
}
self.observers.removeAtIndex(index)
}
///
/// Notifies all subscribers of a node traversal
/// - Parameter node: The node that was just traversed
/// - Parameter isSolved: Whether or not this node is the goal node
///
func notifyObservers(node: Node, isSolved: Bool) {
for observer in observers {
observer.didTraverseNode(node, isSolved: isSolved)
}
}
}
///
/// A observer to a search method
///
protocol SearchMethodObserver : class {
///
/// Notification event when a node was just traversed
/// - Parameter node: The node that was just traversed
/// - Parameter isSolved: Whether or not this node is the goal node
///
func didTraverseNode(node: Node, isSolved: Bool)
}