Home > Java > javaTutorial > body text

Java Design Patterns Factory Method Pattern Revealed

WBOY
Release: 2024-05-09 14:42:01
Original
861 people have browsed it

The factory method pattern is a creational design pattern. It defines the process of creating objects through an interface, and delegates the specific creation process to a specific factory class that implements the interface, thus decoupling the creation process and specific classes, making it easy to expand and improve. Testability. The main structures include abstract factory, concrete factory and product.

Java Design Patterns Factory Method Pattern Revealed

Java Design Pattern Factory Method Pattern Revealed

Introduction

Factory Method A pattern is a creational design pattern that allows programmers to define an interface for creating objects but abstracts the creation process. This means that the process of concrete object creation can be handled by a concrete factory subclass that implements this interface.

Advantages of the factory method pattern

  • Decouple the creation process from the concrete class.
  • Easy to extend, new product types can be supported by adding new factory subclasses.
  • Improve code testability because the creation process can be isolated for testing.

Structure

The factory method pattern mainly consists of three parts:

  • Abstract Factory: Define the interface required to create the object.
  • Specific factory: Implements the interface for creating products, used to create instances of specific types of products.
  • Product: Object created by factory method.

Code Example

The following is a Java code example of the factory method pattern:

// 抽象工厂接口
interface ShapeFactory {
    Shape createShape(ShapeType type);
}

// 具体工厂类
class CircleFactory implements ShapeFactory {
    @Override
    public Shape createShape(ShapeType type) {
        return new Circle();
    }
}

// 具体工厂类
class SquareFactory implements ShapeFactory {
    @Override
    public Shape createShape(ShapeType type) {
        return new Square();
    }
}

// 产品类
class Shape {
    private String type;
    public Shape(String type) {
        this.type = type;
    }
    public String getType() {
        return type;
    }
}

// 圆形产品类
class Circle extends Shape {
    public Circle() {
        super("Circle");
    }
}

// 正方形产品类
class Square extends Shape {
    public Square() {
        super("Square");
    }
}

// 客户端代码
public class Main {
    public static void main(String[] args) {
        ShapeFactory circleFactory = new CircleFactory();
        Shape circle = circleFactory.createShape(ShapeType.CIRCLE);
        System.out.println(circle.getType()); // 输出: Circle

        ShapeFactory squareFactory = new SquareFactory();
        Shape square = squareFactory.createShape(ShapeType.SQUARE);
        System.out.println(square.getType()); // 输出: Square
    }
}
Copy after login

Conclusion

The factory method pattern is a flexible and extensible design pattern that provides a way to decouple the creation process from concrete classes. This makes the code easier to maintain and extend, and improves testability.

The above is the detailed content of Java Design Patterns Factory Method Pattern Revealed. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!