ScriptBasic > Tutorials
ScriptBasic OOP
Support:
I have been working on extending an interface and realized I had my object / property precedence backwards.
cust1 = customer::New()
PRINT customer::obj[cust1]{"name"}
PRINT customer::obj[cust1]{"addr1"}
PRINT customer::obj[cust1]{"addr2"}
PRINT customer::obj[cust1]{"phone"}{"office"}
PRINT customer::obj[cust1]{"phone"}{"cell"}
customer = CLASS
cust1 = OBJECT
phone = PROPERTY
office = extended property of phone.
FYI Notice that the property names are constants in the above example. You could use a variable for the property name and make your object usage more dynamic.
PRINT customer::ObjDef(cust1)
I have decided to dump the object definition out to a XML string. This will give the user a representation of the object's structure along with type and default value of each property.
Support:
This code project was created to provide an OOP style framework that ScriptBasic users can include in their projects. The following are some of the goals of this project.
[*]Object style programming option for ScriptBasic (would make embedding SB easier if scripts were object based like the API)
[*]Extend properties of an objects base class without have to touch the modules code
[*]Extend methods in user code passing the address of the user methods(functions/subs) to the module to use with extended properties or extending the modules functionality from the base class definition.
[*]Inheritance support.
[*]Polymorphism support.
[*]Encapsulation support. (objects base properties and/or methods)
[*]Object definition to XML.
[/list]
Support:
This is the first example using the object/property in the right precedence order. This example is displaying the default property values for the new instance of the customer object.
--- Code: ---' ScriptBasic OOP Example
GLOBAL CONST NL = "\n"
MODULE Customer
IMPORT t.bas
instance = 0
phone_num{"shop"} = "800-THE-SHOP"
phone_num{"cell"} = "900-OUR-CELL"
age_bal{"current"} = 0
age_bal{"30"} = 0
age_bal{"60"} = 0
age_bal{"90"} = 0
age_bal{"overdue"} = 0
cust_rec{"name"} = "John Doe"
cust_rec{"addr1"} = "123 St."
cust_rec{"addr2"} = "Anytown,USA"
cust_rec{"phone"} = phone_num
cust_rec{"aging"} = age_bal
obj[0] = cust_rec
FUNCTION New(base)
instance += 1
IF base = undef THEN
obj[instance] = obj[0]
ELSE
REF obj[instance] = obj[base]
END IF
new = instance
END FUNCTION
FUNCTION Inherit(base)
instance += 1
obj[instance] = obj[base]
Inherit = instance
END FUNCTION
FUNCTION ObjDef
ObjDef = t::ArrayToXML(cust_rec)
END FUNCTION
FUNCTION Destroy(object)
obj[object] = undef
END FUNCTION
END MODULE
'
' MAIN
'
this_cust = customer::New()
PRINT customer::obj[this_cust]{"name"},NL
PRINT customer::obj[this_cust]{"addr1"},NL
PRINT customer::obj[this_cust]{"addr2"},NL
PRINT customer::obj[this_cust]{"phone"}{"shop"},NL
PRINT customer::obj[this_cust]{"phone"}{"cell"},NL
PRINT customer::obj[this_cust]{"aging"}{"current"},NL
PRINT customer::obj[this_cust]{"aging"}{"30"},NL
PRINT customer::obj[this_cust]{"aging"}{"60"},NL
PRINT customer::obj[this_cust]{"aging"}{"90"},NL
PRINT customer::obj[this_cust]{"aging"}{"overdue"},NL
PRINT NL
PRINT customer::ObjDef(),NL
customer::Destroy(this_cust)
--- End code ---
Results:
--- Code: ---C:\scriptbasic\test>oop2
John Doe
123 St.
Anytown,USA
800-THE-SHOP
900-OUR-CELL
0
0
0
0
0
<?xml version="1.0" encoding="UTF-8"?><V><A l="0" h="9"><S>name</S><S>John Doe</S><S>addr1</S><S>123 St.</S><S>addr2</S><S>Anytown,USA</S><S>phone</S><A l="0" h="3"><S>shop</S><S>800-THE-SHOP</S><S>cell</S><S>90
0-OUR-CELL</S></A><S>aging</S><A l="0" h="9"><S>current</S><I>0</I><S>30</S><I>0</I><S>60</S><I>0</I><S>90</S><I>0</I><S>overdue</S><I>0</I></A></A></V>
C:\scriptbasic\test>
--- End code ---
Inheritance and Polymorphism examples are next up.
Support:
This is an example of inheritance. (see previous example) The module (CLASS) was untouched from the first OOP II example.
--- Code: ---' ScriptBasic OOP Example
GLOBAL CONST NL = "\n"
MODULE Customer
IMPORT t.bas
instance = 0
phone_num{"shop"} = "800-THE-SHOP"
phone_num{"cell"} = "900-OUR-CELL"
age_bal{"current"} = 0
age_bal{"30"} = 0
age_bal{"60"} = 0
age_bal{"90"} = 0
age_bal{"overdue"} = 0
cust_rec{"name"} = "John Doe"
cust_rec{"addr1"} = "123 St."
cust_rec{"addr2"} = "Anytown,USA"
cust_rec{"phone"} = phone_num
cust_rec{"aging"} = age_bal
obj[0] = cust_rec
FUNCTION New(base)
instance += 1
IF base = undef THEN
obj[instance] = obj[0]
ELSE
REF obj[instance] = obj[base]
END IF
new = instance
END FUNCTION
FUNCTION Inherit(base)
instance += 1
obj[instance] = obj[base]
Inherit = instance
END FUNCTION
FUNCTION ObjDef
ObjDef = t::ArrayToXML(cust_rec)
END FUNCTION
FUNCTION Destroy(object)
obj[object] = undef
END FUNCTION
END MODULE
'
' MAIN
'
cust1 = customer::New()
cust2 = customer::Inherit(cust1)
PRINT customer::obj[cust2]{"name"},NL
PRINT customer::obj[cust2]{"phone"}{"shop"},NL
PRINT customer::obj[cust2]{"aging"}{"current"},NL
PRINTNL
customer::obj[cust2]{"name"} = "Mary Smith"
PRINT customer::obj[cust1]{"name"},NL
PRINT customer::obj[cust2]{"name"},NL
customer::Destroy(cust1)
customer::Destroy(cust2)
--- End code ---
C:\scriptbasic\test>oop2i
John Doe
800-THE-SHOP
0
John Doe
Mary Smith
C:\scriptbasic\test>
Support:
Here the polymorphism example redone with OOP II. Once again, the module was untouched from the first OOP II example.
--- Code: ---' ScriptBasic OOP Example
GLOBAL CONST NL = "\n"
MODULE Customer
IMPORT t.bas
instance = 0
phone_num{"shop"} = "800-THE-SHOP"
phone_num{"cell"} = "900-OUR-CELL"
age_bal{"current"} = 0
age_bal{"30"} = 0
age_bal{"60"} = 0
age_bal{"90"} = 0
age_bal{"overdue"} = 0
cust_rec{"name"} = "John Doe"
cust_rec{"addr1"} = "123 St."
cust_rec{"addr2"} = "Anytown,USA"
cust_rec{"phone"} = phone_num
cust_rec{"aging"} = age_bal
obj[0] = cust_rec
FUNCTION New(base)
instance += 1
IF base = undef THEN
obj[instance] = obj[0]
ELSE
REF obj[instance] = obj[base]
END IF
new = instance
END FUNCTION
FUNCTION Inherit(base)
instance += 1
obj[instance] = obj[base]
Inherit = instance
END FUNCTION
FUNCTION ObjDef
ObjDef = t::ArrayToXML(cust_rec)
END FUNCTION
FUNCTION Destroy(object)
obj[object] = undef
END FUNCTION
END MODULE
'
' MAIN
'
cust1 = customer::New()
cust2 = customer::New(cust1)
PRINT customer::obj[cust2]{"name"},NL
customer::obj[cust2]{"name"} = "Mary Smith"
PRINT customer::obj[cust1]{"name"},NL
customer::Destroy(cust1)
customer::Destroy(cust2)
--- End code ---
C:\scriptbasic\test>oop2p
John Doe
Mary Smith
C:\scriptbasic\test>
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version