Home > Java > javaTutorial > What is factory pattern in java

What is factory pattern in java

小老鼠
Release: 2023-12-26 16:15:13
Original
1351 people have browsed it

In Java, the factory pattern is a creational design pattern that provides an interface for creating objects, but the subclass decides which class to instantiate. The factory pattern mainly includes roles: 1. Abstract product: defines the interface of the product, and the specific product class will implement this interface; 2. Specific product: implements the abstract product interface, which is the object created by the factory pattern; 3. Abstract factory: declares An interface for creating products is provided, in which the factory method is used to instantiate a product; 4. Concrete factory: implements the abstract factory interface and is responsible for instantiating specific products.

What is factory pattern in java

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In Java, the Factory Pattern is a creational design pattern that provides an interface for creating objects, but the subclass decides which class to instantiate. In this way, the instantiation process of a class is delayed to its subclasses, so that the instantiation of a class can be determined by its subclasses.

The factory pattern mainly includes the following roles:

Abstract product (Product): defines the interface of the product, and the specific product class will Implement this interface.

Concrete Product: Implements the abstract product interface and is an object created by the factory pattern.

Abstract Factory (Creator): declares an interface for creating products, and the factory method is used to instantiate a product.

Concrete Factory (Concrete Creator): Implements the abstract factory interface and is responsible for instantiating specific products.

The main advantage of using the factory pattern is to decouple the client code from the specific class implementation. The client only needs to know the interfaces of the abstract factory and abstract products, but does not need to know the specific products. implementation details.

The following is a simple factory pattern example:

##

// 抽象产品
interface Product {
    void display();
}
// 具体产品A
class ConcreteProductA implements Product {
    @Override
    public void display() {
        System.out.println("Product A");
    }
}
// 具体产品B
class ConcreteProductB implements Product {
    @Override
    public void display() {
        System.out.println("Product B");
    }
}
// 抽象工厂
interface Creator {
    Product createProduct();
}
// 具体工厂A,用于创建ProductA
class ConcreteCreatorA implements Creator {
    @Override
    public Product createProduct() {
        return new ConcreteProductA();
    }
}
// 具体工厂B,用于创建ProductB
class ConcreteCreatorB implements Creator {
    @Override
    public Product createProduct() {
        return new ConcreteProductB();
    }
}
// 客户端代码
public class Client {
    public static void main(String[] args) {
        Creator creatorA = new ConcreteCreatorA();
        Product productA = creatorA.createProduct();
        productA.display();  // 输出: Product A
        Creator creatorB = new ConcreteCreatorB();
        Product productB = creatorB.createProduct();
        productB.display();  // 输出: Product B
    }
}
Copy after login


In this example, Product is the abstract product interface, and ConcreteProductA and ConcreteProductB are the implementations of specific products. Creator is an abstract factory interface, and ConcreteCreatorA and ConcreteCreatorB are implementations of concrete factories. The client creates an instance of a specific product by calling the factory method of the specific factory without directly instantiating the specific product class. In this way, if you need to change the implementation of the product, you only need to modify the implementation of the specific factory without affecting the client code.

The above is the detailed content of What is factory pattern in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template