You can add an annotation for the this type of the static method. In this case this will refer to the class, and adding an annotation for the this parameter will make the static method visible only on classes that satisfy the constraint (in this case that a constructor with a single argument exists) and it will also help us extract the actual instance type of the class the method is invoked on 

a factory class with the static method responsible for creating the specific classes.

an abstract class with the shared data, logic and the required abstract components to implement.

any extending classes with implementation of the corresponding abstract properties and methods from the parent class.



What are Design Patterns?

Design Patterns are general, reusable solutions to problems that are occurring over and over again in different environments. In Software Engineering, they help you to write better code more quickly. Using those general solutions can save us time and work because we reuse many other developers’ accumulated knowledge and experience.
“[…] it is uncommon for me to encounter developers who have not at least heard of the patterns movement” — Grady Booch in Design Patterns: Elements of Reusable Object-Oriented Software
Maybe you worked as a programmer for years without applying or even knowing a single pattern. I bet many developers out there do precisely that. If so, I highly recommend reading this article first to convince your brain that software design patterns are essential:

Factory Use Case

An example use case may be a user interface where the user can select from a menu of items, such as chairs.

The user has been given a choice using some kind of navigation interface, and it is unknown what choice, or how many chairs the user will add until the application is actually running, and the user starts using it.

So, when the user selected the chair, the factory then takes some property involved with that selection, such as an ID, Type or other attribute and then decides which relevant subclass to instantiate in order to return the appropriate object.

While there are is a large amount of code in this example, and it is spread across several files, the actual factory is the ChairFactory class in the file chair-factory.ts. So, the factory is the part of your program that is creating a separation or abstraction between the instantiating of your object and where it is used.

Sample


Summary

The Factory Pattern defers the creation of the final object to a subclass.

  • The Factory pattern is about inserting another layer/abstraction between instantiating an object and where in your code it is actually used.
  • It is unknown what or how many objects you will need to be created until runtime.
  • You want to localize knowledge of the specifics of instantiating a particular object to the subclass so that the client doesn't need to be concerned about the details.
  • You want to create an external framework, that an application can import/reference, and hide the details of the specifics involved in creating the final object/product.
  • The unique factor that defines the Factory pattern, is that your project now defers the creation of objects to the subclass that the factory had delegated it to.