OOP design patterns in AHK

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
nnnik
Posts: 2789
Joined: 30 Sep 2013, 01:01
Location: Germany

OOP design patterns in AHK

11 Oct 2017, 01:49

Today I want to bring you closer to one of my most favourite topics in the world of programming: design patterns.
Design patterns are like larger bricks of code that you will encounter and repeat a lot once you start seriously programming in OOP. They have one thing in common - they tackle certain problems in a adjusteable way.
It's fine if you imagine them like large lego bricks of code that you can connect with additional code. It mainly helps you planning and then developing what you planned leading to a higher success rate and better software.

In the following topic we will collect design pattern that have been used in AHK and describe them:
Name:If there is an official name for it use the official name if there isn't just think one up
AHK only:(yes/source) tells us whether this is a thing you just found useful for ahk or if it has an external source - if yes I would like to link an external source if possible
Description:Here you should explain shortly how this design pattern works and what it does.
Use-cases:When is this design pattern useful and what kind of problem does it tackle.
Code:Provide an example code for certain design patterns and use comments to show wht you described.

This topic is just for collecting the patterns.
You can discuss them here: https://autohotkey.com/boards/viewtopic ... 17&t=38448
Recommends AHK Studio
User avatar
nnnik
Posts: 2789
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

11 Oct 2017, 01:49

Reserved for index
Recommends AHK Studio
User avatar
nnnik
Posts: 2789
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

11 Oct 2017, 02:32

Since the explination I gave doesn't exactly show what I mean I guess I will go ahead and show the first 2 patterns:


Name: Singleton
AHK only: a common pattern first introduced by the gang of four - they also wrote a famous book about design patterns you should check that out
Description: A Singleton pattern is a pattern where a class only has one instance - here it is combined with automatic initialization. It works by writing to a static variable in the constructor once and then returning it's content ins
Use-cases: This is especially useful for libraries
Code:

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



Name: automatically initialized singleton
AHK only: That's one thing that probably could be used outside of AHK
Description: A singleton pattern is a pattern where a class only has one instance - here it is combined with automatic initialization. It works by setting a certain key on startup and checking that key in it's constructor.
Use-cases: This is especially useful for libraries. Imagine you have a class Version of GDIp and it automatically calls gdipStartup. Of course this is a simple example - there are way more complex libraries out there.
Code:

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

Recommends AHK Studio
User avatar
RUNIE
Posts: 275
Joined: 03 May 2014, 14:50
GitHub: Run1e

Re: OOP design patterns in AHK

11 Oct 2017, 02:40

Name: Subclassed method
AHK-only: kinda specific to AHK syntax
Description: Creates a new temporary instance of a subclass when the subclass is called as a method via the main class.
Use-cases: Making a subclass that can be called as a method
Code:

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


See here for a bit more detail: https://autohotkey.com/boards/viewtopic.php?f=7&t=38149
Helgef
Posts: 2897
Joined: 17 Jul 2016, 01:02
Contact:

Re: OOP design patterns in AHK

12 Oct 2017, 18:28

Nice topic nnnik, and interesting ideas from all contributors. :thumbup:

I give a small example, which I find useful sometimes,

Name: Proxy / router / implicit reference object
AHK only: Wikipedia has an article Proxy pattern which seems to be relevant.
Description: In short, operate on an object through another object.
Use-cases: In ahk it is especially useful when dealing with self-refrences and meta functions. (Comment limited by my imagination)
Code: Two simple examples, first, a proxy class which only forwards method calls to a referenced object,

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


Second, a more useful example, dealing with an object which has self-references,

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


Real use example: I use a proxy for the hash table data structure, which implements __set and __get to enable the familiar array ((val:=) obj[key]:=val) syntax while avoiding the possiblity to overwrite methods, eg, obj["methodName"] := 37 stores 37 under the key methodName in the hash table, without overwriting the method.

Cheers.
Last edited by Helgef on 13 Oct 2017, 02:22, edited 1 time in total.
User avatar
nnnik
Posts: 2789
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

17 Oct 2017, 09:05

Name: super global automatically initialized Singleton
AHK-Only: Probably
Description: A Singleton that is stored in the super global variable of it's base class and initializes itself automatically - it works like the other singletons I have shown so far exept that it also overwrites its base classes super global variable.
Use-Cases: Libraries - it has the advantage over static super global Singletons that __Get, __Set, __Call and __Delete work properly.
Code:

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

Recommends AHK Studio
User avatar
RUNIE
Posts: 275
Joined: 03 May 2014, 14:50
GitHub: Run1e

Re: OOP design patterns in AHK

25 Dec 2017, 21:59

Name: Run multiple __New methods automatically in an extended class
AHK only: Yes.
Description: If you extend a class, the __New method in the base class will not run if it's overwritten in the class that extends it.
Use-cases: If you want to replicate how for example python handles inheritance.

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



Only downside is that it'll store a variable in the instance called __NewInit.
User avatar
RUNIE
Posts: 275
Joined: 03 May 2014, 14:50
GitHub: Run1e

Re: OOP design patterns in AHK

25 Dec 2017, 22:19

This is a continuation of the above, which also supports __Delete.

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

User avatar
nnnik
Posts: 2789
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: OOP design patterns in AHK

26 Dec 2017, 04:23

I think the __New is a good one.
However the __Delete one probably needs work
Recommends AHK Studio
User avatar
evilC
Posts: 4083
Joined: 27 Feb 2014, 12:30

Re: OOP design patterns in AHK

14 May 2018, 12:10

Not sure I agree with the Singleton implementations in here.
For me, a Singleton pattern in AHK would look like this (If you used a property, of course it could be done using a GetInstance() method too):

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

User avatar
evilC
Posts: 4083
Joined: 27 Feb 2014, 12:30

Re: OOP design patterns in AHK

14 May 2018, 12:36

Here is a version that you can derive from to create a singleton.
It also stops the user from trying to new one up directly.

Uses Runie's "Run multiple __New methods" technique

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


Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest