🧰 Developer’s toolbox: 🏭 Factory Method Design Pattern

Vee Lesyk
Dots and Spaces
Published in
1 min readAug 13, 2019

The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.

Wikipedia says:

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method — either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes — rather than by calling a constructor.

Real world example

Consider the case of currency creation. Where we want to create a currency object depending on the country.

Github repo: link.

Swift

FactoryMethod.swift

TypeScript

FactoryMethod.ts
Factory Method

P.S. We would be happy to see comments according to mistakes & typos.

--

--

Vee Lesyk
Dots and Spaces

#h+ #livemoredomore Adventurer. Unique experience wizard. Maker of things. Convergence commander. Information warlock. Problem solver. System: ☉; Planet: ♁.