Factory Pattern - Why to use?

Factory Pattern - Why to use?

factory-header.png

Factory pattern is a creational pattern that provides an interface for creating objects in the superclass, but allows sub-classes to alter the type of objects that will be created.

A cre­ation­al pat­terns pro­vide var­i­ous object cre­ation mech­a­nisms, which increase flex­i­bil­i­ty and reuse of exist­ing code.

Now let’s talk about why we want to use the Factory pattern? (The problem) :

Let’s say we are building a transportation application like uber, at first you only support user transportation using cars so your code is basically about cars and how it is gonna work with cars. Now let’s imagine your business is growing and you will add a new type of transportation using helicopters, ships, or any other type of transportation. Woww this is great news for your business but what about your code? If you remember you build the application at first for cars only that’s mean your code is coupled to Car Class so adding helicopters or ships will effect with changes in the entire codebase and every time you want to add a new transportation type you will need to do more and more changes and at last you will find you ended with pretty nasty code with dozens of conditions switches to handle transportation type.

via GIPHY

How Factory Pattern will solve our problem?

The Fac­to­ry Method pat­tern will replace direct object con­struc­tion calls with calls to a spe­cial fac­to­ry method. Objects returned by a fac­to­ry method are often referred to as prod­ucts.

The structure:-

W3sDesign_Factory_Method_Design_Pattern_UML.jpg

  • The Prod­uct: declares the inter­face, which is com­mon to all objects that can be pro­duced by the cre­ator and its subclasses.

  • Con­crete Prod­ucts: are dif­fer­ent imple­men­ta­tions of the prod­uct interface.

  • The Cre­ator class: declares the fac­to­ry method that returns new prod­uct objects. It’s impor­tant that the return type of this method match­es the prod­uct interface.

  • Con­crete Cre­ators: over­ride the base fac­to­ry method so it returns a dif­fer­ent type of product.

It's Implementation Time🤓:

protocol TransportationCreator {
    func factoryMethod() -> Transporation
}

class CarCreator: TransportationCreator {
    public func factoryMethod() -> Transporation {
        return Car()
    }
}

class HelicaptorCreator: TransportationCreator {
    public func factoryMethod() -> Transporation {
        return Helicopter()
    }
}

protocol Transporation {
    func operation() -> String
}

class Car: Transporation {
    func operation() -> String {
        return "This is Car Operation"
    }
}

class Helicopter: Transporation {
    func operation() -> String {
        return "This is Helicopter Operation"
    }
}

Cons & Pros of Factory Design Pattern :

  • Cons :
  1. You avoid tight cou­pling between the cre­ator and the con­crete products.

  2. Sin­gle Respon­si­bil­i­ty Prin­ci­ple. You can move the prod­uct cre­ation code into one place in the pro­gram, mak­ing the code eas­ier to support.

  3. Open/Closed Prin­ci­ple. You can intro­duce new types of prod­ucts into the pro­gram with­out break­ing exist­ing client code

  • Pros:
  1. The code may become more com­pli­cat­ed when you need to intro­duce a lot of new sub­class­es to imple­ment the pat­tern

That’s it hopefully you got it and it was simple and clear for you, if you found this article helpful please leave some claps and share it so everyone can benefit from it too

Greetings to SwiftCairo ❤️🇪🇬