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.
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.
Create an interface:
Create an entity class that implements the interface.
Create a factory that generates objects based on the entity class for the given information.
Use this factory to get the object of the entity class by passing the type information.
Executing the program, outputting results:
Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method.