Sunday 25 September 2022

Design Pattern: Abstract Factory 

a)Abstract Factory Design Pattern:

In factory design pattern, we saw that for particular product like bottle, we have methods defined in it and then we implement them. In abstract factory, we go one more step ahead and it combine multiple products which final user may need. 

E.g. A shop will need bottle, chair, table, refrigerator, cupboard/ shelves to stack products, payment system and so on.

So what abstract factory will do is composed of multiple steps:

1) Create Abstract products in form of interfaces:

a) interface bottle(){

  void cap();

int shape();

String color();

}

b) interface chair(){

  void designType();

int specifications();

String color();

}

c) interface table(){

  void designType();

int material();

String color();

}

d) interface shelf(){

String design();

int material();

String specifications();

}

2) Create concrete classes which will implement the above interfaces for each product:

a) public class OilBottle implements Bottle {

  String capShape = "Circular";

 String capMaterial = "aluminium";

  @Override

   public void cap() {

   System.out.println("The cap is : " + capShape +  "and " + capMaterial + "with company logo on it");

}

 String bottleShape = "Cylindrical";

 int height = 12;

 int diameter = 10;

int volumeBottle;

  

  public int shape(){

   System.out.println("The bottleShape is : " + bottleShape);

  int areaBottle = 3.14 * height * (diameter/2)*(diameter/2);

  return areaBottle;

}


 String bottleColor = "red";  

public String color(){

  return bottleColor;

}

}


In same way, we will implement classes like juiceBottle, plasticBottle, sauceChair, chemicalChair, etc. 

b) public class WoodenChair implements Chair {

void designType();

int specifications();

String color();

  String chairType = "Victorian era";

 String chairSize = "rigid & heavy";

  @Override

   public void designType() {

   System.out.println("The chair is : " + chairType +  "and " + chairSize + "with company logo on it");

}

 String chairShape = "Four sturdy twisted legs & two hand-supports with flat - thick walled back with neck support");

 int legVolume = 88;

 int baseInSqIN = 336;

String dimensions= "in";

  

  public int specification(){

   System.out.println("The chairShape is : " + chairShape);

  int specChair = legVolume*4 + baseInSqIN;

  return specChair;

}


 String chairColor = "red";  

public String color(){

  return chairColor;

}

}

In same way, we will implement classes like metalicChair, plasticChair, fibreglassChair, epoxyMetalChair, etc. 

In same way, we will implement classes for table like metalTable, plasticTable, epoxyTable, chemicalResinTable, etc. & for shelf like metalShelf, plasticShelf, epoxyShelf, chemicalResinShelf, etc

3) Create abstract classes which will have methods defined to create above different products:

interface shopSetUp(){

  chair createChair(String material);

bottle createBottle(String material);

table createTable(String material);

shelf createShelf(String material);

}

4) Create concrete class which will implement above abstract class and its methods as per requirements

public class JuiceShop implements shopSetUp(){

          Chair chair;

         Shelf shelf;

         Table table;

         Bottle bottle;

//All the above classes will be used in methods below

           public  chair createChair(String material){

          if(material.equalsIgnoreCase("WOODEN")){

             chair = new WoodenChair();

         else if(material.equalsIgnoreCase("METAL")){

            chair = new MetalChair();

         else if(material.equalsIgnoreCase("PLASTIC")){

           chair = new PlasticChair();

         else if(material.equalsIgnoreCase("FIBREGLASS"){

         chair = new FibreChair();

        else 

           chair = null;

       return chair;

}


             public  table createTable(String material){

          if(material.equalsIgnoreCase("WOODEN")){

             table = new WoodenTable();

         else if(material.equalsIgnoreCase("METAL")){

           table = new MetalTable();

         else if(material.equalsIgnoreCase("PLASTIC")){

           table = new PlasticTable();

         else if(material.equalsIgnoreCase("FIBREGLASS"){

         table = new FibreTable();

        else 

           table = null;

       return table;

}

   public  shelf createShelf(String material){

          if(material.equalsIgnoreCase("WOODEN")){

             shelf = new WoodenShelf();

         else if(material.equalsIgnoreCase("METAL")){

           shelf = new MetalShelf();

         else if(material.equalsIgnoreCase("PLASTIC")){

           shelf = new PlasticShelf();

         else if(material.equalsIgnoreCase("FIBREGLASS"){

         shelf = new FibreShelf();

        else 

           shelf = null;

       return shelf;

}

 public  bottle createBottle(String material){

          if(material.equalsIgnoreCase("BAMBOO")){

             bottle = new BambooBottle();

         else if(material.equalsIgnoreCase("METAL")){

           bottle = new MetalBottle();

         else if(material.equalsIgnoreCase("PLASTIC")){

           bottle = new PlasticBottle();

         else if(material.equalsIgnoreCase("FIBREGLASS"){

         bottle = new FibreBottle();

        else 

           bottle = null;

       return bottle;

}


}


No comments:

Post a Comment