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> {
    }

}

      

+3
ios strategy-pattern networking swift solid-principles


source to share


No one has answered this question yet

Check out similar questions:

196
What is the difference between the Strategy Design Pattern and the Government Design Pattern?
114
What is the difference between template method and strategy templates?
112
Using the strategy template and the team template
90
What is the difference between Factory and Strategy Patterns?
90
What is the difference between the bridge pattern and the strategy pattern?
88
Real World Strategy Example
2
Refactor with a strategy template and then apply the SOLID principle
2
Topic 1: Error EXC_BREAKPOINT (code = 1,) in DataSnapshot?
0
Firebase: message: didRefreshRegistrationToken: The category implements a method that will also be implemented by its main class
0
Pass function to completion handler



All Articles
Loading...
X
Show
Funny
Dev
Pics