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

Semantic Versioning NodeJS
admin07/07/2023

Semantic Versioning NodeJS
How to Use Semantic Versioning in NPM
Microservice in a Monorepo
admin22/06/2023

Microservice in a Monorepo
Microservice in a Monorepo
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.
Mới nhất

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 - Proxy
admin11/08/2023

TypeScript Design Pattern - Proxy
Provide a surrogate or placeholder for another object to control access to it.
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.
Đ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