Danh mụcThẻBài viết

admin

I'm a Full-stack developer

Thẻ

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
TypeScript Design Pattern - Prototype
Ngày đăng: 07/08/2023

What is a prototype pattern?


The prototype pattern is one of the Creational pattern groups. The responsibility is to create a new object through clone the existing object instead of using the new key. The new object is the same as the original object, and we can change its property does not impact the original object.


When should I use the prototype pattern?


The singleton pattern should be used when a developer wants to:


  • Initialize an object that is the same as the original object without using a new operator or constructor because we could be missing some properties of the original object leading to the new key does not use.
  • Initialize object at run-time.
  • The properties to init an object is complex.


How to implement


For example, we will define a class that has the responsibility to generate a CSV file with a header and name.


export class ExportCsvFile {
  public _name: string;
  public _headers: string[] = ['#', 'Username', 'Email'];
  
  constructor(name: string, headers: string[]) {
    this._name = name;
    this._headers = [...this._headers, ...headers];
  }


  public clone(): this {
    return Object.create(this);
  }
}


const csvFile1 = new ExportCsvFile('CSV 1', []);
console.log(csvFile1._name);
console.log(csvFile1._headers);


const csvFile2 = csvFile1.clone();
csvFile2._name = 'CSV 2'
csvFile2._headers = [...csvFile2._headers, 'Is Activate', 'Last Login'];
console.log(csvFile2._name);
console.log(csvFile2._headers);


Execution result

CSV 1

[ '#', 'Username', 'Email' ]

CSV 2

[ '#', 'Username', 'Email', 'Is Activate', 'Last Login' ]


Pros and Cons

Pros:


  • You can clone objects without coupling to their concrete classes.
  • You can get rid of repeated initialization code in favor of cloning pre-built prototypes.
  • You can produce complex objects more conveniently.
  • You get an alternative to inheritance when dealing with configuration presets for complex objects.


Cons:


  • Cloning complex objects that have circular references might be very tricky.


Wrapping Up


Thank you for reading, and happy coding!

I hope this article will help make the concepts of the Prototype Pattern

Đề xuất

TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
How to integrate ChatGPT-3.5 Turbo into Node.js
admin10/01/2024

How to integrate ChatGPT-3.5 Turbo into Node.js
Step-by-Step Guide to Incorporating ChatGPT-3.5 Turbo into Node.js for Basic ReactJS Applications
Mới nhất

Difference Between Stack and Queue
admin07/04/2024

Difference Between Stack and Queue
In the fundamental data structure, besides the linked list, the stack and queue are also used widely in computer science and programming.
TypeScript Design Pattern - Bridge
admin08/08/2023

TypeScript Design Pattern - Bridge
Decouple an abstraction from its implementation so that the two can vary independently.
TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
Đinh Thành Công Blog

My website, where I write blogs on a variety of topics and where I have some experiments with new technologies.

hotlinelinkedinskypezalofacebook
DMCA.com Protection Status
Góp ý
Họ & Tên
Số điện thoại
Email
Nội dung
Tải ứng dụng
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About