Make subclasses act like methods

Post a reply

Confirmation code
Enter the code exactly as it appears. All letters are case insensitive.
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review

Expand view Topic review: Make subclasses act like methods

Make subclasses act like methods

Post by RUNIE » 11 Oct 2017, 01:23

One of the usecases for this is if you want to have a method but the method ends up being large and messy, so you would rather split up the method code but still be able to call it as a method.

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

Here, MyClass.SubClass(Params) would create a new instance of MyClass.SubClass and call the .Call() method in that instance. When execution is done, the last reference to the instance is released.

This implies that a new instance of MyClass.SubClass is created for each 'method' call, so inside the SubClass you can do whatever you want with the instance without worrying about messing it up for later calls.

The code can be thought of as a more complicated version of this:

Code: [Select all]GeSHi © Codebox Plus

MyClass.SubClass("Test", "Second", 3)

Class MyClass {
SubClass(Param*) {
for Index, Param in Params
ParamText .= "`n" Param
msgbox % SubStr(ParamText, 2)

Also I learnt about this design pattern from COCO's JSON encoder/decoder: ... r/JSON.ahk