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
First define an abstract product Class:
package com.fanqiechaodan.factory.simple.product; /** * @Classname Product * @Description 抽象产品类 */ public abstract class Product { public abstract void use(); }
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..."); } }
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); } } }
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(); } }
First define an interface to represent the product
package com.fanqiechaodan.factory.method.product; /** * @Classname Product * @Description 产品接口 */ public interface Product { void use(); }
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..."); } }
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(); }
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(); } }
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(); } }
First define the abstract product
public interface ProductA { void doSomething(); } public interface ProductB { void doSomething(); }
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 ..."); } }
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(); }
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(); } }
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(); } }
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!