Abstract Factory Pattern in Java

In this article, we will discuss about Abstract factory design pattern and provide an implementation in Java.
 

Abstract Factory Pattern in Java

 
An abstract factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

 
Abstract Factory pattern in java
 
In Abstract Factory pattern, the client uses an abstract interface to create products without explicitly specifying their classes. The abstract interface uses concrete factories that can create the objects as per the Factory pattern.

This way, the client is decoupled of any specifics of the concrete products.
 

For example, Apple creates iPhone and iPad products. Similarly, Samsung creates Samsung Phone and Tablet.

Lets see how we can generate these products using Abstract factory pattern in java.
 

Create abstract parent classes

 

Lets create abstract parent classes Phone and Tablet.

 

package com.topjavatutorial.patterns.abstractfactory;

public abstract class Phone{
  String name;
  
  void prepare(){
    System.out.println("\nCreating "+ name);
  }
}

package com.topjavatutorial.patterns.abstractfactory;

public abstract class Tablet {
  String name;
  
  void prepare(){
    System.out.println("\nCreating " + name);
  }
}


 

Create concrete child classes

 
Lets create concrete classes implementing the abstract classes created above.

We will create IPad,IPhone,SamsungPhone and SamsungTablet classes. Notice that these phones/tablets belong to two different companies.
 

IPad class

 

package com.topjavatutorial.patterns.abstractfactory;

public class IPad extends Tablet{

  public IPad(){
    name = "iPad";
  }
}


 
The IPad class does not add any extra features.

In IPhone class, lets override the prepare() method and add support for Apple pay features.
 

IPhone class

 

package com.topjavatutorial.patterns.abstractfactory;

public class IPhone extends Phone {
  
  public IPhone(){
    name = "iPhone";
  }

  public void prepare(){
    System.out.println("Creating "+ name);
    System.out.println("Adding Apple Pay features");
  }
}


 

Similarly, lets create SamsungTablet and SamsungPhone classes.
 

SamsungTablet class

 

package com.topjavatutorial.patterns.abstractfactory;

public class SamsungTablet extends Tablet{

  public SamsungTablet(){
    name = "Samsung Tablet";
  }
}


 
In SamsungPhone class, lets override the prepare() method and add support for Samsung pay features.
 

SamsungPhone class

 

package com.topjavatutorial.patterns.abstractfactory;

public class SamsungPhone extends Phone {
  
  public SamsungPhone(){
    name = "Samsung Phone";
  }

  public void prepare(){
    System.out.println("Creating "+ name);
    System.out.println("Adding Samsung Pay features");
  }
}


 

Creating an Abstract factory interface

 
The AbstractFactory defines an interface that all Concrete factories must implement. This consists of methods for creating the products.
 

package com.topjavatutorial.patterns.abstractfactory;

public interface AbstractFactory {
  
  public Phone createPhone(String type);

  public Tablet createTablet(String type);
}


 

Creating Concrete Factory classes for Apple and Samsung products

 

We have the concrete implementation classes for Phone and Tablet ready. Lets provide a way to encapsulate creation of the concrete classes.

The concrete factories implement the AbstractFactory created above.
 

AppleProductFactory class

 

package com.topjavatutorial.patterns.abstractfactory;

import com.topjavatutorial.patterns.abstractfactory.AbstractFactory;

public class AppleProductFactory implements AbstractFactory{

  @Override
  public Phone createPhone(String type) {
    return new IPhone();
  }

  @Override
  public Tablet createTablet(String type) {
    return new IPad();
  }
}

 

SamsungProductFactory class

 

package com.topjavatutorial.patterns.abstractfactory;

public class SamsungProductFactory implements AbstractFactory{

  @Override
  public Phone createPhone(String type) {
    return new SamsungPhone();
  }

  @Override
  public Tablet createTablet(String type) {
    return new SamsungTablet();
  }
}


 

Testing our Abstract Factory pattern implementation

 

package com.topjavatutorial.patterns.abstractfactory;

import java.util.Scanner;

public class AbstractFactoryDemo {

  public static void main(String[] args) {
    // Create AbstractFactory
    AbstractFactory factory;
    Phone phone;
    Tablet tablet;
    String company = "";
    System.out.println("Enter company name");
    try (Scanner sc = new Scanner(System.in);) {
      company = sc.next();
    }

    if (company.equalsIgnoreCase("apple")) {
      factory = new AppleProductFactory();
      phone = factory.createPhone("phone");
      phone.prepare();
      tablet = factory.createTablet("tablet");
      tablet.prepare();
    } else if (company.equalsIgnoreCase("samsung")) {

      factory = new SamsungProductFactory();
      phone = factory.createPhone("phone");
      phone.prepare();
      tablet = factory.createTablet("tablet");
      tablet.prepare();
    } else {
      System.out.println("Not supported");
    }
  }

}

 

Output

 
Enter company name
apple

Creating iPhone
Adding Apple Pay features

Creating iPad

 
Enter company name
samsung

Creating Samsung Phone
Adding Samsung Pay features

Creating Samsung Tablet
 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags