Factory method mode
Motivation to create an object often requires a complex process, so it is not suitable for inclusion in a composite factory. When there are new products, the composite factory needs to be modified, which is not conducive to expansion.
Moreover, the creation of some objects can require information that cannot be accessed by the composite factory. Therefore, define a factory interface and determine which product is instantiated by implementing this interface. This is the factory method pattern, which delays the instantiation of the class to the subclass.
Purpose
1. Define an interface and let the subclass decide which product to instantiate.
2. Create an object through a common interface.
accomplish
1. The product interface and specific products are easy to understand.
2. The factory class provides a factory method that returns a product object. But this factory method is abstract.
3. Implement factory methods in specific factories and complete the creation of specific products.
//Several Button classes class Button{/* ...*/} class WinButton extends Button{/* ...*/} class MacButton extends Button{/* ...*/} //Their factory classes interface ButtonFactory{ abstract Button createButton(); } class WinButtonFactory implements ButtonFactory{ Button createButton(){ return new WinButton(); } } class MacButtonFactory implements ButtonFactory{ Button createButton(){ return new MacButton(); } } Applicable scenarios
1. When there are more duplicate code when creating an object, you can consider using the factory method pattern to execute these duplicate parts.
2. Creating an object requires access to certain information, which should not be included in the factory class. Then subclasses can implement the creation of the object.
3. When you need to centrally manage the creation of objects to maintain consistency of the program.
Abstract factory pattern
Defining abstract factory patterns provides a way to encapsulate a set of separate factories with the same theme. In normal use, the client program needs to create a concrete implementation of the abstract factory, and then use the abstract factory as an interface to create a concrete object for this topic. The client program does not need to know (or care) that it gets the specific type of objects from these internal factory methods, because the client program only uses the common interface of these objects. The abstract factory pattern separates the implementation details of a set of objects from their general use.
A "factory" is where products (objects) are created, and its purpose is to separate the creation of products from their use. The purpose of the abstract factory model is to separate the interfaces of several abstract products from the specific implementation of different theme products. This way, when adding a new specific factory, you can do not need to modify the client code that references the abstract factory.
Using the abstract factory pattern allows you to change the specific factory without modifying the client code using the factory, even at runtime. However, using this pattern or similar design patterns may bring unnecessary complexity and additional work to writing code. Proper use of design patterns can offset such "extra work".
accomplish
1. AbstractFactory - Defines the interface method for creating abstract products.
2. ConcreteFactory - Implement method to create specific products.
3. AbstractProduct - An interface that declares different types of products.
4. Product - Define the specific products corresponding to ConcreteFactory and implement the AbstractProduct interface.
5. Client - Use AbstractFactory and AbstractProduct classes.
abstract class AbstractProductA{ public abstract void operationA1(); public abstract void operationA2(); } class ProductA1 extends AbstractProductA{ ProductA1(String arg){ System.out.println("Hello "+arg); } // Implement the code here public void operationA1() { }; public void operationA2() { }; } class ProductA2 extends AbstractProductA{ ProductA2(String arg){ System.out.println("Hello "+arg); } // Implement the code here public void operationA1() { }; public void operationA2() { }; } abstract class AbstractProductB{ //public abstract void operationB1(); //public abstract void operationB2(); } class ProductB1 extends AbstractProductB{ ProductB1(String arg){ System.out.println("Hello "+arg); } // Implement the code here } class ProductB2 extends AbstractProductB{ ProductB2(String arg){ System.out.println("Hello "+arg); } // Implement the code here } abstract class AbstractFactory{ abstract AbstractProductA createProductA(); abstract AbstractProductB createProductB(); } class ConcreteFactory1 extends AbstractFactory{ AbstractProductA createProductA(){ return new ProductA1("ProductA1"); } AbstractProductB createProductB(){ return new ProductB1("ProductB1"); } } class ConcreteFactory2 extends AbstractFactory{ AbstractProductA createProductA(){ return new ProductA2("ProductA2"); } AbstractProductB createProductB(){ return new ProductB2("ProductB2"); } } //Factory creator - an indirect way of instantiating the factories class FactoryMaker{ private static AbstractFactory pf=null; static AbstractFactory getFactory(String choice){ if(choice.equals("a")){ pf=new ConcreteFactory1(); }else if(choice.equals("b")){ pf=new ConcreteFactory2(); } return pf; } } // Client public class Client{ public static void main(String args[]){ AbstractFactory pf=FactoryMaker.getFactory("a"); AbstractProductA product=pf.createProductA(); //more function calls on product } }The FactoryMaker class uses the simple factory mode, while the implementation of specific factories uses the factory method mode.
Applicable scenarios
1. A system is independent of the creation, composition and representation of its products.
2. When a system is configured by one of multiple product families.
3. It is necessary to emphasize the design of a series of related product objects for joint use.
4. Provide a product library, and only want to display their interfaces instead of implementations.
advantage
1. Specific products are separated from customer code
2. A series that can easily change
3. Create a series of product families together
shortcoming
1. It is difficult to expand new products in the product family, it requires modifying the interfaces of the abstract factory and the specific factory.