Disclaimer: I will publish this chapter to share with you the knowledge points in this book. If infringement is involved, ask the author or relevant persons to leave a message, and I will delete it.
The Factory Method mode is the most widely used design mode. After all, it is responsible for the creation of a series of objects, and the creation of objects is the most tedious behavior in object-oriented programming. The design pattern book writes, "the Factory Method pattern delays the instantiation of a class to the subclass ." To be accurate, the Factory Method mode transfers the responsibility for creating an object instance to the Factory class, and uses the abstract principle to delay the instantiation behavior to a specific Factory class.
The plan of "Jiangdong zhuanzhu"
In the object-oriented design, object management is the core. Object creation is the first step in object management. Object creation is very simple. in C #, you only need to use the new operator to call the object construction method. Therefore, the most important thing to manage an object is to grasp the time to create an object.
First, let's look at the features of objects. The types that represent the abstract meanings, such as interfaces and abstract classes, cannot create object instances, which means that the objects we want to create are all related to object types. That is to say, the creation of objects involves implementation details in the design, which leads to an enhanced coupling between the creator and the specific creation. For example, if you need to create some graphic objects, such as Circle and Square, in a project, their structure is shown in.
This class structure is very in line with object-oriented thinking. It abstracts Square and Circle objects through the IShape interface. According to the polymorphism principle, we can use the IShape interface in the program to replace the specific Square and Circle classes, so as to bind the specific object type to the runtime, however, because the interface object cannot be created, once the project needs to create an IShape type object, it is necessary to create a specific class object. For example, IShape shape = new Square ();
If you are developing a graphical tool, you must frequently create objects such as Square and Circle. It can be imagined that, in the various components of this project, if a lot of the above Code lines are filled, the result is that each module cannot be decoupled from the Square object. When we need to change the created object to Circle, we must modify the module that calls the new Square () operation. This undoubtedly increases the workload of modification, and also leads to project scalability and reusability of modules. For the abstract IShape interface of graphic objects, it also becomes an unnecessary and failed design.
Our goal is to put the disaster that this change has brought to the project into the cradle. At this time, we need to use the principle of "encapsulating changes. We can introduce the Factory Method mode to encapsulate the object creation behavior. The IShape type object is the product to be produced by the factory. In the Factory Method mode, the structure of the Factory object should be parallel to the structure of the product, and correspond to one of them. Since there are two specific product objects, the corresponding factory objects should also be two, SquareFactory and CircleFactory. At the same time, we should also abstract a common factory interface IShapeFactory for the two factories.
The implementation code is as follows: 1 namespace DonOfDesign. FactoryPattern. GraphicLib
2 {
3 public interface IShapeFactory
4 {
5 IShape CreateShape ();
6}
7}
8 ----------------------------------------------
9 namespace DonOfDesign. FactoryPattern. GraphicLib
10 {
11 public class SquareFactory: IShapeFactory
12 {
13 public IShape CreateShape ()
14 {
15 return new Square ();
16}
17}
18}
19 ----------------------------------------------
20 namespace DonOfDesign. FactoryPattern. GraphicLib
21 {
22 public class CircleFactory: IShapeFactory
23 {
24 public IShape CreateShape ()
25 {
26 return new Circle ();
27}
28}
29}
30
31
The Factory Method mode encapsulates the creation of objects, transfers all the code for creating specific product objects to their own Factory objects, and implements the code in the CreateShape Method. The entire structure is as follows:
The returned object of the CreateShape method is the IShape interface type, which effectively avoids the dependency between the factory object and the specific product object and decouples the creator and the Creator.
I think the real wise man will not be confused by the appearance. as a matter of fact, the introduction of the Factory Method mode is a trick to "marry Jiang Dong", and he still cannot get rid of the responsibilities transferred to the Factory object. However, in the structure of the Factory class, there are still specific factory objects. I solved the dependencies between modules and specific images, but increased the dependencies on specific factory objects. What benefits will this bring to our design?
Let's analyze the frequency of object creation. for a graphic analysis tool, the creation of IShape objects is undoubtedly frequent, and the biggest possible line is that there may be a need to create an IShape object in each module of this project. the factory object is different. We can concentrate on a module, initialize the factory object, and directly call the CreateShape method of the factory instance when the IShape object is required.
Therefore, the introduction of factory objects is not simply to establish a corresponding factory for the product, but to divide the responsibilities of each module and place the creation of factory objects in a proper place. The best solution is to combine the responsibilities of creating factory objects into a module, instead of creating factory objects when you need to create products. An error example is that the factory object creation and product object creation are put together during product creation and distributed in each module. This is nothing more than a superfluous addition to the Factory Method mode.
Download the sample code: FactoryMetod.rar