02.00.简单工厂模式
简单工厂模式
Simple Factory Pattern
Simple Factory模式实际上不是GoF 23个设计模式中的一员
在工厂类中创建具体对象
namespace 简单工厂模式
{
// 抽象产品
public abstract class Product
{
}
// 产品:phone
public class Phone : Product
{
}
// 产品:car
public class Car : Product
{
}
// SimpleFactory
public class SimpleFactory
{
public Product Create(string name)
{
if (name == "car")
{
return new Car();
}
if (name == "Phone")
{
return new Phone();
}
return null;
}
}
}