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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.