Sunday, January 21, 2018

What is Factory Design Patten and its Code in java

What is Factory Desigen Pattern.also write the code?
A Factory Pattern  define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. 
In other words, subclasses are responsible to create the instance of the class.

 

Advantage of Factory Design Pattern

1-Factory Method Pattern allows the sub-classes to choose the type of objects to create.
2-It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.

 

Usage of Factory Design Pattern

1-When a class doesn't know what sub-classes will be required to create
2-When a class wants that its sub-classes specify the objects to be created.
3-When the parent classes choose the creation of objects to its sub-classes.



Ex:
Step 1->
Create an interface.
Shape.java

public interface Shape
{
   void draw();
}

Step 2->
Create concrete classes implementing the same interface.
1-Rectangle.java
public class Rectangle implements Shape
{
   @Override
   public void draw()
   {
     System.out.println("Inside Rectangle::draw() method.");
   }
}

2-Square.java
public class Square implements Shape
{
   @Override
   public void draw()
   {
      System.out.println("Inside Square::draw() method.");
   }
}

3-Circle.java
public class Circle implements Shape
{
   @Override
   public void draw()
 {
      System.out.println("Inside Circle::draw() method.");
   }
}

Step 3->
Create a Factory to generate object of concrete class based on given information.
ShapeFactory.java
public class ShapeFactory
{
           
   //use getShape method to get object of type shape
   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;
   }
}

Step 4->
Use the Factory to get object of concrete class by passing an information such as type.
FactoryPatternDemo.java
public class FactoryPatternDemo
{
  public static void main(String[] args)
 {
      ShapeFactory shapeFactory = new ShapeFactory();

      Shape shape1 = shapeFactory.getShape("CIRCLE");
              shape1.draw();

     Shape shape2 = shapeFactory.getShape("RECTANGLE");
                shape2.draw();

      Shape shape3 = shapeFactory.getShape("SQUARE");
             shape3.draw();
   } 
}

If you have any issue in understanding the above article , reach out to us leave us a comment.

Thanks for reading
Noeik

1 comment:

  1. Thank you your information Mr.Noeik and your explanation is clearly understandable

    ReplyDelete