Post by waetherman » 11 Oct 2017, 03:09
mods note: split from https://autohotkey.com/boards/viewtopic ... 7&p=175700
As I already said on Discord, that staticSingleton class is neat. I wish there was a list of OOP patterns in AHK when I started, because I was really confused at my beginnings in this language, even though I already had a firm grasp on OOP, MVC etc. in other languages. I remember that I searched for a singleton pattern quite recently, and I found a forum posts, where I think Lexikos asks why not just use a class and have static properties in it; indeed, it worked, until I realized that it was just a global variable containing an object. So how do you avoid polluting the namespace, being able to just pass the singleton around as it’s needed — using a real singleton of course! Except you still have a class, which name is polluting the namespace, so next question is can you avoid that; I never really found time and will to tinker and see if I can wrap a class definition in something, e.g. another class (which can be disposed later), so to follow this example, every module would be a separate class, which defines subclasses, which instances then can be passed between modules.
Anyways, I find it important to note that nnnik’s singleton is not a typical singleton, as it initializes on the beginning of the program (as his example shows), which should be fine most of the time, but you might find a situation, where it isn’t: e.g. when you create a library with a ton of singletons, where you want to give a user the choice which he instantiates due to performance reasons (CPU, memory); you may also want to initialize the singleton not asap, but once the most critical part of the application initialized, like the user interface, so it feels more responsive, and both of these problems are solved by the typical singleton, which creates an instance on the first constructor call.
[size=85]mods note: split from https://autohotkey.com/boards/viewtopic.php?f=7&p=175700[/size]
As I already said on Discord, that staticSingleton class is neat. I wish there was a list of OOP patterns in AHK when I started, because I was really confused at my beginnings in this language, even though I already had a firm grasp on OOP, MVC etc. in other languages. I remember that I searched for a singleton pattern quite recently, and I found a forum posts, where I think Lexikos asks why not just use a class and have static properties in it; indeed, it worked, until I realized that it was just a global variable containing an object. So how do you avoid polluting the namespace, being able to just pass the singleton around as it’s needed — using a real singleton of course! Except you still have a class, which name is polluting the namespace, so next question is can you avoid that; I never really found time and will to tinker and see if I can wrap a class definition in something, e.g. another class (which can be disposed later), so to follow this example, every module would be a separate class, which defines subclasses, which instances then can be passed between modules.
Anyways, I find it important to note that nnnik’s singleton is not a typical singleton, as it initializes on the beginning of the program (as his example shows), which should be fine most of the time, but you might find a situation, where it isn’t: e.g. when you create a library with a ton of singletons, where you want to give a user the choice which he instantiates due to performance reasons (CPU, memory); you may also want to initialize the singleton not asap, but once the most critical part of the application initialized, like the user interface, so it feels more responsive, and both of these problems are solved by the typical singleton, which creates an instance on the first constructor call.