1. Definition
Define an interface for creating a object,but let subclasses decide which class to instantiate. Factory method let a class defer instantiation to subclasses. (Define an interface for creating objects so that subclasses decide which class to instantiate.) The factory method defers the instantiation of a class to its subclasses. )
2. Common source Code
/// <summary> ///Abstract Product class/// </summary> Public Abstract classProduct {//Abstract Methods Public Abstract voidMethod1 (); } /// <summary> ///Specific Product Categories/// </summary> Public classconcreteproduct1:product { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("ConcreteProduct1"); } } /// <summary> ///Specific Product Categories/// </summary> Public classconcreteproduct2:product { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("ConcreteProduct2"); } }
/// <summary> ///Abstract Factory class/// </summary> Public Abstract classCreator { Public AbstractProduct createproduct (type type); } /// <summary> ///Specific factory class/// </summary> Public classConcretecreator:creator { Public OverrideProduct createproduct (type type) {returnActivator.CreateInstance (Type) asProduct; } }
3. Expansion of the factory model
(1) reduced to simple Factory mode
Let's consider a problem: A module requires only one factory class, and there is no need to generate it, using a static method. For the above common source code, only need to remove the abstract factory class, while the createproduct method is set to static method.
/// <summary> /// specific factory class /// </summary> Public class Creator { publicstatic Product createproduct (type type) { Return as Product; } }
This pattern is a weakening of the factory method pattern because it is simple, so called the Simple Factory mode (simply Factory pattern), also called the Static Factory mode.
(2) Upgrade to multiple factory classes
When we are working on a more complex project, we often encounter an effort to initialize an object, and all product classes are initialized in a factory method to make the code structure unclear. For example, a product class has 5 implementations, and each implementation class has a different initialization method, which, if written in a factory method, will inevitably result in the method being extremely large.
Given the need for structural clarity, we define a creator for each product, and then the caller chooses which factory method to associate with.
/// <summary> ///Abstract class/// </summary> Public Abstract classHuman {//Abstract Methods Public Abstract voidMethod1 (); } /// <summary> ///Specific Class/// </summary> Public classBlackhuman:human { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("Blackhuman"); } } /// <summary> ///Specific Class/// </summary> Public classWhitehuman:human { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("Whitehuman"); } }
/// <summary> ///Abstract Factory class/// </summary> Public Abstract classHumanfactory { Public AbstractHuman Createhuman (); } /// <summary> ///Specific factory class/// </summary> Public classBlackhumanfactory:humanfactory { Public OverrideHuman Createhuman () {return NewBlackhuman (); } } /// <summary> ///Specific factory class/// </summary> Public classWhitehumanfactory:humanfactory { Public OverrideHuman Createhuman () {return NewWhitehuman (); } }
/// <summary> /// Scene class /// </summary> class program { staticvoid Main (string[] args) { = (new whitehumanfactory ()). Createhuman (); Whitehuman.method1 (); Console.readkey (); } }
3. Deferred initialization
Define a map container that holds all the resulting objects, and if they already exist in the map window, take them back directly, and if not, produce an object based on the desired type and put it into the map window for the next call.
2. Factory method Mode