public interface IProduct{}
public class Computer : IProduct{}
public class IPhone : IProduct{}
public class Mac : IProduct{}
public class IPad : IProduct{}
public interface IFactory
{
IProduct CreatProduct();
}
public class FactoryComputer : IFactory
{
public virtual IProduct CreatProduct()
{
return default(Computer);
}
}
public class FactoryIPhone : IFactory
{
public virtual IProduct CreatProduct()
{
return default(IPhone);
}
}
public class FactoryMac : IFactory
{
public virtual IProduct CreatProduct()
{
return default(Mac);
}
}
public class FactoryIPad : IFactory
{
public virtual IProduct CreatProduct()
{
return default(IPad);
}
}
最終使用的時候變成了這個樣子
public void ExampleFactoryMethod()
{
IFactory factory = new FactoryComputer();
IProduct computer = factory.CreatProduct();
factory = new FactoryIPhone();
IProduct iPhone = factory.CreatProduct();
factory = new FactoryMac();
IProduct mac = factory.CreatProduct();
factory = new FactoryIPad();
IProduct iPad = factory.CreatProduct();
}