Factory Pattern
Factory Pattern
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 1
JAVA Means DURGA SOFT
Factory Pattern
The factory pattern design pattern handles these problems by defining a separate
method for creating the objects, these methods optionally accept parameters defining how the
object is created, and then return the created object.
Problem: Creating multiple objects for multiple subclasses and utilizing one of them based on
the supplied data is wrong methodology because the remaining objects became unnecessarily
created objects.
Solution: Use Factory Pattern, here the method of super class or some other class
instantiates/creates one of the several sub classes object based on the supplied data provide by
user (at compile time/at runtime) that means objects for remaining sub classes will not be
created.
Def: Define an interface for creating an object, but let the subclasses decide which class to
instantiate. The Factory method lets a class defer instantiation to subclasses.
A Simple Factory pattern returns an instance of one of several possible (sub) classes
depending on the data provided to it. Usually all classes that it returns have a common parent
class and common methods, but each performs a task differently and is optimized for different
kinds of data.
Intent:
Application Areas:
A class can't anticipate the class of objects it must create.
A class specifies its sub-classes to specify which objects to create.
In programmer’s language, you can use factory pattern where you have to create an
object of any one of sub-classes depending on the data provided.
Ex:
In JDBC applications, DriverManager.getConnection() method logic is nothing but
factory pattern logic because based on the argument values that are supplied, it creates and
returns one of the JDBC driver class that implements the java.sql.Connection interface.
I. Connection con=Drivermanager.getConnection(“jdbc:odbc:durga”,”uname”,”pwd”);
Here getConnection(_,_,_) call returns the object of Type-1 JDBC driver
class i.e. sun.jdbc.odbc.JdbcOdbcConnection that implements
java.sql.Connection interface.
II. Connection con= Drivermanager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”
,”uname”,”pwd”);
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 3
JAVA Means DURGA SOFT
}
};
public class FactoryPatternTest
{
public static void main(String[] args)
{
FactoryPatternTest fp=new FactoryPatternTest();
System.out.println();
ShapeEx s=fp.getShape(args[0]); // data is supplied at runtime
System.out.println();
System.out.println("Object is created for: "+s.getClass());
}
//Factory Pattern Logic
public ShapeEx getShape(String shape)
{
if(shape.equals("L"))
return new Line();
else if(shape.equals("S"))
return new Square();
else
return null;
}
}
Output:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 5
JAVA Means DURGA SOFT
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 6
JAVA Means DURGA SOFT
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 7