When I wrote this essay, I thought about it for a long time. First, in fact, there are already many articles about the factory model in the blog garden. if I continue to write this article, I found that I did not write much better than the previous "Predecessors" :). Second, chapter 6 of "essentials and patterns of software design" is not particularly well written, it can only be used as a reference for the preliminary study of the factory model. It doesn't make much sense to write these Reading Notes. So I wrote this reading note to sort out the good information about the factory model in the blog Park for your convenience.
In addition, there are too many duplicate information about the factory information in the blog park. We suggest you check whether there are any duplicate information in the future. You can write it yourself to help yourself and everyone. We all encourage and praise this kind of information, however, if you write something repeated, it means that you are not correct. The bigger one is that you still post it on the homepage ...; In addition, I hope that the source can be indicated when someone else's content is being extracted. I found someone else posted a post today, but I didn't see the source. When I want to quote it, I do not know the original author. So I quoted the author's article, but because I did not know the original author and did not specify it, I hope the original author does not mind or tell me that I will update the article; third, if you add source code to your blog, it is best to describe the source code environment, for example. net2003 + MSSQL and so on. We recommend that you do not use an excessively high version of the source code environment.
The reason why I put this article on the homepage of the blog garden is that I have repeatedly considered the three proposals above and decided to put this essay on the homepage, it's not because I 've excerpted some articles from everyone. If you have any comments on me, I would like to apologize to you first.
First of all, you still need to know some basic information about the factory model: (some of the information here is excerpted from some blogs. I will refer to the reference materials later .)
The design pattern describes how to define an interface for creating objects, so that the subclass decides to instantiate the class. Factorymethod delays the instantiation of a class to the subclass.
1. Abstract Factory is a creation mode to solve the problems brought about by instantiation.
2. Abstract factories include simple factories, factory methods, abstract factories, and reflection factories;
3. Conditions for using the abstract factory model:
A) a system should not depend on how the product is created, combined, or expressed;
B) There are multiple product families, and the system only consumes products in one of them;
C) products belonging to the same product family are used together;
D) The system provides a product library. All products are implemented using the same interface.
4. Main Points of the abstract factory mode:
A) if the requirement for multi-series object building does not change, the abstract factory mode is not necessary.
B) "series objects" refer to the relationships between objects that are mutually dependent or act.
C) The Abstract Factory mode mainly aims to cope with the changes in the needs of the new series. The disadvantage is that it is difficult to cope with changes in the demand for "new objects. It should be noted that, as mentioned above, if we want to add other series classes, the code will be greatly changed.
D) The Abstract Factory mode is often combined with the factory method mode to cope with changes in the "Object creation" requirement.
5. General Abstract Factory principles: Try to use the base class method whenever possible
Differences from a simple abstract factory:
A) Factory is used to create a type of product, while abstractfactory is used to create a product family.
Abstract Factory roles:
A) Abstract Factory. This role is at the core of the factory method model and is unrelated to the application. Any factory class that creates an object in the mode must implement this interface or inherit this class.
B). The specific factory (concrete factory). This role directly creates a product instance under the client call. This role contains the logic for selecting appropriate product objects, which is closely related to the business logic of the application system.
C). Abstract product. The class that assumes this role is the parent class of the object created in the factory method mode or the interfaces they share.
D). A specific product (concrete factory). Any product object created in the abstract factory mode is an instance of a specific product class. This is what the client eventually needs, and it must be filled with the commercial logic of the application system.
6. Advantages and disadvantages of the reflection Factory
The advantage of using a reflection factory is that it greatly reduces the number of factory classes, reduces code redundancy, and makes the system more scalable. After adding new types, you do not need to modify the factory classes.
The cost of using a reflection factory is that the dependency between the factory and the product is not obvious. Because it is a dynamic binding, a factory can be used in theory to instantiate many types, making the code hard to understand. In addition, the test difficulty is increased, and creation is completed dynamically. It is more difficult to write and execute test cases than the traditional factory.
This is what I personally think is useful for your reference:
1. Simple Factory
Disadvantage: cannot cope with "changes to different series of objects ";
From simple to deep learning "factory model" (1)
Interpretation of design patterns-simplefactory pattern. I will give you whatever you want.
2. Factory method
From simple to deep learning "factory model" (2)
3. Abstract Factory
From simple to deep learning "factory model" (3)
4. Reflection Factory
Model type factory model (this article is generally written, but leave it blank)
Dot Net Design Pattern-reflection Factory
5. Comprehensive
Factory model siblings
In addition, some good articles will inevitably be missed because of my own arrangement of this article. I hope you will keep up with the posts and I will update them at any time.
References:
Design Pattern learning note (3) -- abstract factory pattern
Abstract Factory Model
Factorymethod factory Mode
Abstract Factory mode Summary