带有静态注册的工厂模式

本文关键字:工厂 模式 注册 静态 | 更新日期: 2023-09-27 18:29:48

我在尝试使用以下工厂的静态构造函数注册类型时遇到了问题:

 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";
        }
    }
}

我哪里错了?这家工厂看起来运转良好,但我就是不能让克托工作。干杯

带有静态注册的工厂模式

这不是一个答案,而是一个建议。首先,当您使用泛型类时,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'esque类。但是,构造函数注入不会对静态类或子类起作用。

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();
}