Swift,iOS:如何从混凝土类型中获得多态性和脱致协调员

发布于 2025-01-25 09:40:15 字数 1241 浏览 2 评论 0原文

我有以下协议:

protocol Coordinator {
    var rootViewController: UIViewController { get set }
    func start()
}

protocol UIViewControllerFactory {
    func mainViewController() -> UIViewController
}

我创建了一个符合此协议的Main Coordinator ,并且通过了一个工厂,该工厂使我可以将协调器解脱出来,无法创建和捕获混凝土类型,因此可以是 polymormormorphic < /strong>,可以与 uiviewController的更多实现一起使用, rootviewControllers mainmenuviewController ,如下所示:

class MainCoordinator: Coordinator {
    var rootViewController: UIViewController
    let factory: UIViewControllerFactory 
    
    init(rootViewController: UIViewController, factory: UIViewControllerFactory) {
        self.rootViewController = rootViewController
    }
    
    start() {
        guard let mainVC = factory.mainViewController() as? MainViewController, let rootViewController = rootViewController as? UINavigationViewController  else { return }
        
        mainVC.delegate = self
        rootViewController.push(mainVC, animated: true)
    }

尽管我可以看到,但VE创建了协调器,以接受UiviewController的任何子类,它已在 start 函数中耦合到UiviewController的具体实现:MainViewController。

我如何将其从mainviewController中解脱出来,并使它更多态

I have these protocols:

protocol Coordinator {
    var rootViewController: UIViewController { get set }
    func start()
}

protocol UIViewControllerFactory {
    func mainViewController() -> UIViewController
}

And I created a MainCoordinator that conforms to this protocol and I pass a factory that allows me to decouple the coordinator from creating and capturing a concrete type so it can be polymorphic and can be used with more implementations of UIViewController either as rootViewControllers and mainMenuViewController as shown below:

class MainCoordinator: Coordinator {
    var rootViewController: UIViewController
    let factory: UIViewControllerFactory 
    
    init(rootViewController: UIViewController, factory: UIViewControllerFactory) {
        self.rootViewController = rootViewController
    }
    
    start() {
        guard let mainVC = factory.mainViewController() as? MainViewController, let rootViewController = rootViewController as? UINavigationViewController  else { return }
        
        mainVC.delegate = self
        rootViewController.push(mainVC, animated: true)
    }

As you can see, although I've created the coordinator to accept any subclass of UIViewController it has been coupled in the start function to the concrete implementation of UIViewController: MainViewController.

How can I decouple it from MainViewController and have it more polymorphic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2025-02-01 09:40:15

您可以将协调器作为出厂函数中的参数类型传递,并在创建Controller实例时直接在出厂功能中进行委托。这样,您就不必将Controller type显式地从工厂类中出现。

我想到了以下方法。

protocol Coordinator {
    var rootViewController: UIViewController { get set }
    func start()
}

protocol UIViewControllerFactory {
    func getViewController(delegateType:CoordinatoreTypes,delegateObject:Coordinator) -> UIViewController?
}

class MainCoordinator: Coordinator {
    var rootViewController: UIViewController
    let factory: UIViewControllerFactory
    
    init(rootViewController: UIViewController, factory: UIViewControllerFactory) {
        self.rootViewController = rootViewController
        self.factory = factory
    }
    
    func start() {
        guard let controller = factory.getViewController(delegateType: .MainCoordinator, delegateObject: self),let rootViewController = rootViewController as? UINavigationViewController else {
            return
        }
        rootViewController.push(mainVC, animated: true)
    }
}

extension MainCoordinator:DelegateCaller{
    func printHello() {
        print("helloo")
    }
}

enum CoordinatoreTypes{
    case MainCoordinator
    case none
}

class Factory:UIViewControllerFactory{
    func getViewController(delegateType:CoordinatoreTypes,delegateObject:Coordinator) -> UIViewController?{
        switch delegateType{
        case .MainCoordinator:
            let controller = MainViewController()
            controller.delegate = delegateObject as? MainCoordinator
            return controller
        case .none:
            break
        }
        return nil
    }
}

class MainViewController:UIViewController{
    weak var delegate:DelegateCaller?
}

protocol DelegateCaller:AnyObject{
    func printHello()
}

You can pass coordinator as a parameter type in factory function and set delegate directly in factory function while creating controller instance. That way you wouldn’t have to expose controller type explicitly out of factory classes.

I came up with below approach.

protocol Coordinator {
    var rootViewController: UIViewController { get set }
    func start()
}

protocol UIViewControllerFactory {
    func getViewController(delegateType:CoordinatoreTypes,delegateObject:Coordinator) -> UIViewController?
}

class MainCoordinator: Coordinator {
    var rootViewController: UIViewController
    let factory: UIViewControllerFactory
    
    init(rootViewController: UIViewController, factory: UIViewControllerFactory) {
        self.rootViewController = rootViewController
        self.factory = factory
    }
    
    func start() {
        guard let controller = factory.getViewController(delegateType: .MainCoordinator, delegateObject: self),let rootViewController = rootViewController as? UINavigationViewController else {
            return
        }
        rootViewController.push(mainVC, animated: true)
    }
}

extension MainCoordinator:DelegateCaller{
    func printHello() {
        print("helloo")
    }
}

enum CoordinatoreTypes{
    case MainCoordinator
    case none
}

class Factory:UIViewControllerFactory{
    func getViewController(delegateType:CoordinatoreTypes,delegateObject:Coordinator) -> UIViewController?{
        switch delegateType{
        case .MainCoordinator:
            let controller = MainViewController()
            controller.delegate = delegateObject as? MainCoordinator
            return controller
        case .none:
            break
        }
        return nil
    }
}

class MainViewController:UIViewController{
    weak var delegate:DelegateCaller?
}

protocol DelegateCaller:AnyObject{
    func printHello()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文