Factory Pattern

========================

What is the Factory Pattern?

=====================================================

The Factory Pattern is a design pattern that provides an interface for creating objects, but allows subclasses to alter the type of object being created without affecting the rest of the program.

Purpose


The primary purpose of the Factory Pattern is to decouple object creation from their usage, allowing for greater flexibility and reusability. It enables you to create objects in different ways while maintaining a single interface, making it easier to extend or modify existing code.

Key Components


1. Factory Interface


The factory interface defines the contract for creating objects without specifying the exact class of object that will be created. This interface typically includes methods such as createObject(), createType() or createClass() that allow clients to request specific types of objects.

interface FactoryInterface {
    createObjectType(): Object;
    createClass(): Class;
}

2. Concrete Factories


The concrete factories implement the factory interface and provide the actual classes that will be used to create objects. Each concrete factory has its own implementation details, making it easy to add new types of objects without modifying existing code.

class AnimalFactory {
    createObjectType() {
        return Animal.class;
    }
}

class DogFactory {
    createObjectType() {
        return Dog.class;
    }
}

3. Object Creation


The object creation is handled by the client, which requests a specific type of object through an instance of the factory interface.

class Client {
    private final FactoryInterface factory;

    public Client(FactoryInterface factory) {
        this.factory = factory;
    }

    public Object createObjectType() {
        return factory.createObject();
    }
}

Advantages


1. Increased Flexibility


The Factory Pattern allows for greater flexibility in object creation, enabling you to easily add new types of objects without modifying existing code.

2. Reduced Coupling


By decoupling object creation from their usage, the Factory Pattern reduces coupling between classes and improves maintainability.

Example Use Case


Here’s an example use case in Java that demonstrates the Factory Pattern:

// Animal interface
interface Animal {
    void sound();
}

// Concrete animal classes
class Dog extends Animal {
    @Override
    public void sound() {
        System.out.println("Woof!");
    }
}

class Cat extends Animal {
    @Override
    public void sound() {
        System.out.println("Meow!");
    }
}

// Animal factory interface
interface FactoryInterface {
    Animal createObjectType();
}

// Concrete animal factories
class DogFactory implements FactoryInterface {
    @Override
    public Animal createObjectType() {
        return new Dog();
    }
}

class CatFactory implements FactoryInterface {
    @Override
    public Animal createObjectType() {
        return new Cat();
    }
}

// Client class that uses the factory interface
public class Main {
    public static void main(String[] args) {
        FactoryInterface animalFactory = new DogFactory();

        // Create an instance of the created type (Dog)
        Animal animal = animalFactory.createObject();

        // Call the sound method on the created object (Dog)
        animal.sound();
    }
}

In this example, we define an Animal interface and two concrete classes (Dog and Cat) that extend it. We also implement a factory interface FactoryInterface that defines the contract for creating objects of type Animal. Two concrete factories (DogFactory and CatFactory) implement the factory interface, providing instances of their respective classes when requested. The client class uses the factory interface to create an instance of the created type (Dog).