How to implement the factory method pattern using Java code
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
