Skip to content

Instantly share code, notes, and snippets.

@msankhala
Last active December 19, 2021 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msankhala/c469f2ccb4f4969d54e03001fae7d24c to your computer and use it in GitHub Desktop.
Save msankhala/c469f2ccb4f4969d54e03001fae7d24c to your computer and use it in GitHub Desktop.
Factor pattern js pattern
import Motorvehicle from './Motorvehicle';
import Aircraft from './Aircraf';
import Railvehicle from './Railvehicle';
const VehicleFactory = (type, make, model, year) => {
if (type === car) {
return new Motorvehicle('car', make, model, year);
} else if (type === airplane) {
return new Aircraft('airplane', make, model, year);
} else if (type === helicopter) {
return new Aircraft('helicopter', make, model, year);
} else {
return new Railvehicle('train', make, model, year);
}
}
module.exports = VehicleFactory;
const audiAllRoad = VehicleFactory('car', 'Audi', 'A6 Allroad', '2020');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment