Page 1 of 1

Simple OOP using Module and Struct

Posted: Wed Dec 03, 2014 7:41 am
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")

Re: Simple OOP using Module and Struct

Posted: Wed Dec 03, 2014 4:50 pm
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.

Re: Simple OOP using Module and Struct

Posted: Wed Dec 03, 2014 9:06 pm
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. :)

Re: Simple OOP using Module and Struct

Posted: Wed Dec 03, 2014 9:31 pm
by heartbone
iconic
Image