OOP design patterns in AHK

Talk about anything
waetherman
Posts: 92
Joined: 05 Feb 2016, 17:00

OOP design patterns in AHK

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.
Check out my cool intrusiveCALC!
User avatar
nnnik
Posts: 2333
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

11 Oct 2017, 03:20

I adjusted the code to make that easier.
Recommends AHK Studio
User avatar
nnnik
Posts: 2333
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

12 Oct 2017, 23:32

Hmm yeah the code that is the pattern should always be above the example code.
Just curious but I don't quite understand why you need self references
Recommends AHK Studio
Helgef
Posts: 2483
Joined: 17 Jul 2016, 01:02
Contact:

Re: OOP design patterns in AHK

13 Oct 2017, 02:49

Hello. I rearranged the example.

why you need self references

Code: [Select all] [Download] GeSHi © Codebox Plus

this.that:=this

An explicit self-reference like that probably isn't a good example, in AHK we might more often deal with less explicit self-reference like circular-references. A more common example could be something like

Code: [Select all] [Download] GeSHi © Codebox Plus

tf := this.timerFn := objbindmethod(this,"method")
settimer(tf, 10)

in which case clear() in the second example would need to delete the timer and then clear the variable, this.timerFn:="", before the object could be released. A boundFunc is another example of a proxy object, it is a proxy for a func object. :wave:
User avatar
nnnik
Posts: 2333
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

13 Oct 2017, 04:59

Then why didn't you just write circular references?
I think my class indirectReference creates all sort of Proxy objects:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Recommends AHK Studio
Helgef
Posts: 2483
Joined: 17 Jul 2016, 01:02
Contact:

Re: OOP design patterns in AHK

14 Oct 2017, 17:44

Then why didn't you just write circular references?
A circular reference implies a self-reference, and the example, i.e., this.that:=this, is circular too. Making a more intricate and less explicit self-reference doesn't change the point of the example.
User avatar
nnnik
Posts: 2333
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

17 Oct 2017, 08:19

I think I will split this into a discussion and a collection part since it get's less confusing.
Recommends AHK Studio
Helgef
Posts: 2483
Joined: 17 Jul 2016, 01:02
Contact:

Re: OOP design patterns in AHK

17 Oct 2017, 14:03

nnnik wrote:Name: super global automatically initialized Singleton

I enjoy seeing these ideas :thumbup:.
A comment to those who considers doing this, remember to put the __new method below any static class variables and methods using static variables which __new uses or calls. :!:

Return to “Offtopic”

Who is online

Users browsing this forum: No registered users and 5 guests