Abstract Factory
It provides an interface/contract to create family of related objects. (Like an interface for a Factory)
Use Absctract Factories, when you don't want to couple your code to a distinct one, the each distinct Factory will always be injected.
This also means that Abstract Factories cannot work statically.
On the contrary to Static Factory Methods, where the create() will receive as parameter the type of distinct object it should create, Abstract Factories will decide which distinct object by dependincy injection.
As a downside, it will require more bureocracy, as more interfaces will have to be made.
Also you can't work with the Factory statically, and each distinct Factory will have to be instanciated.
Explicitly declare interface/class for each distinct object from the same family.
interface Chair {
color: string;
price(): number;
}
class ChairOffice implements Chair {
color: string;
price(): number { ... }
}
class ChairTable implements Chair {
color: string;
price(): number { ... }
}Declare the Absctract Factory interface. (This interface has the creation method that returns the abstract type)
Then on each concrete Factory class, the creation method will return the distinct object.
Wherever you use the Factory, you always use the Abstract one.
The distinct Factory will then be injected.
Last updated