CategoryTagArticle

admin

I'm a Full-stack developer

Tag

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
TypeScript Design Pattern - Prototype
Published date: 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

Recommend

State management with redux in Flutter
admin14/06/2023

State management with redux in Flutter
In this article I will show an example to manage state in Flutter application using Redux.
Part 1: Build a Chat App with ReactJS + Material UI
admin13/09/2023

Part 1: Build a Chat App with ReactJS + Material UI
In this article, I would like to introduce using ReactJS and material UI to build a Chat App.
TypeScript Design Pattern - Singleton
admin07/08/2023

TypeScript Design Pattern - Singleton
The singleton ensures only a single install is created in a class and provides a method to access this instance everywhere in a codebase.
Newest

JOI - API schema validation
admin12/06/2023

JOI - API schema validation
Data validation is one of topics that I am interesting. I always review my code after developed features or fixed bugs. There are many places where need to validate data, it is really terrible. Some cases, we need to validate data input because ensure the data into API, it will not make any problems to crash system.
Data structure: Singly Linked List
admin07/04/2024

Data structure: Singly Linked List
In this article, I will show you how to set up a singly linked list algorithm in Python.
TypeScript Design Pattern - Builder
admin07/08/2023

TypeScript Design Pattern - Builder
TypeScript Design Pattern - Builder
Đ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
Feedback
Name
Phone number
Email
Content
Download app
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About