Home  >  Article  >  Java  >  How to implement the factory method pattern using Java code

How to implement the factory method pattern using Java code

WBOY
WBOYforward
2023-05-16 17:46:15886browse

1. Introduction to the factory method pattern

Definition: The factory method pattern is a further abstraction of the simple factory pattern. The advantage is that it can introduce new products into the system without modifying the original code, which meets the development requirements. closure principle. Create an interface for object instantiation and let the subclass decide which class is instantiated, thus deferring the class instantiation process to the subclass.

2. Factory method pattern class diagram UML

How to implement the factory method pattern using Java code

3. Factory method pattern role description

Abstract Factory (AbstractFactory): Provided Create an interface for products, through which the caller accesses the factory method of a specific factory to create a product

Concrete Factory (ConcreteFactory): implements the methods defined by the abstract factory and completes the creation of specific products

Abstract Product (Product): Defines the specifications of the product and describes the main features and performance of the product

Concrete Product (ConcreteProduct): Implements the method of defining abstract products. There are concrete factories to create products, concrete factories and concrete One-to-one correspondence between products

4. Code implementation

Factory method pattern implementation:

/**
 * 抽象工厂:提供具体工厂的共有方法
 */
public interface Product {
    public void show();
}

public class ConcreteProduct1 implements Product {
    @Override
    public void show() {
        System.out.println("具体产品1展示...");
    }
}

public class ConcreateProduct2 implements Product {
    @Override
    public void show() {
        System.out.println("具体产品2展示...");
    }
}


/**
 * 抽象工厂
 */
public interface AbstractFactory {
    public Product createProduct();
}

/**
 * 具体工厂1产生具体产品1
 */
public class ConcreteFactory1 implements AbstractFactory {
    @Override
    public Product createProduct() {
        ConcreteProduct1 concreteProduct1 = new ConcreteProduct1();
        System.out.println("具体工厂1创建具体产品1...");
        return concreteProduct1;
    }
    
public class ConcreteFactory2 implements AbstractFactory {
    @Override
    public Product createProduct() {
        System.out.println("具体工厂2产生具体产品2...");
        return new ConcreateProduct2();
    }
}

Calling method:

//获取具体产品1
        Product product = new ConcreteFactory1().createProduct();
        product.show();

        Product product1 = new ConcreteFactory2().createProduct();
        product1.show();

Usage steps:

  • Create an abstract product class and define the public methods of the product

  • Create a specific product class (implement the abstract product interface) and define the generated specific product

  • Create an abstract factory class and define the public interface of the specific factory

  • Create a specific factory class and define the method to create a corresponding specific product instance

  • The caller calls the method of the specific factory class to create instances of different specific products

5. Explore the factory pattern again

Solve the problem: Solve It solves the logical problem of modifying the method logic of the factory class when adding new products to the simple factory class, which is the special opening and closing principle.

Exit the specific product creation to the subclass of the factory class (specific factory). At this time, the factory class is no longer responsible for the creation of all products, but gives the interface that the specific factory must implement, so that the factory method is When adding new products, there is no need to modify the logic of the factory class. Instead, a new factory subclass is added, which conforms to the opening and closing principle.

6. Advantages and Disadvantages

Advantages:

  • High flexibility. For the creation of new products, you only need to write one more corresponding factory class

  • Users only need to guide the name of the factory to get the desired product, and do not need to guide the specific product creation process

Disadvantages:

  • It is easy to have too many classes, which increases the complexity

  • Each factory can only produce one kind of product. This shortcoming can be solved by using the abstract factory pattern.

7. Application scenarios

Customers only pay attention to the name of the factory that creates the product, and do not need to know the specific product name

The above is the detailed content of How to implement the factory method pattern using Java code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete