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.
- 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(); } }
- 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(); } }
- 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(); } }
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.