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

Part 3: React Fragments
admin18/06/2023

Part 3: React Fragments
In this part, I will show you about good benefits when using fragments in React.
Part 5: Creating a Tag List Page on Ghost CMS
admin17/06/2023

Part 5: Creating a Tag List Page on Ghost CMS
In this article, I will show you how to create a Tag list page using the Casper theme.
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
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.
TypeScript Design Pattern - Abstract Factory
admin07/08/2023

TypeScript Design Pattern - Abstract Factory
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.
Đ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