Intent • Define an interface for creating an object, but letsubclasses decide which class to instantiate. factory Method lets a class deferinstantiation to subclasses. • Create an interface for the object to be created so that the subclass can create the object. Applicability • a class can't anticipate the class of objects it mustcreate. • a class is not sure which of the objects it must create. • A class wants its subclasses to specify the objects itcreates • A class requires its subclass to determine which object to create. • Classes delegate responsibility to one of several helpersubclasses, and you want to localize the knowledge of which helper subclass isthe delegate. • delegate the function of the created object to the subclass and want these functions to be delegated to the subclass in a centralized manner. Consequences • it is more flexible to generate objects within the factory method than to generate objects directly. • Connects parallel class hierarchies. What is the difference between the UML factory method and the simple factory method? • A factory method interface is added. • The Factory method supports polymorphism, which means that the created object is postponed to the subclass. Example --- the iterator Collection Interface contains a method Iterator, which is implemented by ArrayList and sorted list. For ArrayList and sorted list, their Iterator objects are returned. For this Collection. iterator method, it is the factory method. The ArrayList and lifecycle list return specific products. For the above, JDK is different from this. It is mainly abstracted for explanation. For details, you can read the source code in JDK to clarify the relationship between them. In addition, Iterator is the iteration mode. • Collection. Iterator () is a factory method role • Collection is an abstract Creator role. • Iterator is an abstract Product role. • Itr (available with jdk Source Code) is a specific product role. (In fact, the template method mode is used in Itr. If you are interested, refer to the source code for analysis ). Example -- toString • the object information string is pieced together and combined according to its own needs. It is equivalent to postponing the implementation of the Object Information string created by toString In the subclass. • ToString is FactoryMethod. • Object is the abstract Creator. • The String object returned by toString is the product. Factory methods and five basic principles • compliant with the principle of opening and closing. • The Factory method cannot meet the requirements when multiple product levels exist. Is the Iterator and toString template mode?