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 - Abstract Factory
Ngày đăng: 07/08/2023

What is an abstract factory pattern?

The abstract factory pattern is one of five design patterns in the Creational Design Pattern group. The abstract factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.


When should I use the abstract factory pattern?

The abstract factory pattern should be used when a developer wants to:

  • expose only the interface of the collection of multiple objects and not the implementation.
  • create objects without any background knowledge of the structure, composition, and architecture of the system.
  • creating multiple factories of related objects so that any specific object can be created at runtime from any of the concrete factory classes.


How to implement

Platform type:
enum EPlatform {
  IOS = 'iOS',
  ANDROID = 'android',
}


Platform interface:
interface IPlatform {
  generateToken(): string;
  destroyToken(): boolean;
}


Implements
export class IosPlatform implements IPlatform  {
  generateToken(): string {
    return 'iOS Token';
  }

  destroyToken(): boolean {
    return false;
  }
}


export class AndroidPlatform implements IPlatform {
  generateToken(): string {
    return 'Android Token';
  }

  destroyToken(): boolean {
    return true;
  }
}


Factory with a static method:
export class Platform {
  public static getPlatform(os: EPlatform): IPlatform {
    switch(os) {
      case EPlatform.IOS:
        return new IosPlatform();
      case EPlatform.ANDROID:
        return new AndroidPlatform();
      default:
        throw new Error('Operation system does not support!!!');
    }
  }
}


The client code:
function client() {
  const iOS = Platform.getPlatform(EPlatform.IOS);
  const android = Platform.getPlatform(EPlatform.ANDROID);

  console.log('iOS Token:', iOS.generateToken());
  console.log('Android Token:', android.generateToken());

  console.log('iOS Destroy Token:', iOS.destroyToken());
  console.log('Android Destroy Token:', android.destroyToken());
}


client();


Result:
iOS Token: iOS Token
Android Token: Android Token

iOS Destroy Token: false
Android Destroy Token: true


Pros and Cons

Props:

  • You can be sure that the products you’re getting from a factory are compatible with each other.
  •  You avoid tight coupling between concrete products and client code.
  •  Single Responsibility Principle. You can extract the product creation code into one place, making the code easier to support.
  •  Open/Closed Principle. You can introduce new variants of products without breaking existing client code.


Cons:

  • The code may become more complicated than it should be since a lot of new interfaces and classes are introduced along with the pattern.


Wrapping Up


Thank you for reading, and happy coding!

I hope this article will help make the concepts of the Abstract Factory Pattern

Đề xuất

RESTful API - How to design?
admin09/06/2023

RESTful API - How to design?
In this article, I will describe the useful points of designing a good API.
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
NodeJS Verify and Decode Cognito JWT Tokens
admin12/06/2023

NodeJS Verify and Decode Cognito JWT Tokens
In this article, I will show you how to verify and decode the Cognito JWT Tokens token.
Mới nhất

Flutter push notification
admin14/06/2023

Flutter push notification
Welcome everyone, in this article, we will implement flutter push notification through APNs (Apple Push Notification Service) and FCM (Firebase Cloud Message), and manage messages sent using AWS SNS (Amazon Simple Notification Service).
TypeScript Design Pattern - Proxy
admin11/08/2023

TypeScript Design Pattern - Proxy
Provide a surrogate or placeholder for another object to control access to it.
How to secure your API gateway
admin17/04/2024

How to secure your API gateway
In this blog, I will cover the 6 methods that technology leaders need to incorporate to secure and protect APIs.
Đ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