Introduction to Factory Pattern

Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern is a creative model that provides the best way to create objects.

In factory mode, we don't expose the creation logic to the client when creating the object, and we point to the newly created object by using a common interface.


Intent: Define an interface to create an object, let its subclasses decide which factory class to instantiate, and the factory mode delays the creation process to subclasses.

Main solution: Mainly solve the problem of interface selection.

When to use: We explicitly plan to create different instances under different conditions.

How to solve: Let its subclass implement the factory interface, and return an abstract product.

Key Code: The creation process is performed in its subclasses.

Application example: 1. You need a car, you can pick up the goods directly from the factory, instead of how to make the car, and the specific implementation of the car. 2, Hibernate to change the database just need to change dialects and drivers.

Advantages: 1. A caller wants to create an object, as long as he knows its name. 2, high scalability, if you want to add a product, just extend a factory class. 3. The specific implementation of the shielded product, the caller only cares about the interface of the product.

Disadvantages: Each time you add a product, you need to add a concrete class and object implementation factory, which increases the number of classes in the system, which increases the complexity of the system to some extent. Degree, while also increasing the dependency of the system specific class. This is not a good thing.

Usage scenarios: 1. Logger: The record may be recorded to the local hard disk, system events, remote server, etc., and the user can choose where to log the log. 2, database access, when the user does not know which type of database the system uses, and the database may change. 3, design a framework to connect to the server, you need three protocols, "POP3", "IMAP", "HTTP", you can use these three as a product class to achieve an interface.

Note: As a way to create a class, you can use the factory method mode wherever you need to generate complex objects. One thing to note is that complex objects are suitable for factory mode, while simple objects, especially those that only need to be created by new, do not need to use factory mode. If you use the factory pattern, you need to introduce a factory class, which will increase the complexity of the system.

factory patternuml diagram


We will create an Shape interface and an entity class that implements the Shape interface. The next step is to define the factory class ShapeFactory.

FactoryPatternDemo, our demo class uses ShapeFactory to get the Shape object. It will pass information to ShapeFactory (CIRCLE / RECTANGLE / SQUARE) to get the type of object it needs.

Step 1

Create an interface:

public interface Shape { void draw(); }

Step 2

Create an entity class that implements the interface.

public class Rectangle implements Shape { @Override public void draw() { System.out< Span class="hl-code">.println("Inside Rectangle::draw() method."); } }

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

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

Step 3

Create a factory that generates objects based on the entity class for the given information.

public class ShapeFactory { //Get the shape type object using the getShape method public Shape getShape(String shapeType){ if(shapeType< Span class="hl-code"> == null){ return null; } if(shapeType< Span class="hl-code">.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 the object of the entity class by passing the type information.

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< /span>"); //Call Circle's draw method< /span> shape1.draw< Span class="hl-brackets">(); //Get the object of the Rectangle and call its draw method Shape shape2 = shapeFactory.getShape("RECTANGLE< /span>"); //Invoke Rectangle's draw method< /span> shape2.draw< Span class="hl-brackets">(); //Get the object of Square and call its draw method Shape shape3 = shapeFactory.getShape("SQUARE< /span>"); //Call Square's draw method< /span> shape3.draw< Span class="hl-brackets">(); } }

Step 5

Executing the program, outputting results:

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition