This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging
The Factory Pattern is one of the most common design patterns in Java. This type of design pattern is a creation pattern that provides an optimal way to create objects. In the factory pattern, objects are created without exposing the creation logic to the client, and by using a common interface to point to the newly created object.
introduce
The direction of | parsing |
---|---|
intentions | Define an interface for creating an object, and let its subclasses decide which factory class to instantiate. The factory pattern delays its creation to the subclasses |
Mainly to solve | It mainly solves the problem of interface selection |
When to use | We explicitly plan to create different instances under different conditions |
How to solve | Let its subclasses implement the factory interface and return an abstract product |
The key code | The creation process is performed in its subclass |
Examples of application | 1. You need a car that can be picked up directly from the factory, regardless of how the car was built or the implementation inside the car. 2, Hibernate database only need to change the dialect and driver. |
advantages | 1. A caller wants to create an object, all he needs to know is its name. 2, scalability, if you want to add a product, as long as the extension of a factory class can be. 3. The specific implementation of the product is masked. The caller only cares about the interface of the product. |
disadvantages | Each time a product is added, a concrete class and object implementation factory need to be added, which makes the number of classes in the system multiply, increases the complexity of the system to a certain extent, but also increases the dependency of the concrete class of the system. That’s not a good thing, okay |
Usage scenarios | 1. Logger: Records may be recorded to local disks, system events, remote servers, etc. Users can choose where to record logs. 2, database access, when the user does not know the final system which kind of database, and the database may change. 3, the design of a connection to the server framework, need three protocols, “POP3”, “IMAP”, “HTTP”, can be these three as a product class, the common implementation of an interface. |
Matters needing attention | As a class creation pattern, you can use the factory method pattern anywhere you need to generate complex objects. It is important to note that complex objects are suitable for using the factory pattern, while simple objects, especially, only need to passnew You can complete the object creation without using the factory pattern. If you use the factory pattern, you need to introduce a factory class, which increases the complexity of the system |
implementation
- To create a
Shape
Interface and ImplementationShape
The entity class of the interface - Defining a factory class
ShapeFactory
. FactoryPatternDemo
Class USESShapeFactory
In order to getShape
Object. It will be toShapeFactory
Pass information (CIRCLE/RECTANGLE/SQUARE) to get the type of object it needs.
Step 1
Create an interface:
// Shape.java
public interface Shape {
void draw();
}
Copy the code
Step 2
Create an entity class that implements the interface.
// Rectangle.java public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}Copy the code
// Square.java public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); }}Copy the code
// Circle.java public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}Copy the code
Step 3
Create a factory that generates objects based on the entity class of the given information.
// Shapefactory.java public class ShapeFactory {// 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; }}Copy the code
Step 4
Using this factory, you get objects of the entity class 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 "); // Get the Rectangle object and call its draw method Shape Shape2 = shapeFactory.getShape(" Rectangle "); // Call the Rectangle 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(); }}Copy the code
Step 5
Execute the program and print the result:
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Copy the code