Implementing the factory method pattern

To demonstrate how to use the factory method pattern, we will use the text validation types that were created in Chapter 3, Extensions. In this example, a function will be created that will determine which text validation type to use based on the parameters passed into the factory method by the code that called it. As a refresher, the code for the TextValidation protocol and the TextValidation protocol extension are shown in the following code:

protocol TextValidation { var regExFindMatchString: String {get} var validationMessage: String {get} } extension TextValidation { var regExMatchingString: String { get { return regExFindMatchString + "$" } } func validateString(str: String) -> Bool { if let _ ...

Get Swift 4 Protocol-Oriented Programming - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.