This module will help you to have consistent implementations of the abstract factory pattern.
Extend the Factory base class in your own factory and implement the getClass static method with your business logic.
Example
This is the example in the tests.
'use strict'; const Factory = ; /* eslint-disable global-require */ /** * @classdesc * A factory class to get a fruit. * * @class FruitFactory */ /** * Decide which fruit to instantiate based on the size and sugar. * * @param * Contains the keys: 'size' and 'sugar'. * * @throws Error * If no fruit could be found. * * @return * The fruit to instantiate. */ static { if typeof contextsize === 'undefined' || typeof contextsugar === 'undefined' throw 'Unable to find fruit.'; if contextsize >= 5 // This is a big fruit. return contextsugar >= 5 ? : ; return contextsugar >= 5 ? : ; } moduleexports = FruitFactory;
Use it:
const fruit = FruitFactory;console;// Outputs 'Mango'.