madreloidpx / personal-website

madreloidpx.vercel.app
0 stars 0 forks source link

Factory Pattern #2

Open madreloidpx opened 2 years ago

madreloidpx commented 2 years ago

A Factory Pattern or Factory Method Pattern simply creates an interface or abstract class for constructing an object and lets the subclasses determine which class to instantiate, according to the pattern. Subclasses, in other terms, are in charge of producing the class instance. A class can use the Factory Method to defer instantiation to subclasses.

A superclass defines all standard and general behavior (using pure virtual "placeholders" for creation steps) and then delegates the creation details to client-supplied subclasses. The Factory Method makes a design more adjustable while also adding a layer of complexity. Other design patterns necessitate the creation of new classes, whereas the Factory Method just necessitates the creation of a new action. We generate items without disclosing the creation mechanism to the client in the Factory design, and we refer to newly formed objects using a common interface.

Sub-classes can select the object they want to produce using the Factory Method Pattern. It encourages loose coupling by removing the need for application-specific classes to be bound into the code. That is, the code only interacts with the resulting interface or abstract class, and it will function with any classes that implement or extend that interface or abstract class.

image