C # Simple Factory mode and single-row design mode latent analysis

Source: Internet
Author: User

The Simple factory design pattern, also known as the Static Factory method, is a mode in which a factory class creates an instance of the product class based on the parameters passed in Factory.

Simple Factory mode is the simplest and most practical mode in the factory model family. The Simple factory design pattern is for objects that create classes .

In general, a simple factory involves three roles/classes:

First, factory class: Factory class is the core of a simple factory, through the factory class can be separate out of the customer needs, in the call its subclasses.

Abstract Product class: This class is the parent of the object created by the factory method pattern, typically implemented by interfaces and abstract classes.

Third, specific product categories: Factory methods Any object created by the method is an instance of this role, implemented by a specific class.

Below we use a simple factory to design a food ordering device:

  

First, define a pizza class, which is an abstract class that holds the pizza type that the customer clicked on, and the code is as follows:

1 namespace day08_0100. Simple Factory 2 {3     Public Abstract class Pizza 4     {5public          abstractstring  Info (); 6     }7 }

  

Second, in the definition of two string classes, respectively, the type of pizza, and return the value with return, the code is as follows:

1 namespaceday08_0100. Simple Factory2 {3     Public classPgpizza:pizza4     {5          Public Override stringInfo ()6         {7             return "Hint Code 1";8         }9     }Ten}
1 namespaceday08_0100. Simple Factory2 {3     Public classCheesepizza:pizza4     {5          Public Override stringInfo ()6         {7             return "Hint Code 2";8         }9     }Ten}

After that, is the most critical, is to create a factory class (Pizzafactory), which contains a static method , the return value type of the method must be abstract class ;

The factory class determines which instance of the product class is created based on the parameters passed in.

Example code:

1 namespaceday08_0100. Simple Factory2 {3     Public classpizzafactory4     {5         Public StaticPizza getinstance (stringtype)6        {7Pizza pizza=NULL;8            Switch(type)9            {Ten                 Case "Hint Code 1": OnePizza =NewPgpizza (); A                     Break; -                 Case "Hint Code 2": -Pizza =NewCheesepizza (); the                     Break; -            } -            returnPizza; -        } +     } -}

Finally, it is called in the form and the following code is written under the button control Click event:

1 Private voidBtnok_click (Objectsender, EventArgs e)2         {3             //01. Get to what type of pizza the user chooses4             if(rbopg.checked)//need to order bacon Pizza5             {6Pizza pizza=pizzafactory.getinstance (rbopg.text);7                 stringword=Pizza. Info ();8 MessageBox.Show (word);9                 //Animal animal=new Dog ();Ten             } One}

So, ordering pizza with a simple factory has been completed.

Single-row design mode

 Single-column mode: As the name implies, an object can only be initialized once;

Function: Reduce the amount of memory space, save memory overhead

Let me tell you an example:

1 class Program2     {3         4         Static voidMain (string[] args)5         {6A A1 =A ();7A A2 =A ();8Console.WriteLine (a1 = = A2);//Output False9         }  Ten}

  Because the A object opens up A1 and A2 two memory spaces in the memory heap, their memory addresses cannot be the same.

So, how can we open up a space instead of two or more? Then we need to use a single-column design pattern.
The following is the definition of Class A:
1   Public classA2     {3         Private StaticA A =NULL;//static variables are initialized when the class is loaded, and are initialized only once4     //Construction method Privatization cannot be new object5         PrivateA ()6         {7         }8          Public StaticA getsingleinstance ()9         {Ten             if(A = =NULL) One             { AA =NewA (); -             } -             returnA; the         }  -}
Run the following code:
1 class Program2     {3         4         Static voidMain (string[] args)5         {6A a1 = A.getsingleinstance ();//Same memory address7A A2 =a.getsingleinstance ();8A A3 =a.getsingleinstance ();9Console.WriteLine (a1 = = A2);//TrueTenConsole.WriteLine (A2 = = A3);//True One             A         }   -}

The construction method is set to a private method and cannot be new.

C # Simple Factory mode and single-row design mode latent analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.