I recently read some knowledge about design patterns and plan to record the learning process to further deepen my understanding.
In terms of the design pattern type, the simple factory pattern belongs to the creation pattern, also known as the staticfactory method pattern, but not one of 23 gof design patterns. In simple factory mode, a factory object determines the product type instance to create. The simple factory model is the simplest and Practical Model in the factory model family. It can be understood as a special implementation of different factory models.
I understand it as follows: there is a creation factory that can produce a variety of products, such as iPad, iPod, iPhone, iMac products (four categories ), we abstract these four products into an appleproduct class, and then inherit these three product classes from this base class (appleproduct ). In the factory class, we create a production product method and instantiate different product subclasses through polymorphism to obtain different production product types.
First, observe the UML class diagram of a simple factory:
NextCodeImplemented.
First, create a base class (appleproduct) abstracted from the general information of the product ):
/// <Summary> /// Product parent /// </Summary> Class Appleproduct { /// <Summary> /// Product Name /// </Summary> Public String Productname { Get ; Set ;} /// <Summary> /// Subclass re-Production Method /// </Summary> /// <Returns> </returns> Public Virtual String Produce (){ Return " Apple product " ;}}
Now create iPhone, iPod, iPad, iMac
1 Class IMac: appleproduct 2 { 3 Public Override String Produce () 4 { 5 This . Productname = " IMac " ; 6 7 Return " The factory production line is now in production: " + This . Productname; 8 } 9 } 10 11 Class IPad: appleproduct 12 { 13 Public Override String Produce () 14 { 15 This . Productname = " IPad " ; 16 17 Return " The factory production line is now in production: " + This . Productname; 18 } 19 } 20 21 Class IPhone: appleproduct 22 { 23 Public Override String Produce () 24 { 25 This . Productname = " IPhone " ; 26 27 Return " The factory production line is now in production: " + This . Productname; 28 } 29 } 30 31 Class IPod: appleproduct 32 { 33 Public Override String Produce () 34 { 35 This . Productname = " IPod " ; 36 37 Return " The factory production line is now in production: " + This . Productname; 38 } 39 }
Now is the most important production factory class
1 /// <Summary> 2 /// Factory 3 /// </Summary> 4 Class Applefactory 5 { 6 Public Static Appleproduct createproduct ( String Producttype) 7 { 8 Appleproduct Product = Null ; 9 Switch (Producttype) 10 { 11 Case " IPhone " : 12 Product = New IPhone (); 13 Break ; 14 Case " IPad " : 15 Product = New IPad (); 16 Break ; 17 Case " IPod " : 18 Product = New IPod (); 19 Break ; 20 Case " IMac " : 21 Product = New IMac (); 22 Break ; 23 Default : 24 Product = New IMac (); 25 Break ; 26 } 27 Return Product; 28 } 29 }
Run the code (output on the console)
1 Class Program 2 { 3 Static Void Main ( String [] ARGs) 4 { 5 Console. writeline ( " Enter the product type (iPhone, iMac, iPod, and iPad) that needs to be produced by the apple OEM) " ); 6 String Productname = Console. Readline (); 7 Appleproduct producttype = Applefactory. createproduct (productname ); 8 Console. writeline (producttype. Produce ()); 9 Console. Read (); 10 } 11 }
Running effect:
Summary:
The advantage of the simple factory model is that when we want to produce a new product, we only need to add a new product class to inherit the product base class and rewrite the production product method and call it, the factory returns instances of different objects based on different parameters (commands). Each product class has different production details and production methods. Callers do not have to pay attention to the details of production products, you only need to know the product to be produced. The disadvantage lies in the key factory categories. Every time a new product is added, the judgment logic of the factory class needs to be modified, which is not applicable to complicated production environments. At the same time, the use of the simple factory mode is not conducive to the expansion of the system, so it can only be used in some relatively simple environments.
Click Download: Sample program
Author:Sunny pig
Source:Http://www.cnblogs.com/IPrograming
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.