2014-11-05 180 views
3

我有一個簡單的抽象工廠實現:抽象工廠使用Unity

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?

我已經通過搜索這個問題了很多時間。但我找不到有用的東西。

回答

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

謝謝,這真棒。 – fkucuk 2014-11-05 14:44:46

2

這是可能的。

在團結容器名稱註冊您的CarFactories

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

然後改變你的方法使用統一的容器來回報您的工廠的實例

return _container.Resolve<ICarFactory>(userLocation); 
+0

這是真棒。謝謝! – fkucuk 2014-11-05 14:44:13