OOP design patterns in AHK

Helpful script writing tricks and HowTo's
User avatar
nnnik
Posts: 2280
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: 2280
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: 2280
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: 252
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: 2404
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: 2280
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

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 1 guest