Java 23 design patterns of the factory pattern

Definition of:

The factory pattern: Define a factory interface to create a product object, delaying the actual creation of a product product object to a concrete subfactory class. This satisfies the “separation of creation and use” feature required in the creation pattern

Divided by actual business:Simple Factory Pattern.Factory method modelandAbstract factory pattern

We call the objects we create “products” and the objects we create products “factories”. If there are not many products to create, only one factory class can complete this pattern. This pattern is called “simple factory pattern”.

The Simple Factory Pattern is also known as the static Factory Method Pattern because the methods used to create instances in a Simple Factory Pattern are usually static methods.

Simple Factory PatternEach additional product needs to add a specific product class and a corresponding specific factory class, which increases the complexity of the system and violates the “Open Closed Principle”.

Factory method modelIs a further abstraction of the simple factory pattern, which has the advantage of enabling the system to introduce new products without modifying the original code, that is, to satisfy the Open Closed Principle.

II. Simple Factory Pattern

The model structure

The main roles of the Simple Factory pattern are as follows:

SimpleFactory: is the core of the SimpleFactory pattern and is responsible for implementing the internal logic to create all instances. The factory class’s method for creating a product class can be called directly from the outside world to create the desired product object

Abstract Product: is the parent class of all objects created by the simple factory and is responsible for describing the common interface shared by all instances

ConcreteProduct: Is the creation target of the Simple Factory pattern



Code implementation:

Public interface Car {// void productCar(); } // Specific products: Public void setName () {setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); }} // public class Baomacar implements Car{@Override public void implements Car() {/ Override public void implements Car() { Println (" I'll make a BMW "); }} // public class SimpleCarFactory {public Car getCar(int a){switch (a){case 0: return new BenchCar (); case 1: return new BaoMaCar(); default: return null; }}} // use try {Car = new SimpleCarFactory().getCar(0); // use try {Car = new SimpleCarFactory().getCar(0); // Get the product car.productCar(); } catch (Exception e) {e.printStackTrace(); } // result System.out: I'll build the Mercedes

Two: factory method model

Factory Method, also known as Polymorphic Factory Pattern. In the factory method pattern, the core factory class is no longer responsible for the creation of all products, but subclasses are responsible for the creation. The core class becomes an abstract factory role, responsible only for specifying the interfaces that a specific factory subclass must implement, without getting into the details of which product class should be instantiated.

Structure of the pattern

The main roles of the Factory Method pattern are as follows.

1. Abstract Factory: Provides an interface to create products through which callers access the Factory method newProduct() of a specific Factory to create products.

2. ConcreteFactory: It mainly realizes the abstract method in the abstract factory and completes the creation of specific products.

3. Abstract Product: It defines the specification of the Product and describes the main features and functions of the Product.

4. ConcreteProduct: It implements the interface defined by the abstract product role, and is created by the concrete factory, which corresponds to the concrete factory.



Code implementation

Public interface Car {// void productCar(); } // Specific products: Public void setName () {setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); }} // public class Baomacar implements Car{@Override public void implements Car() {/ Override public void implements Car() { Println (" I'll make a BMW "); } // public Car createCar(); // public Car createCar(); } // Factory specific implementation: Public Class BaomaFactory implements AbstractFactory {@Override public Car CreateCar () {Override public Car CreateCar () {Override public Car CreateCar (); Println (" I am here at the BMW factory to create BMW products "); return new BaoMaCar(); }} // Specific factory: Public class BenchiFactory implements AbstractFactory {@Override public Car CreateCar () {Override public Car CreateCar () { Println (" I created Mercedes-Benz products at the Mercedes-Benz factory "); return new BenChiCar(); }} // call Car Bencar = new BenChiFactory().createCar(); benCar.productCar(); Car baoCar= new BaoMaFactory().createCar(); baoCar.productCar(); // The result is that the System. Out of the System is the product of the BMW factory, and I'm going to create a BMW car

Definition of the AbstractFactory pattern: A pattern structure that provides an interface for a visiting class to create a set of related or interdependent objects without specifying the specific class of the desired product to obtain different levels of the same family of products. The abstract factory pattern is an updated version of the factory method pattern, which produces only one level of products, while the abstract factory pattern can produce multiple levels of products. The following conditions are generally required to use the abstract factory pattern. There are multiple product families in the system, each specific factory creates the same family but belongs to different hierarchical structure of products. The system can only consume one family of products at a time, that is, the products of the same family can be used together. Structure of the pattern Abstract The main roles of the factory pattern are as follows. 1. Abstract Factory: provides an interface to create products. It contains multiple methods to create products, newProduct(), which can create multiple products of different levels. 2. Concrete Factory: It mainly realizes multiple abstract methods in the abstract Factory and completes the creation of specific products. 3. Abstract Product (Product) : It defines the specification of the Product and describes the main features and functions of the Product. Abstract Factory pattern has multiple abstract products. 4. ConcreteProduct: Implements the interface defined by the abstract product role, which is created by the concrete factory. It has a many-to-one relationship with the concrete factory.

Code implementation

// public interface TV {// void productTV (); } public class implements TV {@Override public void implements () {System.out.println();} public class implements TV {@Override public void implements () {System.out.println(); }} // Specific products: Public class Baomatv implements TV {@Override public void productTV () {System.out.println(); public class Baomatv implements TV {@Override public void productTV () {System.out.println(); }} public interface Car {// void productCar(); // void productCar(); } // Specific products: Public void setName () {setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); setName (); }} // public class Baomacar implements Car{@Override public void implements Car() {/ Override public void implements Car() { Println (" I'll make a BMW "); } // public Car createCar() {public Car createCar() {// public Car createCar(); // public TV createTV (); } // Specific factory: Public class BenchiFactory implements AbstractFactory {@Override public Car CreateCar () {return new BenchiFactory (); public class BenchiFactory implements AbstractFactory {@Override public Car CreateCar () {return new BenchiFactory (); } @Override public Tv createTv() { return new BenChiTv(); }} // Factory specific implementation: Public class BaomaFactory implements AbstractFactory {@Override public Car CreateCar () {return new BaomaCar (); public class BaomaFactory implements AbstractFactory (); } @Override public Tv createTv() { return new BaomaTv(); }} // Use Car Bencar = new BenChiFactory().createCar(); benCar.productCar(); Tv benTv= new BenChiFactory().createTv(); benTv.productTv(); Car baoCar= new BaoMaFactory().createCar(); baoCar.productCar(); Tv baoTv= new BaoMaFactory().createTv(); baoTv.productTv(); // The result is a new System. Out of the System, I will create a Benz TV



The factory pattern is typically used in combination with dynamic proxies and singletons

Struggle has been going on since man began