简单工厂模式就是将一个类的实例化交给一个静态工厂来执行。
中
Product:
class="code_img_closed" src="/Upload/Images/2016052105/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('dfa0bf24-8ffa-4bee-889f-c51c961ec363',event)" src="/Upload/Images/2016052105/2B1B950FA3DF188F.gif" alt="" />using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleFactory { public abstract class Product { } }View Code
CreateProduct:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleFactory { public class CreateProduct : Product { } }View Code
Factory:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleFactory { public class Factory { public static Product CreateProduct() { return new CreateProduct(); } } }View Code
Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleFactory { class Program { static void Main(string[] args) { Product product = Factory.CreateProduct(); Console.WriteLine("Created {0}", product.GetType().Name); Console.ReadLine(); } } }View Code
优点:
缺点:
使用场景: