Reader small image

You're reading from  TypeScript 4 Design Patterns and Best Practices

Product typeBook
Published inSep 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781800563421
Edition1st Edition
Right arrow
Author (1)
 Theofanis Despoudis
Theofanis Despoudis
author image
Theofanis Despoudis

Theo Despoudis lives in Ireland, where he works as a Software Engineer for WP Engine and as a part-time tech practitioner for Fixate. He is the co-author of The React Workshop and Advanced Go Programming in 7 Days, Dzone Core Member, and maintains some open source projects on GitHub. Theo is available for conference talks, independent consulting, and corporate training services opportunities.
Read more about Theofanis Despoudis

Right arrow

Factory method pattern

The fourth design pattern that you will learn now is the Factory method. This pattern deals with the creation of objects and particularly with delegating the creation of objects using sub-classes. The objects you want to create usually share a common characteristic; they are similar in nature or in type, or they are part of a hierarchy.

You use an interface with a distinct create method and then you provide concrete classes that implement this factory and construct objects of a particular sub-class. Then this factory interface can be used in places where you have hardcoded types in parameters or variables.

A factory object is an abstraction that is responsible for creating objects. The way that it creates them though is the key differentiator. When you have multiple types of objects that either inherit from a similar class or have a similar role, then you may find that passing each type as a parameter is cumbersome. You will have to create all those different...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
TypeScript 4 Design Patterns and Best Practices
Published in: Sep 2021Publisher: PacktISBN-13: 9781800563421

Author (1)

author image
Theofanis Despoudis

Theo Despoudis lives in Ireland, where he works as a Software Engineer for WP Engine and as a part-time tech practitioner for Fixate. He is the co-author of The React Workshop and Advanced Go Programming in 7 Days, Dzone Core Member, and maintains some open source projects on GitHub. Theo is available for conference talks, independent consulting, and corporate training services opportunities.
Read more about Theofanis Despoudis