Global instance of a class or static class with initialization method

I have a class that handles the Localization of my application. My goal is that the class is usable in the entire application so I made it static. This allows me to call the code below anywhere in my application.

Localizer.GetString(/* Key to localize */)

The method uses several fields in the Localizer class. To make sure these fields are set, a Init method was added to initialize the Localizer. If the user of my class forgets to call Init at for example the start-up of the application, exceptions will occur because the fields are not set.

One solution I'm thinking about is making the Localizer class not static, add a constructor that sets the fields and initialize the class in a global static class in my application, something like this:

public static class Globals
{
    public static Localizer Localize = new Localizer(/* Field arguments here */);
}

Now I'm not sure what the best approach is. Either

  • Static Localizer but user has to make sure Init is called before using the class.
  • Instantiated Localizer in a global static class in the application.
  • One more note: The user has no access to the source of the class.


    An alternative to what you're doing would be to use dependency injection . Dependency injection is a super fancy name for passing stuff into things instead of those things accessing that stuff directly. I know that's a vague statement - but if your class takes an argument for a field instead of creating the type itself - it's already using dependency injection.

    So, let's say you have a Localizer class. It has no static methods and there is no static instance of a localizer just being global.

    You create a Localizer instance specialized to your needs once when the app boots up:

    var localizer = new Localizer(...);
    

    Then, whenever a component needs the localizer - you pass it around

    var component = new MyComponent(localizer); // we pass the values in
    

    This makes the localizer easily modifiable, makes the classes easy to test in isolation and makes it easy to configure different components differently (what if you want the help page to always be in English all of a sudden? Or some other specific page?).

    If it's still unclear here's a nice talk by Misko Havery about not looking for things. There is also a nice Martin Fowler article about it but it's probably a bit harder to follow.

    The only tedious thing here is that you need to pass it around any time. I don't mind the explicitness but a lot of people prefer using dependency injection containers to manage the overhead.

    链接地址: http://www.djcxy.com/p/84522.html

    上一篇: 模糊QT快速文本

    下一篇: 具有初始化方法的类或静态类的全局实例