团结的抽象工厂

时间:2014-11-05 13:55:38

标签: c# unity-container factory-pattern

我有一个简单的抽象工厂实现:

  public abstract class ICarFactory{
    public abstract ISportsCar CreateSportCar();
    public abstract IFamilyCar CreateFamilyCar();
}

public abstract class ISportsCar {        
    public abstract void Accelerate();
}

public abstract class IFamilyCar  {
    public abstract void Accelarete();
}

public class BMWFactory : ICarFactory {
    public override ISportsCar CreateSportCar() {
        return new BMWi7();
    }

    public override IFamilyCar CreateFamilyCar() {
        return new BMWM5();
    }
}

public class WolksvagenFactory : ICarFactory {

    public override ISportsCar CreateSportCar() {
        return new WVGolfR();
    }

    public override IFamilyCar CreateFamilyCar() {
        return new WVGolf();
    }
}

public class BMWi7 : ISportsCar {
    public override void Accelerate() {
        Console.WriteLine(this.ToString() + "accelerated.");
    }
}

public class BMWM5 : IFamilyCar {

    public override void Accelarete() {
        Console.WriteLine(this.ToString() + "accelerated.");
    }
}

public class WVGolfR : ISportsCar {

    public override void Accelerate() {
        Console.WriteLine(this.ToString() + "accelerated.");
    }
}

public class WVGolf : IFamilyCar {
    public override void Accelarete() {
        Console.WriteLine(this.ToString() + "accelerated.");
    }
}

客户端代码如下:

 class Program
{


    static void Main(string[] args)
    {
        ISportsCar sportsCar = GetFactory().CreateSportCar();
        sportsCar.Accelerate();

    }

    static ICarFactory GetFactory()
    {
        string userLocation;
        userLocation = "Hamburg"; //Get user location from user settings

        if (userLocation == "Hamburg")
        {
            return new BMWFactory();
        }
        else
        {
            return new WolksvagenFactory();
        }
    }


}

是否有可能通过" GetFactory()"完成工作。使用Microsoft.Practices.Unity?

的方法

我通过搜索这个问题花了很多时间。但我找不到有用的东西。

2 个答案:

答案 0 :(得分:5)

您可以通过在每个位置注册一组特定工厂和默认工厂来实现命名注册:

_container = new UnityContainer();      
//Register location specific factories
_container.RegisterType<ICarFactory,BMWFactory>("Hamburg");
...
//Register the default factory
_container.RegisterType<ICarFactory,WolksvagenFactory>();

然后,您可以使用以下代码解析工厂(使用IsRegistered检查该位置是否有特定工厂):

static ICarFactory GetFactory()
{
    string userLocation;
    userLocation = "Hamburg"; //Get user location from user settings

    //try get specific factory for location
    if(_container.IsRegistered<ICarFactory>(userLocation)){         
        return _container.Resolve<ICarFactory>(userLocation);
    }
    //if not found, get default factory
    return _container.Resolve<ICarFactory>();   
}

查看小提琴here

答案 1 :(得分:2)

有可能。

在统一容器中注册您的CarFactories名称

_container.RegisterType<ICarFactory, BMWFactory>("Hamburg");
_container.RegisterType<ICarFactory, WolkswagenFactory>("Wolfsburg");

然后更改您的方法以使用unity容器返回工厂的实例

return _container.Resolve<ICarFactory>(userLocation);