Abstract Factory Design Pattern In Java

By | | Updated : 2021-12-07 | Viewed : 166 times

Abstract Factory Design Pattern In Java

Abstract Factory Design Pattern is one of the creational categories which we are going to learn here. The tutorial is to learn what Abstract Factory Design Pattern is and will learn about Abstract Factory Design Pattern with example.

What Is Abstract Factory Design Pattern?

An Abstract Factory Pattern contains a method to produce the Factories of different types of object creation. Abstract Factory Pattern can be considered factories of factories. As the Abstract Factory Design pattern does create the instances as like as Factory Method. Unlike Factory Method, Abstract Factory creates the factories. In Abstract Factory Pattern, factories can produce the instances. It comes under the creational design pattern as it creates the objects.

Factory Method Vs Abstract Factory

Both patterns can contain a method in which the implementations logic exists for creating the instances. But The factory design pattern does create only instances of the same type whereas the Abstract Factory Design Pattern creates factories of different types for instantiation. And again these Factories can create objects of the same type.

Abstract Factory Design Pattern Example In Java

Rules For Abstract Factory Pattern Implementation

We will now know rules/steps for implementation of Abstract Factory

  1. Define Cake and Candy Interfaces

  2. Define implemented classes for Cake and Candy

  3. Define Factory Classes such as CakeFacoty and CandyFactory

  4. Define Abstract Factory class and Its implementation

  5. Define A Client for Abstract Factory

Now we will try to follow the above rules for implementing the Abstract Factory Pattern.

Define Cake and Candy Interfaces

Cake.java
public interface Cake {
    Long getCakePrice();
}
Candy.java
public interface Candy {
    Long getCandyPrice();
}

Define implemented classes for Cake and Candy

BlackForest.java
public class BlackForest implements Cake {

    public Long price;

    public BlackForest(Long price) {
        this.price = price;
    }

    @Override
    public Long getCakePrice() {
        return price;
    }
}
ChocolateTruffle.java
public class ChocolateTruffle implements Cake {

    public Long price;

    public ChocolateTruffle(Long price) {
        this.price = price;
    }

    @Override
    public Long getCakePrice() {
        return price;
    }
}
StrawBerryCrush.java
public class StrawBerryCrush implements Cake {
    public Long price;

    public StrawBerryCrush(Long price) {
        this.price = price;
    }

    @Override
    public Long getCakePrice() {
        return price;
    }
}
CandyBar.java
public class CandyBar implements Candy {
    public Long price;

    public CandyBar(Long price) {
        this.price = price;
    }

    @Override
    public Long getCandyPrice() {
        return price;
    }
}
Chocolate.java
public class Chocolate implements Candy {
    public Long price;

    public Chocolate(Long price) {
        this.price = price;
    }

    @Override
    public Long getCandyPrice() {
        return price;
    }
}
Jelly.java
public class Jelly implements Candy {
    public Long price;

    public Jelly(Long price) {
        this.price = price;
    }

    @Override
    public Long getCandyPrice() {
        return price;
    }
}

Define Factory Classes such as CakeFacoty and CandyFactory

CakeFactory.java
public class CakeFactory extends FoodFactory {
    @Override
    public Cake makeCake(String cakeName) {
        Cake cake = null;
        if (cakeName.equalsIgnoreCase("BlackForest")) {
            cake = new BlackForest(600l);
            System.out.println("Ordered cake is: BlackForest and price is:" + cake.getCakePrice() + "$");
        } else if (cakeName.equalsIgnoreCase("StrawBerryCrush")) {
            cake = new StrawBerryCrush(500l);
            System.out.println("Ordered cake is: StrawBerryCrush and price is:" + cake.getCakePrice() + "$");
        } else if (cakeName.equalsIgnoreCase("ChocolateTruffle")) {
            cake = new ChocolateTruffle(650l);
            System.out.println("Ordered cake is: ChocolateTruffle and price is:" + cake.getCakePrice() + "$");
        } else {
            System.out.println("The order cake is not presented now.");
        }
        return cake;
    }


    @Override
    public Candy makeCandy(String candyName) {
        return null;
    }
}
CandyFactory.java
public class CandyFactory extends FoodFactory {

    public Candy makeCandy(String CandyName) {
        Candy candy = null;
        if (CandyName.equalsIgnoreCase("Jelly")) {
            candy = new Jelly(6l);
            System.out.println("Ordered Candy is: Jelly and price is:" + candy.getCandyPrice() + "$");
        } else if (CandyName.equalsIgnoreCase("CandyBar")) {
            candy = new CandyBar(5l);
            System.out.println("Ordered Candy is: CandyBar and price is:" + candy.getCandyPrice() + "$");
        } else if (CandyName.equalsIgnoreCase("BubbleGum")) {
            candy = new BubbleGum(3l);
            System.out.println("Ordered Candy is: BubbleGum and price is:" + candy.getCandyPrice() + "$");
        } else {
            System.out.println("The Ordered Candy is not presented now.");
        }
        return candy;
    }

    @Override
    public Cake makeCake(String cakeName) {
        return null;
    }

}

Define Abstract Factory class and Its implementation

FoodFactory.java
public abstract class FoodFactory {
    public abstract Cake makeCake(String cakeName);

    public abstract Candy makeCandy(String candyName);
}
FactoryProducer.java
public class FactoryProducer {
    public FoodFactory getFactory(String foodType) {
        if (foodType.equalsIgnoreCase("Cake")) {
            return new CakeFactory();
        } else if (foodType.equalsIgnoreCase("Candy")) {
            return new CandyFactory();
        }
        return null;
    }
}

Define A Client for Abstract Factory

AbstractFactoryDemo.java
public abstract class AbstractFactoryDemo {
    public static void main(String[] args) {
        FactoryProducer producer = new FactoryProducer();
        producer.getFactory("Cake").makeCake("BlackForest");
        producer.getFactory("Candy").makeCandy("Jelly");
    }
}

Once you run the AbstractFactoryDemo you will get different types of objects.

Leave A Reply