Builder & Factory Pattern in Swift
Builder & Factory Pattern in Swift
Builder: Separate the construction of a complex object from its representation so that the same construction process can create different representations, Parse a complex representation, create one of several targets.
Factory: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Question 1: You are asked to implement the builder design pattern for rendering simple chunks of code.
Sample use of the builder you are asked to create:
The expected output of the above code is:
Please observe the same placement of curly braces and use two-space indentation.
Solution:
Question 2: You are given a class called Employee. The Employee has two
fields: id and name.
Please implement a non static
EmployeeFactory that has a createEmployee() method that takes a
Employee’s name.
The id of the Employee should be set as a
0-based index of the object created. So, the first employee the
factory makes should have id=0, second id =1 and so on.