Simple factory pattern + generic

Simple factory pattern UML:

 

1. We first define the core logic of a class factory pattern: ProductFactoryClass,

2. Then abstract product categories: ProductClass1, ProductClass2

3. Interface: IProduct

 Nothing to say directly attached to the bar code:

1. When an external call:

1             SimpleFactory<Product1> product = new SimpleFactory<Product1>();
2             product.CreateShow().Product(20,200);

2. Entrance:

. 1     ///  <Summary> 
2      /// simple realization of the plant layer 
 . 3      ///  </ Summary> 
. 4      public  class SimpleFactory <T> WHERE T: new new ()
 . 5      {
 . 6          Private  static T _instance;     
 . 7          public   T CreateShow ( )
 . 8          {
 . 9              IF (_instance == null )     
 10              {
 . 11                  _instance = new new T ();   
 12 is              }
 13 is              return _instance;
14         }
15     }

3. abstract commodity:

 1     public class Product1 : IProduct
 2     {
 3         public void Product(int count, double price)
 4         {
 5             Console.WriteLine("Total:{0}", count * price);
 6             Console.ReadKey();
 7         }
 8     }
 9     public class Product2: IProduct
10     {
11         public void Product(int count, double price)
12         {
13             Console.WriteLine("Total:{0}", count * price*2);
14             Console.ReadKey();
15         }
16     }

4. Interface:

1     public interface IProduct
2     {
3         public void Product(int days, double price);
4     }

 

Guess you like

Origin www.cnblogs.com/LY-NET/p/11876782.html