簡體   English   中英

具有靜態注冊的工廠模式

[英]Factory pattern with static registration

嘗試使用靜態構造函數注冊我的類型時遇到問題,使用以下工廠:

 public class Factory<T>
{
    public static Factory<T> Instance { get { return _instance; } }

    private static Factory<T> _instance = new Factory<T>();
    private Factory() { }
    static Factory() { }

    static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>();

    public void Register(string id, T obj)
    {
        if (obj.GetType().IsAbstract || obj.GetType().IsInterface)
            throw new ArgumentException("Cannot create instance of interface or abstract class");

        _registeredType.Add(id, obj.GetType());
    }

    public T Create(string id, params object[] parameters)
    {
        Type type;

        if(!_registeredType.TryGetValue(id, out type))
            throw new UnsupportedShapeException(id);

        return (T)Activator.CreateInstance(type, parameters);
    }
} 

然后如果我使用靜態構造函數進行注冊它不起作用:

    public interface IShape
{
    string print { get; }
}

public class Circle : IShape
{
    static Circle()
    {
        Factory<IShape>.Instance.Register("Circle", new Circle());
    }

    public string print
    {
        get
        {
            return "Circle";
        }
    }
}

我哪里錯了? 工廠似乎設置得很好,但我無法讓ctor工作。 干杯。

這不是答案,而是建議。 首先,當您使用泛型類時,CLR會為每個實現創建類。 這個類將有不同的靜態變量,你不能為所有類使用一個工廠。 好消息是您可以使用泛型方法而不是泛型類。 而且您甚至不需要創建T對象的實例:

public class Factory
{
    public static Factory Instance { get { return _instance; } }

    private static Factory _instance = new Factory();
    private Factory() { }

    static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>();

    public void Register<T>(string id)
    {
        var type = typeof(T);
        if (type.IsAbstract || type.IsInterface)
            throw new ArgumentException("Cannot create instance of interface or abstract class");

        _registeredType.Add(id, type);
    }

    public T Create<T>(string id, params object[] parameters)
    {
        Type type;

        if(!_registeredType.TryGetValue(id, out type))
            throw new UnsupportedShapeException(id);

        return (T) Activator.CreateInstance(type, parameters);
    }
} 

現在您可以使用Factory注冊和解析對象:

Factory.Instance.Register<Circle>("Circle");
var firstCircle = Factory.Instance.Create<Circle>("Circle");
var secondCircle = Factory.Instance.Create<IShape>("Circle");

我並不是100%確定我知道你要追求的是什么,但是,最好是制作包含工廠實例化的controller'esqe類。 但是,構造函數注入不適用於靜態類或后代。

public static class StaticFactoryClassController
{
    private static readonly IStaticFactoryService service=AppServiceFactory.Instance.Create<IStaticFactoryService>();

    public static void DoSomething() 
    {
        Service srv = new StaticFactoryClassService(service);
        srv.DoSomething(); 
    }
}

有了這個,你就可以創建一個服務類 -

public class StaticFactoryClassService
{
    private readonly IStaticFactoryService service;

    public StaticFactoryClassService(IStaticFactoryService service)
    {
        this.service = service;
    }

    public void DoSomething()
    {
       this.service.DoSomething();
    }
}

最后你的綁定界面 -

public interface IStaticFactoryService
{
    DoSomething();
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM