Author Topic: ScriptBasic OOP  (Read 37998 times)

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP
« on: October 08, 2009, 09:23:36 PM »
Here is an example how you can use OOP (Object-oriented Programming) with ScriptBasic. I plan to show other aspects of OOP style programming as time permits.

Comments and suggestions welcome !

Code: [Select]
' ScriptBasic OOP Example

MODULE Customer

instance = 0

' Default property settings
_name[0] = ""
_addr1[0] = ""
_addr2[0] = ""

property{"name"} = _name[0]
property{"addr1"} = _addr1[0]
property{"addr2"} = _addr2[0]

FUNCTION GetName(object)
  GetName = property{"name"}[object]
END FUNCTION

FUNCTION SetName(object,value)
  property{"name"}[object] = value
END FUNCTION

FUNCTION GetAddr1(object)
  GetAddr1 = property{"addr1"}[object]
END FUNCTION

FUNCTION SetAddr1(object,value)
  property{"addr1"}[object] = value
END FUNCTION

FUNCTION GetAddr2(object)
  GetAddr2 = property{"addr2"}[object]
END FUNCTION

FUNCTION SetAddr2(object,value)
  property{"addr2"}[object] = value
END FUNCTION

FUNCTION New
  instance += 1
  _name[instance] = _name[0]
  _addr1[instance] = _addr1[0]
  _addr2[instance] = _addr2[0]
  new = instance
END FUNCTION

FUNCTION Destroy(object)
  _name[object] = undef
  _addr1[object] = undef
  _addr2[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'
mycust = customer::New()

customer::SetName(mycust,"John Doe")
customer::SetAddr1(mycust,"123 Main St.")
customer::SetAddr2(mycust,"Anytown, USA")

PRINT customer::GetName(mycust),"\n"
PRINT customer::GetAddr1(mycust),"\n"
PRINT customer::GetAddr2(mycust),"\n"

customer::Destroy(mycust)

C:\scriptbasic\test>custobj
John Doe
123 Main St.
Anytown, USA

C:\scriptbasic\test>

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP
« Reply #1 on: October 09, 2009, 01:17:42 PM »
I added constants for the property names so I didn't have to use quoted strings for the names of the associative array property elements. It seems SB doesn't like name as a constant so I changed it to cname.

Code: [Select]
' ScriptBasic OOP Example

MODULE Customer

CONST cname = "cname"
CONST addr1 = "addr1"
CONST addr2 = "addr2"

instance = 0

' Default property settings
_cname[0] = ""
_addr1[0] = ""
_addr2[0] = ""

property{cname} = _cname[0]
property{addr1} = _addr1[0]
property{addr2} = _addr2[0]

FUNCTION GetName(object)
  GetName = property{cname}[object]
END FUNCTION

FUNCTION SetName(object,value)
  property{cname}[object] = value
END FUNCTION

FUNCTION GetAddr1(object)
  GetAddr1 = property{addr1}[object]
END FUNCTION

FUNCTION SetAddr1(object,value)
  property{addr1}[object] = value
END FUNCTION

FUNCTION GetAddr2(object)
  GetAddr2 = property{addr2}[object]
END FUNCTION

FUNCTION SetAddr2(object,value)
  property{addr2}[object] = value
END FUNCTION

FUNCTION New
  instance += 1
  _cname[instance] = _cname[0]
  _addr1[instance] = _addr1[0]
  _addr2[instance] = _addr2[0]
  new = instance
END FUNCTION

FUNCTION Destroy(object)
  _cname[object] = undef
  _addr1[object] = undef
  _addr2[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'
mycust = customer::New()

customer::SetName(mycust,"John Doe")
customer::SetAddr1(mycust,"123 Main St.")
customer::SetAddr2(mycust,"Anytown, USA")

PRINT customer::GetName(mycust),"\n"
PRINT customer::GetAddr1(mycust),"\n"
PRINT customer::GetAddr2(mycust),"\n"

customer::Destroy(mycust)
« Last Edit: October 09, 2009, 02:15:43 PM by support »

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP - Polymorphism
« Reply #2 on: October 09, 2009, 07:04:11 PM »
Here is a shot at adding Polymorphism support to the first OOP example.

Quote
Pointers to base class

One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that brings Object Oriented Methodologies to its full potential.

Code: [Select]
' ScriptBasic OOP Example

MODULE Customer

CONST cname = "cname"
CONST addr1 = "addr1"
CONST addr2 = "addr2"

instance = 0

' Default property settings
_cname[0] = ""
_addr1[0] = ""
_addr2[0] = ""

property{cname} = _cname[0]
property{addr1} = _addr1[0]
property{addr2} = _addr2[0]

FUNCTION GetName(object)
  GetName = property{cname}[object]
END FUNCTION

FUNCTION SetName(object,value)
  property{cname}[object] = value
END FUNCTION

FUNCTION GetAddr1(object)
  GetAddr1 = property{addr1}[object]
END FUNCTION

FUNCTION SetAddr1(object,value)
  property{addr1}[object] = value
END FUNCTION

FUNCTION GetAddr2(object)
  GetAddr2 = property{addr2}[object]
END FUNCTION

FUNCTION SetAddr2(object,value)
  property{addr2}[object] = value
END FUNCTION

FUNCTION New(base)
  instance += 1
  IF base = undef THEN
    _cname[instance] = _cname[0]
    _addr1[instance] = _addr1[0]
    _addr2[instance] = _addr2[0]
  ELSE
    REF property{cname}[instance] = property{cname}[base]
    REF property{addr1}[instance] = property{addr1}[base]
    REF property{addr2}[instance] = property{addr2}[base]
  END IF
  new = instance
END FUNCTION

FUNCTION Destroy(object)
  _cname[object] = undef
  _addr1[object] = undef
  _addr2[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'
cust1 = customer::New()
customer::SetName(cust1,"John Doe")
customer::SetAddr1(cust1,"123 Main St.")
customer::SetAddr2(cust1,"Anytown, USA")

cust2 = customer::New(cust1)

PRINT customer::GetName(cust2),"\n"
PRINT customer::GetAddr1(cust2),"\n"
PRINT customer::GetAddr2(cust2),"\n"
PRINTNL

customer::SetName(cust2,"Mary Smith")

PRINT customer::GetName(cust1),"\n"

customer::Destroy(cust1)
customer::Destroy(cust2)

C:\scriptbasic\test>custobj
John Doe
123 Main St.
Anytown, USA

Mary Smith

C:\scriptbasic\test>

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP - Inheritance
« Reply #3 on: October 09, 2009, 10:16:52 PM »
I have added inheritance to the above example.

Code: [Select]
' ScriptBasic OOP Example

MODULE Customer

CONST cname = "cname"
CONST addr1 = "addr1"
CONST addr2 = "addr2"

instance = 0

' Default property settings
_cname[0] = ""
_addr1[0] = ""
_addr2[0] = ""

property{cname} = _cname[0]
property{addr1} = _addr1[0]
property{addr2} = _addr2[0]

FUNCTION GetName(object)
  GetName = property{cname}[object]
END FUNCTION

FUNCTION SetName(object,value)
  property{cname}[object] = value
END FUNCTION

FUNCTION GetAddr1(object)
  GetAddr1 = property{addr1}[object]
END FUNCTION

FUNCTION SetAddr1(object,value)
  property{addr1}[object] = value
END FUNCTION

FUNCTION GetAddr2(object)
  GetAddr2 = property{addr2}[object]
END FUNCTION

FUNCTION SetAddr2(object,value)
  property{addr2}[object] = value
END FUNCTION

FUNCTION New(base)
  instance += 1
  IF base = undef THEN
    _cname[instance] = _cname[0]
    _addr1[instance] = _addr1[0]
    _addr2[instance] = _addr2[0]
  ELSE
    REF property{cname}[instance] = property{cname}[base]
    REF property{addr1}[instance] = property{addr1}[base]
    REF property{addr2}[instance] = property{addr2}[base]
  END IF
  new = instance
END FUNCTION

FUNCTION Inherit(base)
  instance += 1
  property{cname}[instance] = property{cname}[base]
  property{addr1}[instance] = property{addr1}[base]
  property{addr2}[instance] = property{addr2}[base]
  Inherit = instance
END FUNCTION 

FUNCTION Destroy(object)
  _cname[object] = undef
  _addr1[object] = undef
  _addr2[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'
cust1 = customer::New()
customer::SetName(cust1,"John Doe")
customer::SetAddr1(cust1,"123 Main St.")
customer::SetAddr2(cust1,"Anytown, USA")

cust2 = customer::Inherit(cust1)

PRINT customer::GetName(cust2),"\n"
PRINT customer::GetAddr1(cust2),"\n"
PRINT customer::GetAddr2(cust2),"\n"
PRINTNL

customer::SetName(cust2,"Mary Smith")

PRINT customer::GetName(cust1),"\n"
PRINT customer::GetName(cust2),"\n"

customer::Destroy(cust1)
customer::Destroy(cust2)

C:\scriptbasic\test>custobj
John Doe
123 Main St.
Anytown, USA

John Doe
Mary Smith

C:\scriptbasic\test>

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP - Property List
« Reply #4 on: October 10, 2009, 01:17:14 AM »
The property list returns the property names and default values for the class.

Note: Functions GetName and SetName have been changed to GetCName and SetCName to match the property values. This change will be reflected in the examples moving forward.


Code: [Select]
' ScriptBasic OOP Example

MODULE Customer

CONST cname = "cname"
CONST addr1 = "addr1"
CONST addr2 = "addr2"

instance = 0

' Default property settings
_cname[0] = "<enter name>"
_addr1[0] = "<enter address 1>"
_addr2[0] = "<enter address 2>"

property{cname} = _cname[0]
property{addr1} = _addr1[0]
property{addr2} = _addr2[0]

FUNCTION GetCName(object)
  GetCName = property{cname}[object]
END FUNCTION

FUNCTION SetCName(object,value)
  property{cname}[object] = value
END FUNCTION

FUNCTION GetAddr1(object)
  GetAddr1 = property{addr1}[object]
END FUNCTION

FUNCTION SetAddr1(object,value)
  property{addr1}[object] = value
END FUNCTION

FUNCTION GetAddr2(object)
  GetAddr2 = property{addr2}[object]
END FUNCTION

FUNCTION SetAddr2(object,value)
  property{addr2}[object] = value
END FUNCTION

FUNCTION New(base)
  instance += 1
  IF base = undef THEN
    _cname[instance] = _cname[0]
    _addr1[instance] = _addr1[0]
    _addr2[instance] = _addr2[0]
  ELSE
    REF property{cname}[instance] = property{cname}[base]
    REF property{addr1}[instance] = property{addr1}[base]
    REF property{addr2}[instance] = property{addr2}[base]
  END IF
  new = instance
END FUNCTION

FUNCTION Inherit(base)
  instance += 1
  property{cname}[instance] = property{cname}[base]
  property{addr1}[instance] = property{addr1}[base]
  property{addr2}[instance] = property{addr2}[base]
  Inherit = instance
END FUNCTION

FUNCTION PropList
  PropList = JOIN("|",property)
END FUNCTION

FUNCTION Destroy(object)
  _cname[object] = undef
  _addr1[object] = undef
  _addr2[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'
PRINT customer::PropList()

C:\scriptbasic\test>custobj
cname|<enter name>|addr1|<enter address 1>|addr2|<enter address 2>
C:\scriptbasic\test>
« Last Edit: October 10, 2009, 01:32:57 AM by support »

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP
« Reply #5 on: October 11, 2009, 01:25:38 AM »
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

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: ScriptBasic OOP
« Reply #6 on: October 11, 2009, 01:50:41 AM »
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.

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP II
« Reply #7 on: October 11, 2009, 07:56:02 PM »
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: [Select]
' 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)

Results:
Code: [Select]
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>



Inheritance and Polymorphism examples are next up.

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP II - Inheritance
« Reply #8 on: October 11, 2009, 11:44:06 PM »
This is an example of inheritance. (see previous example) The module (CLASS) was untouched from the first OOP II example.

Code: [Select]
' 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)

C:\scriptbasic\test>oop2i
John Doe
800-THE-SHOP
0

John Doe
Mary Smith

C:\scriptbasic\test>

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP II - Polymorphism
« Reply #9 on: October 11, 2009, 11:45:01 PM »
Here the polymorphism example redone with OOP II. Once again, the module was untouched from the first OOP II example.

Code: [Select]
' 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)

C:\scriptbasic\test>oop2p
John Doe
Mary Smith

C:\scriptbasic\test>

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP - Extending Interfaces
« Reply #10 on: October 11, 2009, 11:45:54 PM »
Here is an example of extending the customer record with a shipto address block within the user code. I prefixed all module interface structures with the underscore character to specify property elements from the object interface.

Code: [Select]
' 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()

customer::_ship_to{"name"} = customer::obj[cust1]{"name"}
customer::_ship_to{"addr1"} = customer::obj[cust1]{"addr1"}
customer::_ship_to{"addr2"} = customer::obj[cust1]{"addr2"}

customer::_cust_rec{"shipto"} = customer::_ship_to
customer::obj[cust1] = customer::_cust_rec

PRINT customer::obj[cust1]{"shipto"}{"name"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr1"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr2"},NL
PRINTNL

PRINT customer::ObjDef()

customer::Destroy(cust1)

Results:
Code: [Select]
C:\scriptbasic\test>oop2ep
John Doe
123 St.
Anytown,USA

<?xml version="1.0" encoding="UTF-8"?><V><A l="0" h="11"><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>9
00-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><S>shipto</S><A l="0" h="5"><S>name</S><S>John Doe</S><S>addr1</S>
<S>123 St.</S><S>addr2</S><S>Anytown,USA</S></A></A></V>
C:\scriptbasic\test>



The next installment will show extending module methods from user code.
« Last Edit: October 12, 2009, 02:03:44 AM by support »

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP - Extending Methods
« Reply #11 on: October 12, 2009, 12:06:53 PM »
Here is my first pass at extending object methods in user code. This example shows extending the interface properties and copying the the customer main address to the new shipto address block with an extended method call. This allows calling the user extended method function in user code or by the module. This is work in progress so things might change.

This example also shows how encapsulation can be done. In a user method, multiple objects references can be encapsulated with one call to the ObjExt() method of the object.


Code: [Select]
' 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 ObjExt(fna,arg1,arg2,arg3)
  LOCAL r
  r = ICALL(fna,arg1,arg2,arg3)
END FUNCTION

FUNCTION ObjDef
  ObjDef = t::ArrayToXML(_cust_rec)
END FUNCTION

FUNCTION Destroy(object)
  obj[object] = undef
END FUNCTION

END MODULE

'
' MAIN
'

FUNCTION Copy2Ship(custobj)
  customer::obj[custobj]{"shipto"}{"name"} = customer::obj[custobj]{"name"}
  customer::obj[custobj]{"shipto"}{"addr1"} = customer::obj[custobj]{"addr1"}
  customer::obj[custobj]{"shipto"}{"addr2"} = customer::obj[custobj]{"addr2"}
END FUNCTION

updship = ADDRESS(Copy2Ship())

cust1 = customer::New()

customer::_ship_to{"name"} = ""
customer::_ship_to{"addr1"} = ""
customer::_ship_to{"addr2"} = ""

customer::_cust_rec{"shipto"} = customer::_ship_to
customer::obj[cust1] = customer::_cust_rec

customer::ObjExt(updship,cust1)

PRINT customer::obj[cust1]{"shipto"}{"name"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr1"},NL
PRINT customer::obj[cust1]{"shipto"}{"addr2"},NL
PRINTNL

customer::Destroy(cust1)

C:\scriptbasic\test>oop2me
John Doe
123 St.
Anytown,USA

C:\scriptbasic\test>
« Last Edit: October 13, 2009, 02:48:20 AM by support »

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
ScriptBasic OOP - Feedback Request
« Reply #12 on: October 13, 2009, 02:49:29 AM »
This concludes the ScriptBasic OOP framework code project/tutorial from an introduction standpoint.

If you could respond to a few questions I have then it would help me fine tune and expand on ScriptBasic OOP.

Q. Do you see an advantage of using OO style programming with your projects?

Q. Is this code project tutorial enough to get you started using ScriptBasic OOP?

Q. If you have used OOP with other languages, do you see this implementation easy to use?

Q. What do you see as missing in this implementation of OOP?

« Last Edit: October 15, 2009, 12:10:33 PM by support »