Differences between simple factory mode, Factory method mode, prototype mode, and abstract Factory method mode
Source: Internet
Author: User
All kinds of factory models are designed to solve a basic problem: when constructing a class, you must know its specific class name. This is a problem to be solved by coupling various factory models and is dedicated to solving a basic problem: when constructing a class, you must know its specific class name. This is a coupling.
1. simple factory mode: use the factory method to pass a parameter to determine the specific class to be generated. This parameter is usually a string. Converts class name dependencies into string content dependencies, which undoubtedly reduces coupling;
2 Factory method mode: a factory class has many sub-classes. The factory method is set as a virtual function, and different factory subclasses generate corresponding product subclasses in their factory methods. In this way, when we have a pointer to the factory class, we can dynamically generate the required product subclass;
3. prototype: The biggest disadvantage of the factory method is that there must be an inheritance system of the same complex factory class for a product class corresponding to an inheritance system. Can we place factory methods in the factory class into the product class itself? In this case, the two inheritance systems can be one. This is the idea of the prototype mode. if the factory method in the prototype is clone, it returns a copy (which can be a shortest copy or a deep copy, determined by the designer ). In order to ensure that the user code can then use the pointer to call clone to dynamically bind to generate the required specific class. These prototype objects must be constructed in advance.
Another benefit of prototype to the factory method model is that the copy efficiency is generally high for the construction efficiency.
4. Abstract Factory mode: usually implemented by the factory method mode. However, a factory usually contains multiple factory methods to generate a series of products. This mode emphasizes that the customer code ensures that only one series of products are used at a time. If you want to switch to another product series, change to a factory type.
In addition to the factory method mode, the abstract Factory mode can also be implemented using the prototype mode.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.