Search the Community
Showing results for tags 'getters'.
-
An example (or one variation at least) that I created in a discussion we (devs/mods) were having about protecting global variables. ; An enumeration for the getters and setters, as I hate having to write zero and one Global Enum $VERSION_WRAPPER_GET, $VERSION_WRAPPER_SET ; Unused, as I have hidden behind functions, so users who might feel the need to do this $g_iVersion = 'Haha, I''m no a number', can't! ; Global $g_iVersion = 0 ; Basically this is encapsulation and is just one of many principals found in Object Oriented Programming (OOP) ; NOTE: There is no way for me to access the "global" version variable without using the _Get/Set_Version() function(s) Example() Func Example() ; Set to 10, as only integer datatypes are allowed _Set_Version(10) ; Displays 10 ConsoleWrite('1. ' & _Get_Version() & @CRLF) ; Set to a non-digit. Such a bad user/dev! _Set_Version('Not a number') ; Displays 10, as a string was passed previously that wasn't valid ConsoleWrite('2. ' & _Get_Version() & @CRLF) ; Call another function to show that it's not just visible within this function Another_Func() ; Displays 99, which was previously set in the Another_Func() ConsoleWrite('4. ' & _Get_Version() & @CRLF) ; If the variable located in __Version_Wrapper() was purely a global variable, then anything could be assigned to it ; without proper checking. As you can see below, the variable is only assigned to when certain criteria is met ; Before someone mentions performance, you have to weigh up the pros and cons of such a design choice. If you really need data to be protected ; from your users mingling with it, then this is one of many correct approaches to encapsulate data. Otherwise if you're not concerened, then ; maybe it's not for you. You're the developer when it comes down to it EndFunc ;==>Example Func Another_Func() ; Displays 10 ConsoleWrite('3. ' & _Get_Version() & @CRLF) ; Set to 99 and return back to the previous function i.e. Example() _Set_Version(99) EndFunc ;==>Another_Func ; Getter Func _Get_Version() Return __Version_Wrapper($VERSION_WRAPPER_GET) EndFunc ;==>_Get_Version ; Setter Func _Set_Version($iVersion) Return __Version_Wrapper($VERSION_WRAPPER_SET, $iVersion) EndFunc ;==>_Set_Version ; This is a wrapper for the local static variable that will be visible only in the function and for the duration of the application's ; lifecycle ; $iType: Either $VERSION_WRAPPER_GET or $VERSION_WRAPPER_SET ; $iVersion: Used when the type is $VERSION_WRAPPER_SET Func __Version_Wrapper($iType, $iVersion = Null) ; Create a local static variable and initialise to 0 Local Static $s_iVersion = 0 Switch $iType Case $VERSION_WRAPPER_GET ; Getter Return $s_iVersion Case $VERSION_WRAPPER_SET ; Setter ; Now is the chance to check if the value meets certain criteria e.g. [0-9]+ and not a string datatype If StringRegExp($iVersion, '^\d+$') And Not IsString($iVersion) Then $s_iVersion = $iVersion EndIf EndSwitch ; Return null by default, especially if the type is as a setter Return Null EndFunc ;==>__Version_Wrapper
- 7 replies
-
- encapsulation
- getters
-
(and 3 more)
Tagged with: