创建一组相关的产品。
1)Abstract Factory Class:创建一组产品的接口
2)Concrete Factory:继承Abstract Factory;实际过程中一个具体的工厂只需要创建一次,考虑Singleton
3)IProduct:一类产品的接口
4)Concrete Product:继承Iproduct。
5)Client:只包含AbstractFactory和IProduct的引用,不知道ConcreteFactory和ConcreteProduct的存在。
public abstract class AbstractFactory
{
public abstract IProduct1 CreateProduct1();
public abstract IProduct2 CreateProduct2();
}
interface IProduct1 {string GetName();}
public class Product1 : IProduct1 {public string GetName() {return "this is product 1";}}
interface IProduct2 {string GetName();}
public class Product2 : IProduct1 {public string GetName() {return "this is product 2";}}
public sealed class ConcreteFactory : AbstractFactory
{
private static ConcreteFactory _instance = new ConcreteFactory();
public ConcreteFactory GetInstance()
{
if (_instance == null)
_instance = new ConcreteFactory();
return _instance;
}
private ConceteFactory(){}
public override IProduct1 CreateProduct1() { return new Product1();}
public override IProduct2 CreateProduct2() { return new Product2();}
}