Home> Java> javaTutorial> body text

How to use Java factory pattern to create objects and improve code reuse and flexibility

WBOY
Release: 2023-05-14 08:25:05
forward
702 people have browsed it

Introduction

Java factory design patterns are mainly divided into three types:

Simple Factory Pattern: Use a factory class to encapsulate the object creation process, and the client only needs Obtain different product objects by passing different parameters, thereby avoiding the client's direct creation of product objects.

Factory Method Pattern: Abstract the factory class, and each specific product class corresponds to A specific factory class. The factory class creates corresponding product objects through polymorphism. The client only needs to know the factory interface and its implementation class. It can dynamically switch the factory implementation according to the needs, and the scalability is better.

Abstract Factory Pattern: Based on the factory method pattern, the factory class is abstracted again, and multiple factory interfaces are put into one factory interface. Each specific product corresponds to a specific factory class. Through multiple To create the corresponding product object using statefulness, it has better scalability and higher level of abstraction, but it also increases the system complexity

Simple Factory Pattern

First define an abstract product Class:

package com.fanqiechaodan.factory.simple.product; /** * @Classname Product * @Description 抽象产品类 */ public abstract class Product { public abstract void use(); }
Copy after login

Then define the specific product class

public class ProductA extends Product{ @Override public void use() { System.out.println("使用具体产品类A..."); } } public class ProductB extends Product{ @Override public void use() { System.out.println("使用具体产品类B..."); } } public class ProductC extends Product{ @Override public void use() { System.out.println("使用具体产品类C..."); } }
Copy after login

Next define the factory class for creating different products

package com.fanqiechaodan.factory.simple.factory; import com.fanqiechaodan.factory.simple.product.Product; import com.fanqiechaodan.factory.simple.product.ProductA; import com.fanqiechaodan.factory.simple.product.ProductB; import com.fanqiechaodan.factory.simple.product.ProductC; /** * @Classname SimpleFactory * @Description 工厂类 */ public class SimpleFactory { public static Product createProduct(String type) { switch (type) { case "A": return new ProductA(); case "B": return new ProductB(); case "C": return new ProductC(); default: throw new RuntimeException("不支持的产品类型:" + type); } } }
Copy after login

Test:

package com.fanqiechaodan.factory.simple; import com.fanqiechaodan.factory.simple.factory.SimpleFactory; import com.fanqiechaodan.factory.simple.product.Product; /** * @Classname Demo * @Description 简单工厂模式 */ public class Demo { public static void main(String[] args) { Product productA = SimpleFactory.createProduct("A"); productA.use(); Product productB = SimpleFactory.createProduct("B"); productB.use(); Product productC = SimpleFactory.createProduct("C"); productC.use(); Product productD = SimpleFactory.createProduct("D"); productD.use(); } }
Copy after login

How to use Java factory pattern to create objects and improve code reuse and flexibility

Factory method pattern

First define an interface to represent the product

package com.fanqiechaodan.factory.method.product; /** * @Classname Product * @Description 产品接口 */ public interface Product { void use(); }
Copy after login

Secondly define two specific product implementation classes

public class ProductA implements Product{ @Override public void use() { System.out.println("使用具体产品A..."); } } public class ProductB implements Product{ @Override public void use() { System.out.println("使用具体产品B..."); } }
Copy after login

Then define a factory The interface is used to create products

package com.fanqiechaodan.factory.method.factory; import com.fanqiechaodan.factory.method.product.Product; /** * @Classname Factory * @Description 工厂接口 */ public interface Factory { Product createProduct(); }
Copy after login

Next, define two specific factory implementation classes, which are used to create different products

public class FactoryA implements Factory{ @Override public Product createProduct() { return new ProductA(); } } public class FactoryB implements Factory{ @Override public Product createProduct() { return new ProductB(); } }
Copy after login

Test

package com.fanqiechaodan.factory.method.factory; import com.fanqiechaodan.factory.method.product.Product; import com.fanqiechaodan.factory.method.product.ProductB; /** * @Classname FactoryB * @Description 工厂实现类B */ public class FactoryB implements Factory{ @Override public Product createProduct() { return new ProductB(); } }
Copy after login

How to use Java factory pattern to create objects and improve code reuse and flexibility

Abstract Factory Pattern

First define the abstract product

public interface ProductA { void doSomething(); } public interface ProductB { void doSomething(); }
Copy after login

Secondly define the specific product

public class ProductA1 implements ProductA{ @Override public void doSomething() { System.out.println("ProductA1 doSomething ..."); } } public class ProductA2 implements ProductA{ @Override public void doSomething() { System.out.println("ProductA2 doSomething ..."); } } public class ProductB1 implements ProductB{ @Override public void doSomething() { System.out.println("ProductB1 doSomething ..."); } } public class ProductB2 implements ProductB{ @Override public void doSomething() { System.out.println("ProductB2 doSomething ..."); } }
Copy after login

Then define the abstract factory

package com.fanqiechaodan.factory.abstractfactory.factory; import com.fanqiechaodan.factory.abstractfactory.product.ProductA; import com.fanqiechaodan.factory.abstractfactory.product.ProductB; /** * @Classname AbstractFactory * @Description 抽象工厂 */ public interface AbstractFactory { ProductA createProductA(); ProductB createProductB(); }
Copy after login

Next define Specific factory

public class Factory1 implements AbstractFactory{ @Override public ProductA createProductA() { return new ProductA1(); } @Override public ProductB createProductB() { return new ProductB1(); } } public class Factory2 implements AbstractFactory{ @Override public ProductA createProductA() { return new ProductA2(); } @Override public ProductB createProductB() { return new ProductB2(); } }
Copy after login

Test

package com.fanqiechaodan.factory.abstractfactory; import com.fanqiechaodan.factory.abstractfactory.factory.AbstractFactory; import com.fanqiechaodan.factory.abstractfactory.factory.Factory1; import com.fanqiechaodan.factory.abstractfactory.factory.Factory2; import com.fanqiechaodan.factory.abstractfactory.product.ProductA; import com.fanqiechaodan.factory.abstractfactory.product.ProductB; /** * @Classname Demo * @Description 抽象工厂模式 */ public class Demo { public static void main(String[] args) { // 使用具体工厂1创建产品A和产品B AbstractFactory factory1 = new Factory1(); ProductA productA1 = factory1.createProductA(); ProductB productB1 = factory1.createProductB(); productA1.doSomething(); productB1.doSomething(); // 使用具体工厂2创建产品A和产品B AbstractFactory factory2 = new Factory2(); ProductA productA2 = factory2.createProductA(); ProductB productB2 = factory2.createProductB(); productA2.doSomething(); productB2.doSomething(); } }
Copy after login

How to use Java factory pattern to create objects and improve code reuse and flexibility

The above is the detailed content of How to use Java factory pattern to create objects and improve code reuse and flexibility. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
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!