Factory Design Pattern

ansu jain
The Create School
Published in
2 min readApr 4, 2018

--

The factory method pattern is used when there is a choice to be made between classes that implement a common protocol or share a common base class. This pattern allows implementation subclasses to provide specializations without requiring the components that rely on them to know any details of those classes and how they relate to each other

What is Factory Design Pattern?

The factory method pattern selects an implementation class to satisfy a calling component’s request without requiring the component to know anything about the implementation classes or the way they relate to one another.

What are the benefits?

This pattern consolidates the logic that decides which implementation class is selected and prevents it from being diffused throughout the application. This also means that calling components rely only on the top-level protocol or base class and do not need any knowledge about the implementation classes or the process by which they are selected.

When should you use this pattern?

Use this pattern when you have several classes that implement a common protocol or that are derived from the same base class.

When should you avoid this pattern?

Do not use this pattern when there is no common protocol or shared base class because this pattern works by having the calling component rely on only a single type

Let’s look into the example:

protocol JobContactProtocol{ 
func sendRequestResumeEmail()
}
struct Contact {
var name:String
var job:Job
init(name:String, job:Job){
self.name = name
self.job = job
}
enum Job {
case IOS
case Android
}
}
class IOSJob: JobContactProtocol {
var contact:Contact
func sendRequestResumeEmail() {
print("Dear \(contact.name) you are the ios candidate")
}
init(contact:Contact){
self.contact = contact
}
}
class AndroidJob: JobContactProtocol {
var contact:Contact
func sendRequestResumeEmail() {
print("Dear \(contact.name) you are the Android candidate")
}
init(contact:Contact){
self.contact = contact
}
}
struct jobContactorFactory {
static func getJobSeeker(contact:Contact) ->JobContactProtocol{
switch(contact.Job){
case .IOS:
return IOSJob(contact)
case .Android:
return AndroidJob(contact)
}
}
}
var contacts = [Contact]()
contacts.append(Contact(name: "J Rob", job: .IOS))
contacts.append(Contact(name: "J Rob", job: .Android))

for contact in contacts{
let client = jobContactorFactory.getJobSeeker(contact: contact)
print (client.sendRequestResumeEmail())
}

This above code contains a protocol called JobContactProtocol and two conforming classes: IOSJob and AndroidJob.

The factory method pattern solves a problem that arises when there are multiple classes that conform to a protocol and you need to select the one that should be instantiated. You can see this at work in above code, in which jobContactorFactory.getJobSeeker() method selects and instantiates one of the classes that conforms to the JobContactProtocol based on the type of the Job.

--

--