Charles Pegge
09-07-2009, 13:51
Concealing Globals and Combining Incompatible Programs
Supposing you have 2 programs which you want to combine but they have some global variables and functions which share the same name but are incompatible or even do totally different things. One of these programs would have to be edited to change all the conflicting names, which is a perfect way for bugs to infest previously healthy code.
The solution is to encapsulate each program in a class of its own. No need to edit the existing code - even the globals and constants will be isolated and invisible to the other program.
To make the functions available to the ouside world, all you have to do is create methods for each class then call the functions from the methods.
Fortunatley this is an emergent ability of the o2 class system and only a few tweaks were needed to make this possible.
Oxygen Update: http://community.thinbasic.com/index.php?topic=2517
'----------------------------------------
'USING CLASSES TO ENCAPSULATe PROGRAMS
'- isolation of globals
'- exposing internal function via methods
'========================================
uses "oxygen"
dim src as string
src = "
#basic
global a=0,b=0,c=0
class MARTIAN
global a=1,b=2,c=3 'GLOBALS ISOLATED
sub showABC()
print `
MARTIAN GLOBAL
` str(a) `
` str(b) `
` str(c) `
`
end sub
method show()
showABC
end method
end class
class VENUSIAN
global a=4,b=5,c=6 'GLOBALS ISOLATED
sub showABC()
print `
VENUSIAN GLOBAL
` str(a) `
` str(b) `
` str(c) `
`
end sub
method show()
showABC
end method
end class
sub g_show
print `
EARTH GLOBAL
` str(a) `
` str(b) `
` str(c) `
`
end sub
dim as martian m
dim as venusian v
g_show
m.show
v.show
"
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
Supposing you have 2 programs which you want to combine but they have some global variables and functions which share the same name but are incompatible or even do totally different things. One of these programs would have to be edited to change all the conflicting names, which is a perfect way for bugs to infest previously healthy code.
The solution is to encapsulate each program in a class of its own. No need to edit the existing code - even the globals and constants will be isolated and invisible to the other program.
To make the functions available to the ouside world, all you have to do is create methods for each class then call the functions from the methods.
Fortunatley this is an emergent ability of the o2 class system and only a few tweaks were needed to make this possible.
Oxygen Update: http://community.thinbasic.com/index.php?topic=2517
'----------------------------------------
'USING CLASSES TO ENCAPSULATe PROGRAMS
'- isolation of globals
'- exposing internal function via methods
'========================================
uses "oxygen"
dim src as string
src = "
#basic
global a=0,b=0,c=0
class MARTIAN
global a=1,b=2,c=3 'GLOBALS ISOLATED
sub showABC()
print `
MARTIAN GLOBAL
` str(a) `
` str(b) `
` str(c) `
`
end sub
method show()
showABC
end method
end class
class VENUSIAN
global a=4,b=5,c=6 'GLOBALS ISOLATED
sub showABC()
print `
VENUSIAN GLOBAL
` str(a) `
` str(b) `
` str(c) `
`
end sub
method show()
showABC
end method
end class
sub g_show
print `
EARTH GLOBAL
` str(a) `
` str(b) `
` str(c) `
`
end sub
dim as martian m
dim as venusian v
g_show
m.show
v.show
"
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec