Factory pattern can come in handy when you want to reduce the dependency of a class on other classes. On the other hand, it encapsulates the object creation process and users can simply pass in parameters to a generic factory class without knowing how the objects are actually being created. Also it gives you a clean code.
Let’s begin by creating a protocol (You can achieve the same with a super class too but I like…