From the course: Practical Design Patterns in Swift
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Implementing the Factory Method - Swift Tutorial
From the course: Practical Design Patterns in Swift
Implementing the Factory Method
- [Instructor] The main idea behind the Factory Method Pattern is to identify a type, without exposing it. We can achieve that by defining a set of constants that match a particular type. I've gone ahead and created a swift playground project called Factory Method Demo. You can open the project from the exercise files folder, chapter four, four two begin. Consider the following example. We have a serializable protocol that declares a method called serialize. Next, I define three classes that conform to the serializable protocol. First, I create a class called Jason serializer. And it conforms to serializable, so we have to implement the serialize method. I will simply output the console message. We have the name of the class and the name of the function. And then I declare another class called Property List Serializer, that also conforms to serializable. I simply copy and paste the existing class and I'm going to rename it To Property Lists Serializer. And a third one, called XMl…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.