Java Implementation Factory Pattern
Factory pattern is a creative design pattern used to create objects. It hides the specific implementation of the object and only provides a unified interface to the client to create the object. By delegating the creation logic of the object to the factory class, the client can avoid directly instantiating the object, thereby improving the maintainability and flexibility of the code. Applicable scenarios: When a class does not know or care about the class of the object it needs to create, the factory pattern can be used. For example, the client only needs to know the abstract product class interface without knowing the specific product class implementation. When a class wishes to have subclasses specify the creation of objects, the factory pattern can be used, for example, the product creation process can be moved to subclasses. When a class needs to create different objects based on runtime conditions, factory mode can be used. Benefits: 1. The factory mode separates the creation and use of objects, so that the client only needs to care about the use of objects and does not need to care about the object creation process. 2. The factory model meets the open closed principle, which is open for expansion and closed for modification. You can extend the factory by adding new specific product classes without modifying the client code. 3. The factory mode can effectively encapsulate the details of creating objects, making the client code more concise and readable. The following is a complete sample code for implementing the factory pattern using the Java language: ```java //Abstract Product Class interface Product { void operation(); } //Specific product category A class ConcreteProductA implements Product { @Override public void operation() { System.out.println("ConcreteProductA"); } } //Specific product category B class ConcreteProductB implements Product { @Override public void operation() { System.out.println("ConcreteProductB"); } } //Factory class class Factory { public Product createProduct(String productType) { if (productType.equals("A")) { return new ConcreteProductA(); } else if (productType.equals("B")) { return new ConcreteProductB(); } else { throw new IllegalArgumentException("Invalid product type"); } } } //Client class Client { public static void main(String[] args) { Factory factory = new Factory(); Product productA = factory.createProduct("A"); productA.operation(); Product productB = factory.createProduct("B"); productB.operation(); } } ``` In the above code, 'Product' represents an abstract product class, 'ConcreteProductA' and 'ConcreteProductB' represent specific product classes, and 'Factory' represents factory classes` The 'createProduct' method in the Factory class creates corresponding specific product objects based on the passed in product type. In the client code, create a specific product object through the 'createProduct' method of the factory class and call its' operation 'method. Based on the incoming product type, the factory class returns the corresponding specific product object. The output result is: ``` ConcreteProductA ConcreteProductB ```