Easy Tutorial
❮ Builder Pattern Chain Of Responsibility Pattern ❯

Abstract Factory Pattern

The Abstract Factory Pattern is centered around a super-factory that creates other factories. This super-factory is also known as a factory of factories. This type of design pattern falls under the category of creational patterns, as it provides one of the best ways to create objects.

In the Abstract Factory Pattern, an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can provide objects according to the factory pattern.

Introduction

Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Main Solution: Address the issue of interface selection.

When to Use: When the system has more than one product family and the system only consumes one family of products.

How to Solve: Define multiple products within a product family.

Key Code: Aggregate multiple similar products within a factory.

Example: After starting work, you might have multiple sets of clothes for different gatherings, such as business attire (a set of specific products), fashion attire (a set of specific products), or even business women's wear, business men's wear, fashion women's wear, and fashion men's wear for a family. These are all sets, i.e., a series of specific products. Suppose a scenario (though unrealistic, it helps illustrate the Abstract Factory Pattern) where in your home, a specific wardrobe (a concrete factory) can only store one type of clothing (a set, a series of specific products), and you always retrieve that set of clothes from that wardrobe. Using OOP principles, all wardrobes (concrete factories) are instances of a wardrobe class (an abstract factory), and each set of clothes includes specific items like a top (a specific product), pants (a specific product), which are instances of an abstract product like a top or pants.

Advantages: Ensures that clients only use objects from the same product family when multiple objects are designed to work together.

Disadvantages: Extending the product family is very difficult; adding a product to a series requires adding code in both the abstract Creator and the concrete implementations.

Usage Scenarios: 1. Changing the skin of QQ, changing the entire set at once.

  1. Generating programs for different operating systems.

Notes: It is difficult to extend product families but easy to extend product levels.

Implementation

We will create Shape and Color interfaces and concrete classes that implement these interfaces. Next, we create an abstract factory class AbstractFactory. Then, we define factory classes ShapeFactory and ColorFactory that extend AbstractFactory. We then create a factory generator/producer class FactoryProducer.

The AbstractFactoryPatternDemo class uses FactoryProducer to get an AbstractFactory object. It will pass shape information (CIRCLE / RECTANGLE / SQUARE) to AbstractFactory to get the type of object it needs. It also passes color information (RED / GREEN / BLUE) to AbstractFactory to get the type of object it needs.

Step 1

Create an interface for shapes.

Shape.java

public interface Shape {
   void draw();
}

Step 2

Create concrete classes implementing the interface.

Rectangle.java

Rectangle.java

public class Rectangle implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

Square.java

public class Square implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}

Circle.java

public class Circle implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}

Step 3

Create an interface for colors.

Color.java

public interface Color {
   void fill();
}

Step 4

Create concrete classes implementing the interface.

Red.java

public class Red implements Color {

   @Override
   public void fill() {
      System.out.println("Inside Red::fill() method.");
   }
}

Green.java

public class Green implements Color {

   @Override
   public void fill() {
      System.out.println("Inside Green::fill() method.");
   }
}

Blue.java

public class Blue implements Color {

   @Override
   public void fill() {
      System.out.println("Inside Blue::fill() method.");
   }
}

Step 5

Create an abstract class to get factories for Color and Shape objects.

AbstractFactory.java

public abstract class AbstractFactory {
   public abstract Color getColor(String color);
   public abstract Shape getShape(String shape);
}

Step 6

Create factory classes extending AbstractFactory to generate object of concrete class based on given information.

ShapeFactory.java

public class ShapeFactory extends AbstractFactory {

   @Override
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }

   @Override
   public Color getColor(String color) {
      return null;
   }
}

ColorFactory.java

public class ColorFactory extends AbstractFactory {

   @Override
   public Shape getShape(String shapeType){
      return null;
   }

   @Override
   public Color getColor(String color) {
      if(color == null){
         return null;
      }        
      if(color.equalsIgnoreCase("RED")){
         return new Red();
      } else if(color.equalsIgnoreCase("GREEN")){
         return new Green();
      } else if(color.equalsIgnoreCase("BLUE")){
         return new Blue();
      }
      return null;
   }
}

Step 7

Create a factory generator/producer class to get factories by passing an information such as type.

FactoryProducer.java

public class FactoryProducer {
   public static AbstractFactory getFactory(String choice){
      if(choice.equalsIgnoreCase("SHAPE")){
         return new ShapeFactory();
      } else if(choice.equalsIgnoreCase("COLOR")){
         return new ColorFactory();
      }
      return null;
   }
}

Step 8

Use FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing type information.

AbstractFactoryPatternDemo.java

public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {

      // Get shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

      // Get an object of Shape Circle
      Shape shape1 = shapeFactory.getShape("CIRCLE");

      // Call draw method of Shape Circle
      shape1.draw();

      // Get an object of Shape Rectangle
      Shape shape2 = shapeFactory.getShape("RECTANGLE");

      // Call draw method of Shape Rectangle
      shape2.draw();

      // Get an object of Shape Square 
      Shape shape3 = shapeFactory.getShape("SQUARE");

      // Call draw method of Shape Square
      shape3.draw();

      // Get color factory
      AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

      // Get an object of Color Red
      Color color1 = colorFactory.getColor("RED");

      // Call fill method of Red
      color1.fill();

      // Get an object of Color Green
      Color color2 = colorFactory.getColor("GREEN");

      // Call fill method of Green
      color2.fill();

      // Get an object of Color Blue
      Color color3 = colorFactory.getColor("BLUE");

      // Call fill method of Color Blue
      color3.fill();
   }
}

Color color1 = colorFactory.getColor("RED");

// Call the fill method of Red color1.fill();

// Get the object with color Green Color color2 = colorFactory.getColor("GREEN");

// Call the fill method of Green color2.fill();

// Get the object with color Blue Color color3 = colorFactory.getColor("BLUE");

// Call the fill method of Blue color3.fill(); } }



### Step 9

Run the program and output the results:


Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method. Inside Red::fill() method. Inside Green::fill() method. Inside Blue::fill() method. ```

❮ Builder Pattern Chain Of Responsibility Pattern ❯