From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Polymorphic instantiation

Polymorphic instantiation - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Polymorphic instantiation

- [Instructor] The factory method design pattern is one of my favorites. It can be extremely useful and it has basically no drawbacks. It's a creational pattern that promotes loose coupling. The factory method lets us create instances of types that have a common superclass, or ones that conform to a given protocol. The callers don't need to know the complete implementation types, just the protocol or the base class. Thus, we can eliminate the dependencies between the implementation types and the clients. Besides reducing the typed coupling in the system, the factory method pattern has further benefits. The exact type is not known to the callers, only the methods and the calculated properties declared in the protocol are exposed. The other details remain hidden. Thus, we can perform changes without having to refactor depending components. We can even replace the concrete implementation types or add new ones without effecting the callers. The factory method doesn't have any drawbacks…

Contents