Correct use of strategy pattern for simple network layer in Swift 3
Does the following code mean the correct use of the strategy design pattern for a simple network layer in swift 3?
Some code smells that I am not aware of:
-
violates the principle of single responsibility. Each strategy class such as Find has a method for a different implementation type. This is because I want to find an image, user, or chat room. which are stored in different nodes in Firebase. all of these different search methods are grouped together in the search class.
-
When calling a request, if I need to make multiple asynchronous requests, I set the next call request in the closing callback. This is normal?
-
The request object permits access to every insert type and search method. so in my VC VC registration I might be able to load the chat. Is there even a bad access to such an implementation?
I've posted the code below and left all the actual implementation for brevity.
Any advice or guidance is greatly appreciated!
// USE CASE: Would go in viewDidLoad of ViewController
func testMyRequest () {
let myRequest = Request(insert: Insert(), find: Find())
myRequest.find?.user(with: "id", handler: { (user) in
myRequest.find?.nearbyUsers(user: user, handler: { (users) in
// update collectionView datasource
})
})
}
// Is this protocol necessary?
protocol RequestProtocol {
// - Family of algorithms, related actions.
var insert: Insert? { get set }
var find: Find? { get set }
}
// ---------------------------
class Request: RequestProtocol {
var insert: Insert?
var find: Find?
init(insert: Insert?, find: Find?) {
self.insert = insert
self.find = find
}
}
// Use a singleton maybe for the classes below? Why wouldn't I?
class Insert {
init() { }
func user(_ user: User) {
// insert user to firebase implementation
}
func message(_ message: Message) -> Void {
// insert message to firebase impelmentation
}
func image(data: Data, user: User) {
// insert image to firebase impelmentation
}
}
class Find {
init() { }
func user(with id: String, handler: @escaping (_ user: User) -> Void ) {
// find user implementation
}
func allChatrooms(handler: @escaping ([Chatroom]) -> Void) {
// find all chatrooms implementation
}
func nearbyUsers(user: User, handler: @escaping ([User]) -> Void ) {
// find nearby Users relative to current User location implementation
}
// Private helper method
private func findChatPartners (currentUser: User, chatrooms: [Chatroom] ) -> Set<String> {
}
}
No one has answered this question yet
Check out similar questions: