Objective
Design patterns of the article a lot, my humble writing is not very good. Write this only for the consolidation of basic knowledge, in case accidentally help you, is my great honor! Write bad welcome code friends Correct, nonsense end began to get to the point.
Introduction-Simple Factory mode
Official definition: (awkward ~ Seems to have no official definition, the following excerpt from Baidu Encyclopedia) Simple Factory mode is the creation mode, also called Static Factory method (static Factory mode) pattern, but does not belong to one of the 23 gof design patterns. A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.
Popular definition: a factory (Class) is used to create objects.
Realize
Example: If you want to find a certain treasure on a custom-made shoes manufacturers, it is summer you want to order a pair of your own skateboard shoes (yeah!, friction friction ...). After six months to the winter, you want to order a pair of shoes.
Class Diagram:
Code:
/// <summary> ///Shoes/// </summary> Public Abstract classShoes { Public Abstract stringName {Get; } } /// <summary> ///Sandals/// </summary> Public classSandal:shoes { Public Override stringName {Get { return "Sandals"; } } } /// <summary> ///Shoes/// </summary> Public classCottonpaddedshoes:shoes { Public Override stringName {Get { return "Shoes"; } } } /// <summary> ///one Amoy Shoes factory/// </summary> Public classShoesfactory { Public StaticShoes Create (stringShoestype) { if(Shoestype = ="Sandals") { return Newsandal (); } Else if(Shoestype = ="Shoes") { return Newcottonpaddedshoes (); } Throw NewException (string. Format ("our factory does not produce {0}", Shoestype)); } }
Call:
classProgram {Static voidMain (string[] args) { //I want Sandals .Console.WriteLine ("I want Sandals ."); Console.WriteLine ("Making sandals ..."); //first pair of shoesShoes shoes1 = Shoesfactory.create ("Sandals"); Console.WriteLine ("shoe production finished!"); //look what kind of shoes .Console.WriteLine ("the name of the shoe is: {0}", Shoes1. Name); Console.WriteLine ("Production Shoes ..."); //second pair of shoesShoes Shoes2 = Shoesfactory.create ("Shoes"); Console.WriteLine ("shoe production finished!"); //look what kind of shoes .Console.WriteLine ("the name of the shoe is: {0}", Shoes2. Name); Console.read (); } }
Results:
Pros : In the example above, we need shoes to tell the factory directly, do not care about how the shoes are made out of what type of. The client is not directly dependent on the specific object and is more flexible.
Cons: or continue to the above example, what if I want a pair of skateboard shoes now? If the factory wants to do it, it has to modify the factory class, if I want a pair of football shoes, a pair of basketball shoes, a pair of running shoes and so on. Then this factory class will become unusually complex and large and difficult to maintain.
Welcome to criticize correct, reprint please indicate source http://www.cnblogs.com/xinwang/p/6308622.html
C # Design Pattern Consolidation-Simple Factory mode