Logo

dev-resources.site

for different kinds of informations.

Javascript Factory Design Pattern

Published at
3/20/2023
Categories
javascript
designpatterns
factory
programming
Author
bvnkumar
Author
8 person written this
bvnkumar
open
Javascript Factory Design Pattern

Design patterns are templates or solutions for common software development problems.

Design patterns are divided into three categories.

  1. Behavioral
  2. Creational
  3. Structural

The factory pattern is a creational design pattern that provides a generic interface for creating objects.

EX:-

function Developer(name , experience) {
    this.name = name;
    this.type = "Developer";
    this.experience = experience;
}
function Tester(name, experience) {
    this.name = name;
    this.type = "Tester";
    this.experience = experience;
}
function EmployeeFactory() {
    this.create = (name, type) => {
        switch (type) {
            case 1:
                return new Developer(name,4);
                break;
            case 2:
                return new Tester(name,6);
                break;
        }
    }
}

function say() {
    console.log("i am " + this.name + " i am a " + this.type +" and having a "+ this.experience +"years of experience ");
}

let employees = [];
const employeeFactory = new EmployeeFactory();
employees.push(employeeFactory.create("Sam", 1));
employees.forEach((employee) => {
    say.call(employee);
});
Enter fullscreen mode Exit fullscreen mode

As you can see, we have created developer and Tester objects with their names. Using this pattern you could create as many objects you require each with their own name and experience.

factory Article's
25 articles in total
Favicon
Clojure is Awesome!!!
Favicon
Understanding the Factory and Factory Method Design Patterns
Favicon
Factory Design Pattern
Favicon
Things You Want to Ensure When Factory Resetting Your HP Laptop Without a Password
Favicon
The Factory Pattern in C#: Creating Objects the Smart Way
Favicon
Enhance Your Factory Car Radio's Bass with Affordable Upgrades
Favicon
Page Object Model and Page Factory in Selenium
Favicon
Factory functions with private variables in JavaScript
Favicon
Evoluindo nosso Projeto Rails: Integrando o Padrão Factory para Maior Flexibilidade e Organização
Favicon
Javascript Factory Design Pattern
Favicon
Dart Abstract and Factory Keywords
Favicon
Azure Data Factory Overview For Beginners
Favicon
Improve your factories in Symfony
Favicon
Reduzindo a quantidade de Branchs na criação de Objetos com uma estrutura plugável
Favicon
Java 9 Factory Method for Collections: List, Set, Map
Favicon
How to implement simple Factory Pattern in Node.js
Favicon
Factory Design Pattern (simple example implementation in PHP)
Favicon
Spring's FactoryBean Interface
Favicon
Custom Validators for Angular Reactive Forms
Favicon
Design Patterns: Factory
Favicon
Creating objects dynamically with factory pattern in javascript
Favicon
Is this the real life, is this just fantasy?
Favicon
Design Patterns: Factory Pattern, Part 2
Favicon
Patterns in Kotlin: Abstract Factory
Favicon
SUT Factory

Featured ones: