Home > Java > javaTutorial > body text

Research on three design methods of Java factory pattern

WBOY
Release: 2024-02-18 17:16:06
Original
1069 people have browsed it

Research on three design methods of Java factory pattern

Explore three design ideas of Java factory pattern

Factory pattern is a commonly used design pattern for creating objects without specifying a specific class. In Java, the factory pattern can be implemented in many ways. This article will explore the implementation of three Java factory patterns based on different design ideas and give specific code examples.

  1. Simple Factory Pattern

The simple factory pattern is the most basic factory pattern, which creates objects through a factory class. The factory class determines what kind of specific object should be created based on the client's request parameters. The following is an example of a simple factory pattern:

// 抽象产品接口
interface Product {
    void use();
}

// 具体产品A
class ConcreteProductA implements Product {
    @Override
    public void use() {
        System.out.println("使用具体产品A");
    }
}

// 具体产品B
class ConcreteProductB implements Product {
    @Override
    public void use() {
        System.out.println("使用具体产品B");
    }
}

// 简单工厂类
class SimpleFactory {
    public static Product createProduct(String type) {
        if (type.equals("A")) {
            return new ConcreteProductA();
        } else if (type.equals("B")) {
            return new ConcreteProductB();
        }
        return null;
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        Product productA = SimpleFactory.createProduct("A");
        productA.use();
        
        Product productB = SimpleFactory.createProduct("B");
        productB.use();
    }
}
Copy after login
  1. Factory method pattern

The factory method pattern defines an interface for creating objects and lets subclasses decide what to instantiate the type. The factory method pattern can achieve decoupling of products and clients. The following is an example of the factory method pattern:

// 抽象产品接口
interface Product {
    void use();
}

// 具体产品A
class ConcreteProductA implements Product {
    @Override
    public void use() {
        System.out.println("使用具体产品A");
    }
}

// 具体产品B
class ConcreteProductB implements Product {
    @Override
    public void use() {
        System.out.println("使用具体产品B");
    }
}

// 抽象工厂类
interface Factory {
    Product createProduct();
}

// 具体工厂A
class ConcreteFactoryA implements Factory {
    @Override
    public Product createProduct() {
        return new ConcreteProductA();
    }
}

// 具体工厂B
class ConcreteFactoryB implements Factory {
    @Override
    public Product createProduct() {
        return new ConcreteProductB();
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        Factory factoryA = new ConcreteFactoryA();
        Product productA = factoryA.createProduct();
        productA.use();
        
        Factory factoryB = new ConcreteFactoryB();
        Product productB = factoryB.createProduct();
        productB.use();
    }
}
Copy after login
  1. Abstract Factory Pattern

The abstract factory pattern is a higher-level method that combines multiple factory methods together. Design Patterns. It provides an interface for creating different types of product families. The following is an example of the abstract factory pattern:

// 抽象产品A接口
interface ProductA {
    void useProductA();
}

// 具体产品A1
class ConcreteProductA1 implements ProductA {
    @Override
    public void useProductA() {
        System.out.println("使用具体产品A1");
    }
}

// 具体产品A2
class ConcreteProductA2 implements ProductA {
    @Override
    public void useProductA() {
        System.out.println("使用具体产品A2");
    }
}

// 抽象产品B接口
interface ProductB {
    void useProductB();
}

// 具体产品B1
class ConcreteProductB1 implements ProductB {
    @Override
    public void useProductB() {
        System.out.println("使用具体产品B1");
    }
}

// 具体产品B2
class ConcreteProductB2 implements ProductB {
    @Override
    public void useProductB() {
        System.out.println("使用具体产品B2");
    }
}

// 抽象工厂接口
interface AbstractFactory {
    ProductA createProductA();
    ProductB createProductB();
}

// 具体工厂1
class ConcreteFactory1 implements AbstractFactory {
    @Override
    public ProductA createProductA() {
        return new ConcreteProductA1();
    }

    @Override
    public ProductB createProductB() {
        return new ConcreteProductB1();
    }
}

// 具体工厂2
class ConcreteFactory2 implements AbstractFactory {
    @Override
    public ProductA createProductA() {
        return new ConcreteProductA2();
    }

    @Override
    public ProductB createProductB() {
        return new ConcreteProductB2();
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        AbstractFactory factory1 = new ConcreteFactory1();
        ProductA productA1 = factory1.createProductA();
        ProductB productB1 = factory1.createProductB();
        productA1.useProductA();
        productB1.useProductB();
        
        AbstractFactory factory2 = new ConcreteFactory2();
        ProductA productA2 = factory2.createProductA();
        ProductB productB2 = factory2.createProductB();
        productA2.useProductA();
        productB2.useProductB();
    }
}
Copy after login

Through the above examples, we can see the specific implementation of the Java factory pattern under different design ideas. The simple factory pattern is suitable for situations where there are few and simple objects, the factory method pattern provides higher flexibility, and the abstract factory pattern is suitable for creating multiple product families. In actual applications, you can choose a suitable factory pattern to create objects according to specific business needs.

The above is the detailed content of Research on three design methods of Java factory pattern. For more information, please follow other related articles on the PHP Chinese website!

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