admin

I'm a Full-stack developer

Thẻ

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

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.
TypeScript Design Pattern - Builder
admin07/08/2023

TypeScript Design Pattern - Builder
TypeScript Design Pattern - Builder
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

TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
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.
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.