Yi Jiang
Yi Jiang

Reputation: 4038

What kind of design pattern XxxxHelper is?

I got a one-year-old legacy project from other develop a team. In the project, I found a folder called "Utils". In this folder, There are many XxxxHelper.swift source code files. These helpers are also wrapping some third party pods.

Example: DeepLinkHelper wraps CocoaPods "Branch"

import Foundation
import Branch

class DeepLinkHelper {
    static func handleDeepLink(params: [AnyHashable:Any]?, error: Error?) {
        if error == nil {
            if let clickedBranch = params?["+clicked_branch_link"] as? Bool, let referringLink = params?["~referring_link"] as? String, let referringUrl = referringLink.toURL(), clickedBranch {
                let endpoint = referringUrl.lastPathComponent
                switch(endpoint) {
                case "debugLink":
#if ENV_DEBUG
                    NotificationCenter.default.post(name: Notification.Name(rawValue: InternalNotifications.backgroundAlertUser), object: nil, userInfo: ["title":"Branch Link","message":"Success!"])
#endif
                    break
                case "connaccts":
                    // Display connected accounts
                    NotificationCenter.default.post(name: Notification.Name(rawValue:InternalNotifications.switchToViewController), object: nil, userInfo:["destination": ViewControllerNames.connections])
                    break
                case "guestinvite":
                    // Request server to add source account as unvalidated connection
                    if let gValue = params?["g"] as? String {
                        handleGuestInvite(gValue)
                    }
                    break
                default:

                    break
                }
            }
            print("BranchIO-DLparams: \(String(describing:params))")
        }
        else {
            print("BranchIO-Error: \(error!)")
        }
    }
    static func handleGuestInvite(_ gValue: String) {
        NotificationCenter.default.post(name: Notification.Name(rawValue:InternalNotifications.switchToViewController), object: nil, userInfo:["destination": ViewControllerNames.main,"reset":true,"guestInviteData":gValue])
    }
}

Other helpers:

KeychainHelper.swift wraps the pod KeychainSwift. PhotoHelper.swift wraps the pod TOCropViewController and other native kits TrackingHelper.swift wraps the pod Tune

Question again:
What's these design pattern? And, what is the benefit to wrapping native or third-party SDKs and kits?

Upvotes: 0

Views: 68

Answers (2)

martidis
martidis

Reputation: 2975

The so called "wrapper" patterns are all essentially wrapping certain functionality and provide a different interface. The design patterns of this "group" are adapter, facade, decorator and proxy. They only differ in their intent:

  • facade: is used to provide a simple interface to clients, hiding the complexities of the operations it provides behind it
  • adapter: allows two incompatible interfaces to work together without changing their internal structure
  • decorator: allows new functionalities to be added to an object statically or dynamically without affecting the behavior of objects of the same class
  • proxy: a class (proxy) is used to represent and allow access to the functionality of another class

Upvotes: 2

Polymorphic
Polymorphic

Reputation: 430

Helper classes usually serve as implementation for Facade and/or Adapter pattern. Actually it would be definitely better to use the name of the pattern to name these classes instead of using Helper post-fix.

In fact, the main idea is acceptable but the naming does not convey the design pattern.

Upvotes: 1

Related Questions