Chapter 23: Coordinator Pattern
大綱
Creating the Router Protocol
Creating the Concrete Router
Creating the Coordinator
Creating the Concrete Coordinator
When should you use it?
The coordinator pattern is a structural design pattern for organizing flow logic between view controllers.
The coordinator: a protocol that defines the methods and properties all concrete coordinators must implement.
The concrete coordinator: implements the coordinator protocol.
The router: a protocol that defines methods all concrete routers must implement
The concrete router: knows how to present view controllers, but it doesn’t know exactly what is being presented or which view controller will be presented next.
The concrete view controllers: typical UIViewController subclasses found in MVC. However, they don’t know about other view controllers.

When should you use it?
Use this pattern to decouple view controllers from one another. The only component that knows about view controllers directly is the coordinator.
Playground example
目標:
Creating the Router Protocol
Creating the Concrete Router
Creating the Coordinator
Creating the Concrete Coordinator
What should you be careful about?
Make sure you handle going-back functionality when using this pattern.
Make sure you provide any required teardown code passed into onDismiss on the coordinator’s present(animated: onDismiss:).
For very simple apps, the Coordinator pattern may seem like overkill. You’ll be required to create many additional classes upfront; namely, the concrete coordinator and routers.
For long-term or complex apps, the coordinator pattern can help you provide needed structure and increase view controllers’ reusability.
Tutorial project
Key points
The coordinator pattern organizes flow logic between view controllers. It involves a coordinator protocol, concrete coordinator, router protocol, concrete router and view controllers.
The coordinator defines methods and properties all concrete coordinators must implement.
The concrete coordinators know how to create concrete view controllers and their order.
The router defines methods all concrete routers must implement.
The concrete routers know how to present view controllers.
The concrete view controllers are typical view controllers, but they don’t know about other view controllers.
This pattern can be adopted for only part of an app or used across an entire application.
Last updated