Abstract Factory Pattern – Java Code


An abstract class that declares an interface for a general ProductA.

abstract class ProductA
{
   public void method1();
   public void method2();
}

This below three classes provide different implementations of abstract
class ProductA. The object of each of these specific implementations will be
constructed in a factory class.

class ProductA1 extends ProductA
{
   String name;

   public ProductA1(String name)
   {
      this.name = name;
   }

   public void method1()
   {
      //give the definition
   }

   public void method2()
   {
      // give the definition
   }
}
class ProductA2 extends ProductA
{
   String name;

   public ProductA2(String name)
   {
      this.name = name;
   }

   public void method1()
   {
      //give the definition
   }

   public void method2()
   {
      // give the definition
   }
}
class ProductA3 extends ProductA
{
    String name;

    public ProductA3(String name)
    {
       this.name = name;
    }

    public void method1()
    {
       //give the definition
    }

    public void method2()
    {
       // give the definition
    }
}

An abstract class that declares an interface for a general ProductB

abstract class ProductB
{
   public void method1();
   public void method2();
}

This below three classes provide different implementations of abstract
class ProductB. The object of each of these specific implementations will be
constructed in a factory class.

class ProductB1 extends ProductB
{
   String name;

   public ProductB1(String name)
   {
     this.name = name;
   }

   public void method1()
   {
     //give the definition
   }

   public void method2()
   {
     // give the definition
   }
}
class ProductB2 extends ProductB
{
   String name;

   public ProductB2(String name)
   {
      this.name = name;
   }

   public void method1()
   {
      //give the definition
   }

   public void method2()
   {
      // give the definition
   }
}
class ProductB3 extends ProductB
{
    String name;

    public ProductB3(String name)
    {
      this.name = name;
    }

    public void method1()
    {
      //give the definition
    }

    public void method2()
    {
      // give the definition
    }
}

An abstract factory class that declares an interface to create basic types of
ProductA and ProductB.

abstract class AbstractFactory
{
    abstract ProductA createProductA();
    abstract ProductB createProductB();
}

The concrete factory class that implements the interface declared by Abstract-Factory.
This class creates two product objects (A1 and B1) belonging to the family ConcreteFactory1.
The class constructs concrete product objects (ProductA1 and ProductB1) but returns
an abstract reference to these objects.

The client will use only the abstract product objects(ProductA and ProductB) without
knowing the actual implementation they are referring to.

class ConcreteFactory1 extends AbstractFactory
{
    public ProductA createProductA()
    {
       return (new ProductA1("ProductA1"));
    }

    public class ProductB createProductB()
    {
       return (new ProductB1("ProductB1"));
    }
}

Similar to the above concrete class

class ConcreteFactory2 extends AbstractFactory
{
    public ProductA createProductA()
    {
       return (new ProductA2("ProductA2"));
    }

    public class ProductB createProductB()
    {
       return (new ProductB2("ProductB2"));
    }
}

Similar to the above two concrete classes.

class ConcreteFactory3 extends AbstractFactory
{
    public ProductA createProductA()
    {
       return (new ProductA3("ProductA3"));
    }

    public class ProductB createProductB()
    {
       return (new ProductB3("ProductB3"));
    }
}

A main class to execute the program

public class clientApplication
{
    public static void main(String args[])
    {
       String t;
       BufferedReader br = new BufferedReader
                         (new InputStreamReader(System.in));
       t = br.readLine();
       AbstractFactory instantiateFactory = null;
       if(t.equals("1"))
       {
         instantiateFactory = new ConcreteFactory1();
       }
       else if(t.equals("2"))
       {
         instantiateFactory = new ConcreteFactory2();
       }
       else
       {
         instantiateFactory = new ConcreteFactory3();
       }

       ProductA firstProduct  = 
               instantiateFactory.createProductA();
       ProductB secondProduct  = 
               instantiateFactory.createProductB();
    }
}

In the above main function, we create a reference variable of the abstract factory
class. This variable will point to the object of any of the three concrete factory classes.
So the client deals only with the abstract factory object and not the concrete factory class
objects. This is the real essence of Abstract Factory pattern
<

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: