Standard version vs singleton version

XMLocalizator can be used as a usual object, or as a singleton instance. Both have their pros and cons, so decide wisely which one is the best for your solution.

Standard version

If you would like to use the standard object, XMLocalizator.Localizator class is for you. Just create it:

XMLocalizator.Localizator loc = new XMLocalizator.Localizator();

And from now one you can subscribe elements, load localization files and everything that's in Using Localizator section.

Remember that you need to pass the reference to this instance to every object that is going to use Localizator. There are plenty of ways to do such a thing, but suppose we want to localize a window. As mentioned, we need to pass the XMLocalizator.Localizator reference to it somehow, for example via constructor:

SomeWindow win = new SomeWindow(loc);

where the interrior of the SomeWindow class might look similar to

class SomeWindow : SomeGuiMasterClass, ILocalizatorSubscriber
{
     public SomeWindow(XMLocalizator.Localizator localizator)
     {
          loc = localizator;
          loc.Subscribe(this);
          Localize();

          // other operations...         
     }

     // other methods...

     public void Localize()
     {
          this.SomeLabel.Text = loc["Some phrase"];
     }

     // other variables...

     private XMLocalizator.Localizator loc;
}

The standard version is a little bit uncomfortable. Use it only if:
  • using singletons in your project is banned
  • you are against singletons and your application is in the early state of development
  • you want to localize one part of the application in the different language that the other (just play with two Localizator objects...)
  • you are about to implement the Dependency Injection dessign pattern on your own

Singleton version

For your convenience, XMLocalizator contains a singleton version of it's main class - Localizator. To obtain the instance, access the static Instance property of the XMLocalizator.Singleton.LocalizatorSingleton class.

XMLocalizator.Singleton.LocalizatorSingleton loc = XMLocalizator.Singleton.LocalizatorSingleton.Instance;

Needless to say, it's much easier to use it, because XMLocalizator.Singleton.LocalizatorSingleton.Instance lives globally and operating on it's instance in one place makes the changes applied in all other.

And similarly:

class SomeWindow : SomeGuiMasterClass, ILocalizatorSubscriber
{
     public SomeWindow()
     {
          loc.Subscribe(this);
          Localize();

          // other operations...         
     }

     // other methods...

     public void Localize()
     {
          this.SomeLabel.Text = loc["Some phrase"];
     }

     // other variables...

     private XMLocalizator.Singleton.LocalizatorSingleton loc = XMLocalizator.Singleton.LocalizatorSingleton.Instance;
}
As you can see in this example, adding XMLocalizator support into class doesn't interfere with any of the external declaration. In fact, the outside world has a little clue about SomeWindow using XMLocalizator. Just:
  1. Implement ILocalizatorSubscriber (click here to read about The XMLocalizator Design Pattern)
  2. Get the instance of the singleton
  3. Subscribe the class to XMLocalizator

The singleton version is designed as a thread-safe, but you might meet some problems in large applications with, for example, massive testing backup. Use it only if:
  • you can afford having one more singleton in your project
  • you designed your application as a small project
  • you would like to make your existing application multilingual, but organizing passing the reference to every single object that need to be localized scares you
  • your application is relatively not complex enough if we are taking into account multithreading, unit tests, scalability, etc.

Last edited Mar 21, 2011 at 9:56 AM by bartoszlenar, version 6

Comments

No comments yet.