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

Semantic Versioning NodeJS
admin07/07/2023

Semantic Versioning NodeJS
How to Use Semantic Versioning in NPM
TypeScript Design Pattern - Prototype
admin07/08/2023

TypeScript Design Pattern - Prototype
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.
Create Cognito User Pool with AWS CDK
admin09/06/2023

Create Cognito User Pool with AWS CDK
In the previous post, I showed you how to create a simple S3 bucket. Next, in this article, I will guide you to create a Cognito User Pool.
Mới nhất

Part 2: Setup Custom Domain Zone + SSL for Ghost on AWS Lightsail
admin17/06/2023

Part 2: Setup Custom Domain Zone + SSL for Ghost on AWS Lightsail
In this section, I will continue to show you how to point Ghost Instance Static IP to your domain.
TypeScript Design Pattern - Proxy
admin11/08/2023

TypeScript Design Pattern - Proxy
Provide a surrogate or placeholder for another object to control access to it.
What are the SOLID principles?
admin17/06/2023

What are the SOLID principles?
If we want to have good software, the infrastructure should be good first. We should learn more techniques that help to have better quality.
Đ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