noscript

The Abstract Factory design pattern in Java is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern allows you to create objects that are related to each other by a common theme, such as a factory for creating different types of GUI components like buttons, text fields, and menus.

The super-factory that gives rise to additional factories is the center of abstract factory patterns. Another name for this facility is the factory of factories. This kind of design pattern is classified as a creational pattern since it offers one of the most effective methods for producing an object.

An interface in the Abstract Factory design is in charge of building a factory of related objects without specifically defining their classes. According to the Factory pattern, each produced factory can provide the objects.

Components of Abstract Factory Patterns.

1. Abstract Factory

A collection of guidelines for building families of linked objects without defining their specific classes is defined by Abstract Factory, which functions as a high-level blueprint. It defines a number of methods, each in charge of producing a specific kind of object, and makes sure that concrete factories follow a standard interface, making it possible to create connected collections of objects in a consistent manner.

2. Concrete Factories

Concrete Factories carry out the directives provided by the abstract factory. It includes the reasoning behind producing certain examples of things within a family. Additionally, there may be several concrete factories, each designed to provide a unique family of related products.

3. Abstract Products

A family of related items is represented by Abstract Products, which define a set of shared characteristics or operations. It serves as an interface type or abstract that all concrete products in a family must follow and offers a standard method for concrete products to be utilized interchangeably.

4. Concrete Products

These are real-life examples of things made in concrete factories. Their implementation of the methods specified in the abstract products guarantees consistency within a family, and they are part of a particular category or related object family.

5. Client

The client uses abstract products’ abstract interfaces to interact with objects, creating families of items without defining their concrete types through the use of the abstract factory. By altering the concrete factory instance, the client can effortlessly swap between object families, providing them with flexibility.

Example :

// Abstract product A
interface Button {
    void render();
}
// Concrete product A1
class WindowsButton implements Button {
    @Override
    public void render() {
        System.out.println(“Rendering Windows button”);
    }
}
// Concrete product A2
class MacOSButton implements Button {
    @Override
    public void render() {
        System.out.println(“Rendering MacOS button”);
    }
}
// Abstract product B
interface TextField {
    void render();
}
// Concrete product B1
class WindowsTextField implements TextField {
    @Override
    public void render() {
        System.out.println(“Rendering Windows text field”);
    }
}
// Concrete product B2
class MacOSTextField implements TextField {
    @Override
    public void render() {
        System.out.println(“Rendering MacOS text field”);
    }
}
// Abstract factory
interface GUIFactory {
    Button createButton();
    TextField createTextField();
}
// Concrete factory 1
class WindowsFactory implements GUIFactory {
    @Override
    public Button createButton() {
        return new WindowsButton();
    }
    @Override
    public TextField createTextField() {
        return new WindowsTextField();
    }
}
// Concrete factory 2
class MacOSFactory implements GUIFactory {
    @Override
    public Button createButton() {
        return new MacOSButton();
    }
    @Override
    public TextField createTextField() {
        return new MacOSTextField();
    }
}
// Client code
public class Client {
    private GUIFactory factory;
    public Client(GUIFactory factory) {
        this.factory = factory;
    }
    public void createUI() {
        Button button = factory.createButton();
        button.render();
        TextField textField = factory.createTextField();
        textField.render();
    }
    public static void main(String[] args) {
        Client windowsClient = new Client(new WindowsFactory());
        windowsClient.createUI();
        Client macOSClient = new Client(new MacOSFactory());
        macOSClient.createUI();
    }
}

                                                              For more information & classes Call: 2048553004
                                                                      Registration Link: Click Here!

Author: Ambarish Durani

JAVA Trainer

IT Education Centre Placement & Training Institute

© Copyright 2024 | IT Education Centre.