Tuesday, 10 December 2024

Overview of JavaScript Abstract Factory Method Design Pattern

 The Abstract Factory Method in JavaScript follows the same core principles as in other object-oriented languages, enabling the creation of families of related or dependent objects without specifying their concrete classes. This pattern allows you to work with product families in a way that decouples the client from their concrete implementations.

Example of the Abstract Factory Pattern in JavaScript:

We'll create a simple furniture factory that can produce different types of chairs and sofas for different styles, such as Modern and Victorian.

1. Abstract Factory (Interface for creating products)

We define the abstract factory class. In JavaScript, this is often done with a base class or a constructor function.

class FurnitureFactory {

    createChair() {

        throw new Error("Method 'createChair()' must be implemented.");

    }

    createSofa() {

        throw new Error("Method 'createSofa()' must be implemented.");

    }

}

2. Concrete Factories (Implementations of the abstract factory)

Now, we create two concrete factories that implement the abstract methods to produce different types of furniture (Modern or Victorian).

class ModernFurnitureFactory extends FurnitureFactory {

    createChair() {

        return new ModernChair();

    }

    createSofa() {

        return new ModernSofa();

    }

}

 

class VictorianFurnitureFactory extends FurnitureFactory {

    createChair() {

        return new VictorianChair();

    }

 

    createSofa() {

        return new VictorianSofa();

    }

}

3. Abstract Products (Interfaces for product types)

We also define the abstract products (chair and sofa). These will act as blueprints for the specific products in each family.

class Chair {

    sitOn() {

        throw new Error("Method 'sitOn()' must be implemented.");

    }

}

class Sofa {

    lieOn() {

        throw new Error("Method 'lieOn()' must be implemented.");

    }

}

4. Concrete Products (Specific implementations of the products)

Next, we create the concrete product classes (Modern and Victorian).

class ModernChair extends Chair {

    sitOn() {

        console.log("Sitting on a modern chair");

    }

}

 

class ModernSofa extends Sofa {

    lieOn() {

        console.log("Lying on a modern sofa");

    }

}

 

class VictorianChair extends Chair {

    sitOn() {

        console.log("Sitting on a Victorian chair");

    }

}

 

class VictorianSofa extends Sofa {

    lieOn() {

        console.log("Lying on a Victorian sofa");

    }

}

5. Client Code (The client interacts with the abstract factory and products)

The client can now interact with the abstract factory and does not need to know the concrete implementations of the products. The client can use any factory (Modern or Victorian) and still get consistent results.

function clientCode(factory) {

    const chair = factory.createChair();

    const sofa = factory.createSofa();

 

    chair.sitOn();

    sofa.lieOn();

}

 

// Using Modern Furniture Factory

const modernFactory = new ModernFurnitureFactory();

clientCode(modernFactory);

 

// Using Victorian Furniture Factory

const victorianFactory = new VictorianFurnitureFactory();

clientCode(victorianFactory);

 

Explanation:

  1. FurnitureFactory is the abstract factory class that defines the methods createChair() and createSofa().

  2. ModernFurnitureFactory and VictorianFurnitureFactory are concrete factories that implement the abstract factory methods to produce specific products (Modern or Victorian).

  3. Chair and Sofa are abstract product classes defining the behavior of the products (methods sitOn() and lieOn()).

  4. ModernChair, ModernSofa, VictorianChair, and VictorianSofa are concrete product classes implementing the abstract product behavior.

Benefits of the Abstract Factory Pattern in JavaScript:

  • Decoupling: The client code does not depend on specific concrete classes, just on abstract classes or interfaces, making it more flexible.

  • Consistency: It ensures that products created by a factory (like a chair and sofa) are compatible with each other, maintaining a consistent family of objects.

  • Scalability: You can easily add new families of products (e.g., Scandinavian furniture) without changing the client code.

Drawbacks:

  • Increased Complexity: More classes and code are involved, which might make the system more complex and harder to understand.

  • Difficulty in Extending: If you want to add new types of products (e.g., tables), you would need to modify the abstract factory and potentially all concrete factories.

 

No comments:

Post a Comment