Where to put views creation in MVVM and MVC patterns?

半城伤御伤魂 提交于 2019-12-01 12:30:26

Maybe you should try the fully object oriented path. A view composition looks something like that:

// reusable protocol set

protocol OOString: class {
    var value: String { get }
}

protocol Executable: class {
    func execute()
}

protocol Screen: class {
    var ui: UIViewController { get }
}

protocol ViewRepresentation: class {
    var ui: UIView { get }
}

// reusable functionality (no uikit dependency)

final class ConstString: OOString {

    init(_ value: String) {
        self.value = value
    }

    let value: String

}

final class ExDoNothing: Executable {

    func execute() { /* do nothing */ }

}

final class ExObjCCompatibility: NSObject, Executable {

    init(decorated: Executable) {
        self.decorated = decorated
    }

    func execute() {
        decorated.execute()
    }

    private let decorated: Executable

}

// reusable UI (uikit dependency)

final class VrLabel: ViewRepresentation {

    init(text: OOString) {
        self.text = text
    }

    var ui: UIView {
        get {
            let label = UILabel()
            label.text = text.value
            label.textColor = UIColor.blue
            return label
        }
    }

    private let text: OOString

}

final class VrButton: ViewRepresentation {

    init(text: OOString, action: Executable) {
        self.text = text
        self.action = ExObjCCompatibility(decorated: action)
    }

    var ui: UIView {
        get {
            let button = UIButton()
            button.setTitle(text.value, for: .normal)
            button.addTarget(action, action: #selector(ExObjCCompatibility.execute), for: .touchUpInside)
            return button
        }
    }

    private let text: OOString
    private let action: ExObjCCompatibility

}

final class VrComposedView: ViewRepresentation {

    init(first: ViewRepresentation, second: ViewRepresentation) {
        self.first = first
        self.second = second
    }

    var ui: UIView {
        get {
            let view = UIView()
            view.backgroundColor = UIColor.lightGray
            let firstUI = first.ui
            view.addSubview(firstUI)
            firstUI.translatesAutoresizingMaskIntoConstraints = false
            firstUI.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
            firstUI.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
            firstUI.widthAnchor.constraint(equalToConstant: 100).isActive = true
            firstUI.heightAnchor.constraint(equalToConstant: 40).isActive = true
            let secondUI = second.ui
            view.addSubview(secondUI)
            secondUI.translatesAutoresizingMaskIntoConstraints = false
            secondUI.topAnchor.constraint(equalTo: firstUI.topAnchor).isActive = true
            secondUI.leadingAnchor.constraint(equalTo: firstUI.trailingAnchor, constant: 20).isActive = true
            secondUI.widthAnchor.constraint(equalToConstant: 80).isActive = true
            secondUI.heightAnchor.constraint(equalToConstant: 40).isActive = true
            return view
        }
    }

    private let first: ViewRepresentation
    private let second: ViewRepresentation

}

// a viewcontroller

final class ContentViewController: UIViewController {

    convenience override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)   {
        self.init()
    }

    convenience required init(coder aDecoder: NSCoder) {
        self.init()
    }

    convenience init() {
        fatalError("Not supported!")
    }

    init(content: ViewRepresentation) {
        self.content = content
        super.init(nibName: nil, bundle: nil)
    }

    override func loadView() {
        view = content.ui
    }

    private let content: ViewRepresentation

}

// and now the business logic of a screen (not reusable)

final class ScStartScreen: Screen {

    var ui: UIViewController {
        get {
            return ContentViewController(
                content: VrComposedView(
                    first: VrLabel(
                        text: ConstString("Please tap:")
                    ),
                    second: VrButton(
                        text: ConstString("OK"),
                        action: ExDoNothing()
                    )
                )
            )
        }
    }

}

Usage in AppDelegate:

window?.rootViewController = ScStartScreen().ui

Note:

  • it follows the rules of object oriented coding (clean coding, elegant objects, decorator pattern, ...)
  • every class is very simple constructed
  • classes communicate by protocols with each other
  • all dependencies are given by dependency injection as far as possible
  • everything (except the business screen at end) is reusable -> in fact: the portfolio of reusable code grows with every day you code
  • the business logic of your app is concentrated in implementations of Screen objects
  • unittesting is very simple when using fake implementations for the protocols (even mocking is not needed in most cases)
  • lesser problems with retain cycles
  • avoiding Null, nil and Optionals (they pollute your code)
  • ...

In my opinion it's the best way to code, but most people don't do it like that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!