English 中文(简体)
• 如何处理单一州集聚物?
原标题:How to handle a collection of singletons?

我们有以下等级:

public interface IManager {
   object GetObject(int);
}

public class BaseManager : IManager ...

public class XManager : BaseManager {
   ...
   public static XManager Instance;
}

public class YManager : BaseManager {
   ...
   public static YManager Instance;
}

public static class ManagerFacade {
   private static IManager GetManager(type);
   public static object GetObject(type, int) { 
     return GetManager(type).GetObject(int); }
}

你们如何履行GetaManger(GetManager)的职能?

Is it possible to collect these types and their instances (or instance creating delegates) in a static dictionary in the static constructors of these classes? (Thank you Jon, I only remembered "something is different in .Net 4", but not the details)

其他方法将利用基地管理者的等级或代数,或寻求实施IManager。

什么是首选的解决办法?

最佳回答

涉及静态成员的事实使我想,一种基于独裁的方法是最佳的,除非你想援引思考,并列举复国的方法。 这是一部行之有效的样本代码。

// Added another interface - GetInstance
public interface IManager
{
    object GetObject(int i);
}

// made BaseManager abstract; you don t have to do that, but then
// remember to make everything virtual, etc etc.
public abstract class BaseManager : IManager
{
    public abstract object GetObject(int i);
}

每个儿童班级都安装固定构造器,以在我的榜样中创建单一县,这种做法过于简单化,但我相信,你有更好的办法这样做:

public class XManager : BaseManager
{
    public static XManager Instance;

    static XManager() { Instance = new XManager(); }

    public override object GetObject(int i)
    {
        return "XManager Instance: index was " + i.ToString();
    }
}

public class YManager : BaseManager
{
    public static YManager Instance;
    static XManager() { Instance = new YManager(); }

    public override object GetObject(int i)
    {
        return "YManager Instance: index was " + i.ToString();
    }
}

管理人员将采用这种方式:

public static class ManagerFacade
{
    private static readonly Dictionary<Type, IManager> managerInstances 
      = new Dictionary<Type, IManager>()
    {
        {typeof(XManager), XManager.Instance},
        {typeof(YManager), YManager.Instance}
    };

    private static IManager GetManager<T>() where T: IManager
    {
        return managerInstances[typeof(T)];
    }

    public static object GetObject<T>(int i) where T: IManager
    {
        return GetManager<T>().GetObject(i);
    }
}

The console app to test out the Manager facade:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(ManagerFacade.GetObject<XManager>(2).ToString());
        Console.WriteLine(ManagerFacade.GetObject<YManager>(4).ToString());

        // pause program execution to review results...
        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
    }
}

青春期产出:

XManager Instance: index was 2
YManager Instance: index was 4
Press enter to exit

我确信,这样做的方法比较棘手,但我只想说明如何建立和接触独裁者,以支持单一州。

问题回答

我认为,最常见的做法是使用

这样做的最佳办法取决于你预期的使用情况。 如果只有少数管理人员,而且你预计偶尔会增加人数,那么我将采取直截了当的做法:

class ManagerFacade {
  XManager getXManager() {...}
  ....
}

由此减少的错误在于确保打电话者总是得到他们期望的管理人员。 然而,这的确意味着,每增加一名管理人员时,必须更换管理人员,因此在这种情况下,这样做是不适当的。

如果你需要更经常地增加新的管理人员,则执行

class ManagerFacade {
      IManager getManager(type) {...}
      ....
    }

硬化背后的逻辑,使用大体或等同形式。 这意味着,当你增加一名经理时,你需要改写方法,但接口没有变化。

如果你们的管理人员经常或积极变化,那么就把一位独裁者留在你与适当管理人员一起居住的舞台上。 用强权作为类型,这样,在增加管理人员时,你不必改变头等。 如果需要的话,你可以 la然地开始管理。

顺便说一句,你是否确信这里需要一个单一吨? 仅仅因为你想让学校管理员总是回到XManager的同样情况,就意味着XManager需要成为一个单一州。 您可将XManager内部管理,或限制通过静态方法创建管理人员。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签