This week was all about design patterns and ways to implement them. We have only just begun and to start with we talked about Singleton. It is a design pattern that creates a single instance of a class to be used from a well-known access point. I had some previous interaction with this particular design pattern at my work, when we were using the C++ language this was one of the ways we got around some of the problems we had, since moving to C# it went away but still I knew about singleton before this weeks class at school. I was a little bit curious and wanted to read about it more, so I looked up blog post by Ted Neward called, surprise, surprise: “Singleton”. Link to it is here.
In this blog Ted talks about it extensively in my opinion, from the context of it, the problem that it solves and the consequences of the Singleton implementation. I definitely like the information provided and it help me expand my knowledge of the design pattern, which is a good extension on my current knowledge both from work and school. One of the parts in the blog really helped me understand it better and that is: “Reduced name space. “Singleton” is just “global” hiding behind another name. One of the explicit goals (in 1995) was to be able to have the necessary scope-wide state, but without accidentally clashing over names in that global namespace. Languages which support explicit namespacing (Java, C#, C++, Swift, yeah pretty much all of them) mean that we can have this benefit even without doing anything more than moving the global variable into one of those namespacing mechanisms.” This describes Singleton as global and I like this description because it really drove home what Singleton is and how to use it, I think….
Overall, I know I will be learning about many other design patterns and implementations but for now having this is good enough, who knows maybe others will be a better solution than Singleton, but I think it is the simplest one to learn. Neward mention couple of time the debate of Singleton vs statics and that is something interesting on its own. Something that I will research a little bit more when time allows. Until then I remember one thing my boss said about singletons back in the day: “Singletons were used everywhere, and somehow it worked, we didn’t know why, but it did.”