Factory pattern
The Factory Pattern is one of the most commonly used design patterns in Java; it provides a way to create objects that separates the process of creating objects from the process of using them.
The Factory Pattern provides a way to create objects without specifying the concrete class to be created.
By using the Factory Pattern, the creation logic of objects can be encapsulated in a factory class instead of instantiating objects directly in the client code, which improves the maintainability and extensibility of the code.
Types of Factory Pattern
1. Simple Factory Pattern:
- The Simple Factory Pattern is not a formal design pattern, but it is the foundation of the Factory Pattern. It uses a single factory class to create different objects, deciding which type of object to create based on the parameters passed in.
2. Factory Method Pattern:
- The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. The Factory Method defers the creation of objects to subclasses.
3. Abstract Factory Pattern:
- The Abstract Factory Pattern provides an interface for creating a family of related or interdependent objects without specifying their concrete classes.
Summary
Intent
Define an interface for creating an object and let its subclasses decide which concrete class to instantiate. The Factory Pattern defers the creation process of objects to subclasses.
Main problem solved
The problem of interface selection.
When to use it
When we need to create different instances under different conditions.
How to solve
By having subclasses implement the factory interface, an abstract product is returned.
Key code
The creation process of the object is implemented in the subclass.
Application Example
- Car manufacturing: if you need a car, you simply pick it up from the factory without caring about the manufacturing process and its internal implementation.
- Hibernate: when changing the database, only the Dialect and the database Driver need to be changed to switch between different databases.
Advantages
- The caller only needs to know the name of the object to create it.
- High extensibility; if a new product needs to be added, only one more factory class needs to be extended.
- The concrete implementation of the product is hidden; the caller only cares about the interface of the product.
Disadvantages
Each time a product is added, a concrete class and a corresponding factory must be added, which multiplies the number of classes in the system and increases system complexity and the dependency on concrete classes.
Use Cases
- Logging: logs may be written to the local hard disk, system events, remote servers, and so on; the user can choose where to record the logs.
- Database access: when the user does not know which database the final system will use, or the database may change.
- Framework design for connecting to servers: it needs to support the three protocols "POP3", "IMAP", and "HTTP"; these three protocols can be treated as product classes that jointly implement an interface.
Notes
The Factory Pattern is suitable for scenarios that generate complex objects. If an object is relatively simple and can be created with new, then there is no need to use the Factory Pattern. Using the Factory Pattern introduces a factory class, which increases system complexity.
structures
The Factory Pattern contains the following main roles:
- Abstract Product: defines the common interface or abstract class of the products. It can be the parent class or interface of concrete product classes and specifies the common methods of product objects.
- Concrete Product: implements the abstract product interface, defining the specific behavior and attributes of the concrete product.
- Abstract Factory: declares the abstract methods for creating products; it can be an interface or an abstract class. It can have multiple methods for creating different types of products.
- Concrete Factory: implements the Abstract Factory interface and is responsible for actually creating the concrete product objects.
Create a class that implements
We will create a Shape Interface and Implementation Shape entity classes of the interface. The next step is to define the factory class ShapeFactory。
FactoryPatternDemo Class uses ShapeFactory to get Shape objects. It will pass ShapeFactory Passing information (CIRCLE / RECTANGLE / SQUARE), in order to get the type of object it needs.

Step 1
Create an interface:
Shape.java
public
interface
Shape
{
void
draw
(
)
;
}
Step 2
Create concrete classes that implement the interface.
Rectangle.java
public
class
Rectangle
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Inside Rectangle::draw() method.
"
)
;
}
}
Square.java
public
class
Square
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Inside Square::draw() method.
"
)
;
}
}
Circle.java
public
class
Circle
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Inside Circle::draw() method.
"
)
;
}
}
Step 3
Create a factory that generates objects of entity classes based on the given information.
ShapeFactory.java
public
class
ShapeFactory
{
//
Use the getShape method to get an object of a shape type
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 this factory to get objects of entity classes by passing type information.
FactoryPatternDemo.java
public
class
FactoryPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
ShapeFactory
shapeFactory
=
new
ShapeFactory
(
)
;
//
Get the Circle object and call its draw method
Shape
shape1
=
shapeFactory
.
getShape
(
"
CIRCLE
"
)
;
//
Call Circle's draw method
shape1
.
draw
(
)
;
//
Get the Rectangle object and call its draw method
Shape
shape2
=
shapeFactory
.
getShape
(
"
RECTANGLE
"
)
;
//
Call Rectangle's draw method
shape2
.
draw
(
)
;
//
Get the Square object and call its draw method
Shape
shape3
=
shapeFactory
.
getShape
(
"
SQUARE
"
)
;
//
Call Square's draw method
shape3
.
draw
(
)
;
}
}
Step 5
Run the program, output result:
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.