In this blog, we will discuss Abstract Design Pattern, its example, and how it is different from the Factory Design Pattern. But, it’s important to have an understanding of the Factory Design Pattern first. You can refer to my blog from here.

What is Abstract Factory Design Pattern?

It is one of creational type design which provides with an interface for creating related objects without specifying the concrete classes.
It is also known as the factory of factories as we have to create a super factory that creates factories.
Remember the pizzaFactory class

public class SimplePizzaFactory {

public Pizza createPizza(String type) {
    Pizza pizza = null;

    if (type.equals("cheese")) {
        pizza = new CheesePizza();
    } else if (type.equals("pepperoni")) {
        pizza = new PepperoniPizza();
    } else if (type.equals("clam")) {
        pizza = new ClamPizza();
    } else if (type.equals("veggie")) {
        pizza = new VeggiePizza();
    }
    return pizza;
}

}
With the Factory pattern, we have to write the conditional logic to create the instance of a subclass, but abstract Factory pattern creates factory class for each subclass.

How Abstract Factory design pattern different from factory pattern?
As both the design pattern decouples the client from creating an object, but the abstract design pattern adds another level of abstraction over the Factory pattern.
The factory design pattern uses inheritance, while the abstract design pattern uses composition and delegate the responsibility of creating an object to another class.
Abstract Factory Design pattern creates a factory , but the factory design pattern creates a Product. For example, Pizza Factory produces a single product pizza with different varieties.

#java #design pattern #java

Abstract Design Pattern in Java
1.35 GEEK