The factory method mode, also known as the factory mode, is also called the virtual constructor mode or the polymorphic factory mode, which belongs to the class creation mode. In the factory method mode, the parent class is responsible for defining the public interfaces for creating objects, while the Child class is responsible for generating specific objects, the purpose of this operation is to delay the class instantiation operation to the subclass, that is, the subclass determines which class should be instantiated (created.
In simple factory mode, a factory class is at the center of product class instantiation. It knows the details of each product class and determines when and when a product class should be instantiated. The advantage of the simple factory mode is that the client can be independent from the product creation process, and there is no need to modify the client when introducing new products to the system. The disadvantage is that when a new product is to be added to the system, you must modify the factory class to add the necessary processing logic. The fatal weakness of the simple factory mode is the factory class at the core address, because once it cannot determine which class to instantiate, it cannot be used, the factory method can solve this problem well.
The UML diagram of the factory method mode is as follows:
The relationships between classes and objects are as follows:
1. Product: Define the product interface.
2. concreteproduct: The product class that implements the interface.
3. Creator: Declares the factory method and returns a product.
4. concretecreator (real factory): implements the factorymethod factory method, which is called by the customer and returns a product instance.
The following figure shows the order of typical applications. Different factories are responsible for creating different products.
- Instance 1-Multi-Document System
The application factory method mode provides the maximum flexibility to generate different objects. Abstract classes can return a default object, and each derived subclass can return other extended objects.
Code
// Product base
Abstract Class Page
{
}
// Factory abstract class
Abstract Class Document
{
Protected Arraylist pages = New Arraylist ();
Public Document ()
{
This . Createpages ();
}
Public Arraylist pages
{
Get { Return Pages ;}
}
Abstract Public Void Createpages ();
}
// Derived class, specific product class-skill page
Class Skillspage: Page
{
}
// Derived class, specific product class-Education Page
Class Educationpage: Page
{
}
// Derived class, specific product class-experience page
Class Experiencepage: Page
{
}
// Derived class, specific product class -- Introduction page
Class Introductionpage: Page
{
}
// Derived class, specific product class -- result page
Class Resultpage: Page
{
}
// Derived class, specific product class -- Conclusion page
Class Conclusionpage: Page
{
}
// Derived class, specific product class -- Summary Page
Class Summarypage: Page
{
}
// Derived class, specific product class -- document page
Class Bibliographypage: Page
{
}
// Factory-resume, including skills, education, and experience
Class Resume: Document
{
Public Override Void Createpages ()
{
Pages. Add ( New Skillspage ());
Pages. Add ( New Educationpage ());
Pages. Add ( New Experiencepage ());
}
}
// Specific factory-reports, including introduction, results, conclusions, summaries, and documents
Class Report: Document
{
Public Override Void Createpages ()
{
Pages. Add ( New Introductionpage ());
Pages. Add ( New Resultpage ());
Pages. Add ( New Conclusionpage ());
Pages. Add ( New Summarypage ());
Pages. Add ( New Bibliographypage ());
}
}
// Customer application testing
Class Client
{
[Stathread]
Static Void Main ( String [] ARGs)
{
Document [] docs = New Document [ 2 ];
Docs [ 0 ] = New Resume ();
Docs [ 1 ] = New Report ();
Foreach (Document document In Docs)
{
Console. writeline ( " \ N " + Document + " -------------- " );
Foreach (Page In Document. Pages)
{
Console. writeline ( " " + Page );
}
}
Console. Read ();
}
}
- Instance 2-extended mobile phone Factory
In reality, mobile phones of different brands should be made in different factories. Next we will extend the example of a simple factory for mobile phones described in the previous section. The applied mode is the factorymethod model.
Code
// Mobile phone interface
Public Interface Imobile
{
Void Call ();
}
// Phone factory Interface
Public Interface Imobilefactory
{
Imobile producemobile ();
}
// Motorola mobile phone real-world mobile phone interface
Public Class Motorola: imobile
{
Public Void Call ()
{
Console. writeline ( " Motorola mobile phone " );
}
}
// Call interfaces for Nokia mobile phones
Public Class Nokia: imobile
{
Public Void Call ()
{
Console. writeline ( " Nokia mobile phone " );
}
}
// The Motorola factory implemented the method of producing mobile phones and returned to Motorola mobile phones.
Public Class Motorolafactory: imobilefactory
{
Public Imobile producemobile ()
{
Console. writeline ( " Manufactured by Motorola " );
Return New Motorola ();
}
}
// The Nokia factory implemented the method of producing mobile phones and returned to Nokia mobile phones.
Public Class Nokiafactory: imobilefactory
{
Public Imobile producemobile ()
{
Console. writeline ( " Made at Nokia factory " );
Return New Nokia ();
}
}
// Customer application testing
Class Client
{
[Stathread]
Static Void Main ( String [] ARGs)
{
Imobilefactory MF;
Imobile m;
Mf = New Motorolafactory ();
M = Mf. producemobile ();
M. Call ();
Mf = New Nokiafactory ();
M = Mf. producemobile ();
M. Call ();
Console. Read ();
}
}
In the factory method mode, the factory method is used to create the product required by the customer, and the details of the specific product category to be instantiated are also hidden from the customer. The core of the factory method mode is an abstract factory class. Various factory classes inherit factory methods by abstracting factory classes. This allows the customer to focus only on the abstract product and the abstract factory, regardless of which specific product is returned, or how it is created by a specific factory.
1. The design of polymorphism Based on the factory role and Product role is the key to the factory method model. It allows the factory to determine which product object to create, and the details of how to create this object are completely encapsulated in a specific factory. The factory method mode is also called the multi-state factory mode because all the factory classes have the same abstract parent class.
2. another advantage of using the factory method mode is that when a new product is added to the system, you do not need to modify the interfaces provided by the abstract factory and abstract product, and you do not need to modify the client, you do not need to modify other specific factories and products, but you only need to add a specific factory and product. In this way, the system is highly scalable. The excellent object-oriented design encourages the use of encapsulation and delegation to construct software systems. The factory method mode is a typical example of using encapsulation and delegation, encapsulation is embodied by the abstract factory, while delegation is embodied by the Abstract Factory, which gives the responsibility for creating objects to specific factories.
3. the disadvantage of using the factory method mode is that when adding a new product, you need to write a new specific product class and provide the corresponding specific factory class. when both are relatively simple, the system has additional costs.
The following scenario is suitable for the factory method mode:
1. The class does not know which object to create.
2. The class uses its subclass to specify the object to be created.
3. The customer needs to know which object to create.