What happened to this single that I created

I created a class that allows you to access global access to a variable only by creating it once, essentially a singleton.

However, it does not correspond to any of the “correct” ways to implement a singlet. I suppose that he is not mentioned because something is “wrong” with him, but I see no problem with him, in addition, the lack of lazy initialization.

Any thoughts?

static class DefaultFields { private static readonly string IniPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "defaultFields.ini"); private static readonly IniConfigSource Ini = GetIni(); /// <summary> /// Creates a reference to the ini file on startup /// </summary> private static IniConfigSource GetIni() { // Create Ini File if it does not exist if (!File.Exists(IniPath)) { using (FileStream stream = new FileStream(IniPath, FileMode.CreateNew)) { var iniConfig = new IniConfigSource(stream); iniConfig.AddConfig("default"); iniConfig.Save(IniPath); } } var source = new IniConfigSource(IniPath); return source; } public static IConfig Get() { return Ini.Configs["default"]; } public static void Remove(string key) { Get().Remove(key); Ini.Save(); } public static void Set(string key, string value) { Get().Set(key, value ?? ""); Ini.Save(); } } 
+4
source share
8 answers

It does not match the usual singleton patterns, as your class is static and just controls access to static variables.

Where as a singleton, as a rule, there is a static single instance of the class, where the only static functions are the creation and access to the singleton, which stores the variables as ordinary non-static member variables.

The value of this class could be easily changed or made more instance more than once, but yours cannot

+5
source

You are right about a singleton, its class with a unique instance that provides global access.

This may seem like a static class, but it is usually implemented differently.

Also keep in mind that this template should be used with some caution, as it is very difficult to reorganize one single after its deep code. Should be used primarily when you have hardware limitations or unique factory access points are implemented. I will try to avoid it when possible.

Implementation Example:

 public class A { /// <summary> /// Unique instance to access to object A /// </summary> public static readonly A Singleton = new A(); /// <summary> /// private constructor so it can only be created internally. /// </summary> private A() { } /// <summary> /// Instance method B does B.. /// </summary> public void B() { } } 

And can be used as

A.Singleton.B ()

Hope this helps.

+4
source

All methods of your class are static, so you hide one instance from your users. With a singleton pattern, one instance is opened through a public property, usually called Instance (in other languages, such as Java, it can be a method called getInstance or similar).

alt text

Your code is not mistaken - it is simply not a singleton pattern. If you want to implement singleton, I would recommend the article Jon Skeet Implementing the Singleton Template in C # .

+2
source

The biggest problem that I see is that you do not make any SyncLock restrictions to write to your INI file. Several streams that try to write values ​​at the same time may turn out to have unpredictable results, for example, how to make records and only one constant (or several streams trying to write a file right away, which leads to an I / O error).

I would create a private “Lock” object and then wrap the entries in your file in SyncLock to ensure that only one thread at a time has the ability to change values ​​(or, at the very least, commit the changes to the INI file).

+2
source

This is not a very singleton, it is a static class.

In many ways, static classes are similar to singleton's, true. But static classes cannot implement interfaces, cannot inherit functionality from a base class, and you cannot carry a reference to them.

0
source

Why readonly in the Ini field?

But if you want to implement a singleton pattern, this is something like this:

 static DefaultFields { private readonly string IniPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "defaultFields.ini"); private readonly IniConfigSource Ini = GetIni(); private static DefaultFields _default; public static DefaultFields Default { get { if(this._default == null){ this._default = new DefaultFields(); } return this._default; } } private DefaultFields() { } /// <summary> /// Creates a reference to the ini file on startup /// </summary> private IniConfigSource GetIni() { // Create Ini File if it does not exist if (!File.Exists(IniPath)) { using (FileStream stream = new FileStream(IniPath, FileMode.CreateNew)) { var iniConfig = new IniConfigSource(stream); iniConfig.AddConfig("default"); iniConfig.Save(IniPath); } } var source = new IniConfigSource(IniPath); return source; } public IConfig Get() { return Ini.Configs["default"]; } public void Remove(string key) { Get().Remove(key); Ini.Save(); } public void Set(string key, string value) { Get().Set(key, value ?? ""); Ini.Save(); } } 
0
source

I am also interested in the answers to this question. In my opinion, there is a stream of monophonic examples that use lazy instantiation, but I think you should ask yourself if this is really necessary on an individual basis.

Although this article is about Java, these concepts should apply. This provides a number of examples for different singleton implementations. http://www.shaunabram.com/singleton-implementations/

I have also seen numerous references to the book Effective Java, paragraph 71 - use the lazy authority wisely. Basically, do not do this unless you need to.

0
source

lazy initialization is very important for a singleton class. By declaring your class static, you are implementing a static class, not a singleton class.

0
source

All Articles