Factory Method Factory Mode Mode
name:
Also known as Virtual Constructor mode, polymorphic factory factory model
problem:
Consider an application framework to display multiple documents to the user. There are two main abstract class Application and Document, and customers must implement these two abstractions to implement specific classes. Suppose there is a specific class DrawApplication and DrawDocument, Applicatiob class to manage the Document class and create them, however, Application is unpredict which document will be instantiated, only knowing a document will be created, that is, the framework class must instance Class, but the frame class only knows the abstract class that cannot be instantiated.
solve:
Define a CREATEDocument method in Application, redefine this method by the subclass of Application to return the appropriate document subclass. Once an Application subclass is instantiated, you can instantiate the documentation related to the application without having to know these document classes. Createdocument is called "factory method" because it is responsible for "production" an object.
effect:
If you need to add a document, you need to add a new document class and create the corresponding factory class for this document class. For the client, there is no need to modify the image role or other specific factory class. For new products, systems with this mode fully support "open-closed" principles.
Figure