Simple OOP using Module and Struct

Share your advanced PureBasic knowledge/code with the community.
rambodash
User
User
Posts: 23
Joined: Thu Jun 13, 2013 1:00 am

Simple OOP using Module and Struct

Post by rambodash »

Using struct and modules you can do OOP easily in PB.
It's very simple to follow. Enjoy!

Code: Select all

;Class Example PureBasic

;-----START CLASS DEFINE-----
DeclareModule MyClass
  ;class variables
  Structure MyClass
    a.i
    b.i
  EndStructure
  ;class public methods
  Declare setVal(*x.MyClass, na.i, nb.i)
  Declare hello(*x.MyClass, name.s) 
EndDeclareModule

Module MyClass
  Declare printText(str.s) ;this is private
  
  ;define public class methods
  Procedure setVal(*x.MyClass, na.i, nb.i)
    *x\a = na
    *x\b = nb
  EndProcedure
  
  Procedure hello(*x.MyClass, name.s)
    printText("Hello" + name.s)
  EndProcedure
  
  ;define private class method
  Procedure printText(str.s)
    Debug str
  EndProcedure
  
EndModule
;-----END CLASS DEFINE------

;Demo Usage
;create class instance Obj1
Define.MyClass::MyClass Obj1;
;create class instance Obj2
Define.MyClass::MyClass Obj2

;call class method
MyClass::setVal(Obj1, 3, 3)
MyClass::setVal(Obj2, 4, 4)

;test
Debug "Obj1:"
Debug Obj1\a
Debug Obj1\b
Debug "Obj2:"
Debug Obj2\a
Debug Obj2\b
MyClass::hello(Obj1, "World")
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Simple OOP using Module and Struct

Post by Tenaja »

Although I have yet to work with classes or oop, it is on my bucket list.

I have wondered, ever since Modules were added to PB, why something like this has not been shared. Thank you for sharing.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Simple OOP using Module and Struct

Post by Shield »

While there is some notion of "objects" involved in this example here, it is far from object-oriented programming.
This is still plain procedural code that is working on a data structure. A very similar concept is used in PB's
object model, yet we don't call that OOP (because it isn't). :)


I wanted to point out the two most important points:

Methods are not actually part of an object.
Even though the procedures belong to a module, they are not tied to the data structure to make up the actual object.
In OOP, it is important that the object can "decide" (i.e. dispatch) by itself, which method is to be invoked based on its type
and inheritance structures with respect to overrides (i.e. virtual calls). In PB, this is only more or less practically possible
via interfaces or via the prototype approach (both of which are cumbersome to use and error-prone).

No inheritance is possible.
The fact that methods don't belong to an object makes it impossible to do proper inheritance. Yes, it is possible
to extend the data structure and pass that extended version to procedures expecting the simple version, but that
does not make it OOP because the programmer still has to call the right procedures by himself / herself.

In my opinion, inheritance is the most critical aspect of OOP that provides greater extensibility and a way of abstraction.


Regardless, still many thanks to @rambodash for this good example of how to write good and maintainable procedural code. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Simple OOP using Module and Struct

Post by heartbone »

iconic
Image
Keep it BASIC.
Post Reply