Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Factory pattern

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:

2. Factory Method Pattern:

3. Abstract Factory Pattern:


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

  1. 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.
  2. Hibernate: when changing the database, only the Dialect and the database Driver need to be changed to switch between different databases.

Advantages

  1. The caller only needs to know the name of the object to create it.
  2. High extensibility; if a new product needs to be added, only one more factory class needs to be extended.
  3. 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

  1. 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.
  2. Database access: when the user does not know which database the final system will use, or the database may change.
  3. 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:

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.

UML diagram of the Factory pattern

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.

Other related articles

  • Design Patterns - Factory Pattern