How to implement simple Factory Pattern in Node.js
nodejs
131
factory
2
Pattern
8
Male avatar

loveprogramming viết ngày 03/05/2021

https://grokonez.com/design-pattern/implement-simple-factory-method-pattern-node-js-example

How to implement simple Factory Pattern in Node.js

Instead of using class constructors or new keyword to create an object of a class, we can abstract this process. So, we can determine the type of object at run-time, by the time of generating that class object. The implementation seems like Factory Method, but simpler than Factory Method. This simple Factory is not treated as a standard GoF design pattern, but the approach is common to any place where we want to separate the code that varies a lot from the code that does not vary.

In this tutorial, grokonez shows you how to do it in NodeJs.

Nodejs simple Factory Pattern example

Overview

In this example, we have 3 types of cars: Audi, BMW, Mercedes. The car generating process depends on the input string "Audi" or "BMW" or "Mercedes".

CarFactory is the Factory class. In the client, we won't use new keyword but create() method to create a new car:


const BMW = CarFactory.create('BMW');

Sample Structure

nodejs-simple-factory-pattern-example-sample-structure

Implement simple Factory Pattern in Nodejs

Create default class

This Car class is a parent class for Audi, BMW, Mercedes class that we're gonna create in the next steps.

default_car.js


class Car {
    constructor(name) {
        this.name = name + '-' + Math.random().toString(36).substring(2, 15);
    }

    showInfo() {
        console.log(`I'm ${this.name}`)
    }
}

module.exports = Car;

This class has constructor method that assign a generated id to the car's name and a showInfo() method to show the name field.

Create subclasses of default class

Now we create 3 classes: Audi, BMW, Mercedes that extends default Car class above.

More at:

https://grokonez.com/design-pattern/implement-simple-factory-method-pattern-node-js-example

How to implement simple Factory Pattern in Node.js

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

444 bài viết.
77 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
White
11 1
Chức năng pattern matching trong C++ thấy nói sẽ kill (Link). Cách viết C++ do vậy sẽ thay đổi tương đối. Tự nhiên tôi thấy phải nhìn lại visitor ...
cpplover viết gần 6 năm trước
11 1
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
444 bài viết.
77 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!