Abstract Design Pattern in Java #5

Reading Time: 2 minutes

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?

  1. It is one of creational type design which provides with an interface for creating related objects without specifying the concrete classes.
  2. 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?

  1. 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.
  2. The factory design pattern uses inheritance, while the abstract design pattern uses composition and delegate the responsibility of creating an object to another class.
  3. 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.

Implementation

Step 1: Create abstract and concrete classes

public abstract class Pizza {
 public abstract void bakePizza();
}

public class CheesePizza extends Pizza {
 @Override public void bakePizza() {
 //TODO
 System.out.println(“Bake Cheese Pizza”);
}
}
public class VeggiePizza extends Pizza {
 @Override public void bakePizza() {
   //TODO
 System.out.println(“Bake Veggie Pizza”);
}
}

 

Step 2 : Create Pizza AbstractFactory interface or abstract class
public interface PizzaAbstractFactory {
public Pizza orderPizza();
}


Step 3: Create Factory class for each subclass
public class CheesePizzaFactory implements PizzaAbstractFactory { @Override public Pizza orderPizza() {
return new CheesePizza();
}

public class VeggiePizzaFactory implements PizzaAbstractFactory { @Override public Pizza orderPizza() {
return new VeggiePizza();
}

Step 4: Create a consumer class that will provide the entry point for the client classes to create sub-classes.

public class PizzaFactory {
public static getPizza(PizzaAbstractFactory factory){
return factory.orderPizza();
}
}


Step 5: Write a simple test method and see how to use the abstract factory to get the instance of sub-classes.

public class DesignPatternExample {
public static void main(String[] args) {
Pizza cheesePizza = PizzaFactory.getPizza(new CheesePizzaFactory());
Pizza veggiePizza = PizzaFactory.getPizza(new VeggiePizzaFactory());
}
}

Conclusion

So in this blog, we have understood the abstract factory design pattern and how this design pattern makes our code robust and adds a layer of abstraction over the Factory design pattern.

Please feel free to provide your suggestions. 🙂 Please comment if you have some queries and want to add some points.

References:
http://www.journaldev.com
https://javarevisited.blogspot.com

blog-footer

Leave a Reply