This article describes the factory model implementation method of Java design pattern. Share it for your reference, as follows:
The factory model mainly provides a transitional interface for creating objects, so as to shield and isolate the specific process of creating objects, so as to achieve the purpose of improving flexibility.
The factory model is divided into three categories:
1) Simple Factory model: not conducive to the generation of series of products;
2) Factory Method: also known as polymorphic factory;
3) Abstract Factory model: also known as toolbox, it produces product families, but is not conducive to the generation of new products;
1. Simple factory model
Simple factory mode is also known as static factory method mode. By renaming, you can see that this pattern must be very simple. Its purpose is simple: define an interface for creating objects.
In the simple factory model, a factory class is at the center of instantiation calls to a product class, which determines which product class should be instantiated, just as a traffic policeman stands in the flow of vehicles and decides to release the vehicle in which direction to flow. Let's take a look at its composition:
1) Factory role: This is the core of this model, which contains certain business logic and judgment logic. In java it is often implemented by a concrete class.
2) Abstract product role: It is generally a parent class inherited by a specific product or an interface implemented. Implemented by interfaces or abstract classes in Java.
3) Specific product role: The object created by the factory class is an instance of this role. Implemented by a concrete class in java.
2. Factory method model
The factory method model is the further abstraction and promotion of the simple factory model. In the factory method model, it is no longer determined by only one factory class that product class should be instantiated. This decision is handed over to the subclass of the abstract factory to make. Let's take a look at its composition:
1) Abstract factory role: This is the core of the factory method pattern, it has nothing to do with the application. It is an interface that a specific factory role must implement or a parent class that must be inherited. In java it is implemented by abstract classes or interfaces.
2) Specific factory role: It contains code related to specific business logic. Called by the application to create the corresponding specific product object.
3) Abstract product role: It is the parent class inherited by a specific product or an interface implemented. In Java, there are generally abstract classes or interfaces to implement them.
4) Specific product role: The object created by a specific factory role is an instance of this role. Implemented by concrete classes in Java.
The factory method pattern uses multiple subclasses inherited from the abstract factory role to replace the "God class" in the simple factory pattern. As mentioned above, this will share the pressure on the object; and this will make the structure flexible - when a new product (i.e., the car of the nouveau riche) is generated, as long as it is generated according to the contract provided by the abstract product role and the abstract factory role, it can be used by the customer without having to modify any existing code. It can be seen that the structure of the factory role also conforms to the principle of opening and closing!
The code is as follows:
//Abstract product role public interface Moveable { void run();}//Specific product role public class Plane implements Moveable { @Override public void run() { System.out.println("plane...."); }}public class Broom implements Moveable { @Override public void run() { System.out.println("broom......"); }}//Abstract factory public abstract class VehicleFactory { abstract Moveable create();}//Special factory public class PlaneFactory extends VehicleFactory{ public Moveable create() { return new Plane(); }}public class BroomFactory extends VehicleFactory{ public Moveable create() { return new Broom(); }}//Test class public class Test { public static void main(String[] args) { VehicleFactory factory = new BroomFactory(); Moveable m = factory.create(); m.run(); }}3. Abstract factory pattern
The code is as follows
//Abstract factory class public abstract class AbstractFactory { public abstract Vehicle createVehicle(); public abstract Weapon createWeapon(); public abstract Food createFood();}//Concrete factory class, where Food, Vehicle, Weapon are abstract classes, public class DefaultFactory extends AbstractFactory{ @Override public Food createFood() { return new Apple(); } @Override public Vehicle createVehicle() { return new Car(); } @Override public Weapon createWeapon() { return new AK47(); }}//Test class public class Test { public static void main(String[] args) { AbstractFactory f = new DefaultFactory(); Vehicle v = f.createVehicle(); v.run(); Weapon w = f.createWeapon(); w.shoot(); Food a = f.createFood(); a.printName(); }}In the abstract factory pattern, the abstract product may be one or more, thus forming one or more product families. In the case of only one product family, the abstract factory pattern actually degenerates to the factory method pattern.
Summarize:
(1) The simple factory pattern is to create instances of other classes by a specific class. The parent class is the same and the parent class is specific.
(2) The factory method pattern is that there is an abstract parent class defining public interface, and the subclass is responsible for generating concrete objects. The purpose of this is to delay the instantiation of the class to the subclass.
(3) The abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their specific classes. It targets hierarchical structures with multiple products. The factory method model is aimed at the hierarchy structure of a product.
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.