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 - Abstract Factory
Published date: 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

Recommend

Part 2: Setup Custom Domain Zone + SSL for Ghost on AWS Lightsail
admin17/06/2023

Part 2: Setup Custom Domain Zone + SSL for Ghost on AWS Lightsail
In this section, I will continue to show you how to point Ghost Instance Static IP to your domain.
Microservice in a Monorepo
admin22/06/2023

Microservice in a Monorepo
Microservice in a Monorepo
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

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.
TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
What are the SOLID principles?
admin17/06/2023

What are the SOLID principles?
If we want to have good software, the infrastructure should be good first. We should learn more techniques that help to have better quality.
Đ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