Tutorial: PureBasic and the OOP

Developed or developing a new product in PureBasic? Tell the world about it.
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Tutorial: PureBasic and the OOP

Post by Dräc »

Hi everybody!

Because a tutorial doesn’t exist to clarifies this purpose in PureBasic, find here a tutorial dealing with OOP implementation in PureBasic

Tutorial: PureBasic and the OOP

This tutorial does not explain the reason to be of the object-oriented programming but simply how we can implement it in a language as PureBasic.
If the tutorial resumes the definitions of the OO concepts, it is to be in agreement with the reader on the concept definitions that will be employed.

Nevertheless let me try to suggest in some lines the interest of OO for those who wish it:
For me, the object concept brings a concrete answer to the developer in the way of conceptualize its code.

To conceptualize, understand that we group the alike elements, to work at first on the common similarity (object classes) and postpone as far as possible the differences (objects themselves).

This approach suggests coding only the minimum because we limit the redundancy.

A classic example to illustrate:
We can describe a rectangle and a circle in an independent way and thus create so many different codes.
Nevertheless both are forms: thus they have some similarities.
The purpose of the object-oriented approach is to bring the developer to implement only once the similarity. The advantages are normally: fewer codes, more light, a code more strong in front of modifications …
This example is simple and, of course, procedural approach also allows to reduce a such redundancy.
Simply the object-oriented programming suggests structuring its procedural programming to have a better answer to this requirement.
Every programmer tries to simplify his codes and the object-oriented is a technique to achieve this goal.
But to completely feel the advantage of the object representation, I can only advise you to learn also about the design patterns.
These last ones do not deal as a language in particular (but it must however be object-oriented). They process common programming problems and give an adapted solution in term of limiting redundancies.
To my taste, they give all the sense to the OOP.

But don’t imagine that OOP has got only advantages. Some running performances are lost because of many memory allocations and functions call.


Well! I hope simply that my english translation will be good enough and made the tutorial also pleasant and profitable for you in the reading, than it was for me in its writing…
Last edited by Dräc on Fri Sep 21, 2007 11:22 am, edited 1 time in total.
User avatar
GedB
Addict
Addict
Posts: 1312
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Dräc,

Those pages are beautiful. The Articles are well written, too.

It must have taken you ages!
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Initially I was about to write: "Where's the tutorial?!"

Now I just saw the big letters are supposed to be a link... :roll:


I didn't read your pages in detail but it looks like you did some really
great work there! Well done layout, too. :) Very cool!
Good programmers don't comment their code. It was hard to write, should be hard to read.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Nice work!!!! *gets reading glasses on* :D
--Kale

Image
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Good work! In my opinion, this should be packaged as a PDF and bundled with every PB download!

Thanks!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
okasvi
Enthusiast
Enthusiast
Posts: 150
Joined: Wed Apr 27, 2005 9:41 pm
Location: Finland

Post by okasvi »

Thanks! This is one of tutorials ive been missing... :D
Fred
Administrator
Administrator
Posts: 16618
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

This is a very good paper ! (BTW, some french words are still left: constructeur, destructeur, also POO (for Programmation Orienté Objet - don't get me wrong :wink:) in the main title should be changed to OOP).
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

Wow! will try to read all of this...
- This is a great contribution.
Ta - N
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

This is great, i've never really understood OOP but it looks like if i read your site, i might ;)

Very good job :)
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Post by Psychophanta »

Dräc, THANKS a lot :D
I enjoy it
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Leo
User
User
Posts: 21
Joined: Sat Aug 02, 2003 8:48 pm
Location: Netherlands, Wijk bij Duurstede

Post by Leo »

Very nice work. I tried OOP myself with instances in linkedlist, but your solution is more sophisticated. I altered your example a little bit with my ideas, perhaps you could improve your code with it.

Greetings

Leo

Code: Select all

; POO_Heritage.pb [http://drac.site.chez.tiscali.fr/Tutorials Programming PureBasic/indexTutorials.htm#POO]
; Dräc - 2005
;
; Cet exemple montre comment une Classe concrète (Rect1) hérite d'une Classe abstraite (Form)
; Elle montre aussi comment accéder aux attributs d'un objet: soit par des méthodes, soit par un pointeur sur l'objet.
;
; This example shows how a concrete Class (Rect1) inherits from an abstract Class (Form)
; It shows also how to access  to object attributes: either by Methods, or by pointer on the object.


Interface Form
  Draw()
  Cut()
  Get_var1()
  Get_var2()
EndInterface

Structure Form_
  *Methods.l
  var1.l
  var2.l
EndStructure

Procedure Draw_Form(*this.Form_)
  Debug "Draw_Form "
EndProcedure

Procedure Cut_Form(*this.Form_)
  Debug "Cut_Form"
EndProcedure

Procedure Get_var2_Form(*this.Form_)
  ProcedureReturn *this\var2
EndProcedure


Structure Methods_Form
  Draw.l
  Cut.l
  Get_var1.l
  Get_var2.l
EndStructure

Procedure Init_Methods_Form(Ptr_Methods.l)
  *Methods.Methods_Form=Ptr_Methods
  *Methods\Draw=@Draw_Form()
  *Methods\Cut=@Cut_Form()
  *Methods\Get_var2=@Get_var2_Form()
EndProcedure
  
Methods_Form.Methods_Form
Init_Methods_Form(@Methods_Form)

; Ici la méthode Get_var1() n'est pas implémentée: la Classe Form est une Classe Abstraite
; On n'a donc pas besoin de déclarer de Constructeur ni de Destructeur de cette Classe
; Here the method Get_var1() is not implemented: the Class Form is an Abstract Class
; Thus we don't need to declare a Constructor or a Destructor of this Class

Procedure Init_New_Form(*this.Form_, var1.l, var2.l)
  *this\var1=var1
  *this\var2=var2
EndProcedure  

Procedure.l New_Form(var1.l, var2.l)
  Shared Methods_Form
  *this.Form_ = AllocateMemory(SizeOf(Form_))
  *this\Methods=@Methods_Form
  Init_New_Form(*this, var1, var2)
  ProcedureReturn *this
EndProcedure

Procedure Free_Form(*this)
  FreeMemory(*this)
EndProcedure

; ----------------------------------

Interface Rect1 Extends Form
  Erase()
  Get_var4()
EndInterface

Structure Rect1_ Extends Form_
  var3.l
  var4.l
  rectname.s
EndStructure

Procedure Draw_Rect1(*this.Rect1_)
  Debug "Draw_Rect1 "+ *this\rectname
EndProcedure

Procedure Erase_Rect1(*this.Rect1_)
  Debug "Erase_Rect1"
EndProcedure

Procedure Get_var1_Rect1(*this.Rect1_)
  ProcedureReturn *this\var1
EndProcedure

Procedure Get_var4_Rect1(*this.Rect1_)
  ProcedureReturn *this\var4
EndProcedure

Structure Methods_Rect1 Extends Methods_Form
  Erase.l
  Get_var4.l
EndStructure

Procedure Init_Methods_Rect1(Ptr_Methods.l)
  Init_Methods_Form(Ptr_Methods)
  *Methods.Methods_Rect1=Ptr_Methods
  *Methods\Draw=@Draw_Rect1()
  *Methods\Erase=@Erase_Rect1()
  *Methods\Get_var1=@Get_var1_Rect1(); La Classe concrète Rect1 se charge de donner l'implémentation de Get_var1()
  *Methods\Get_var4=@Get_var4_Rect1(); The concrete Class Rect1 takes care to give the implementation of Get_var1() method
EndProcedure
  

Methods_Rect1.Methods_Rect1
Init_Methods_Rect1(@Methods_Rect1)

Procedure Init_New_Rect1(*this.Rect1_, var1.l, var2.l, name.s, var4.l)
  Init_New_Form(*this, var1,var2)
  *this\rectname=name
  *this\var4=var4
EndProcedure  

Procedure.l New_Rect1(var1.l, var2.l, name.s, var4.l)
  Shared Methods_Rect1
  *this.Rect1_ = AllocateMemory(SizeOf(Rect1_))
  Debug *this
  *this\Methods=@Methods_Rect1
  Init_New_Rect1(*this, var1, var2, name, var4)
  Debug *this
  ProcedureReturn *this
EndProcedure

Procedure Free_Rect1(*this)
  FreeMemory(*this)
EndProcedure
; ----------------------------------

Rect.Rect1 = New_Rect1(1, 2, "leo",6)
Rect2.Rect1 = New_Rect1(3, 4, "hannie",7)
Form2.Form = New_Form(8,9)

Debug ">> Method Test"
Rect\Draw()
Rect\Cut()
Rect\Erase()

Rect2\Draw()

Form2\Draw()
Form2\Cut()

Debug""
Debug ">> Access Test"
*Rect.Rect1_= Rect
Debug *Rect\var1
Debug Rect\Get_var1()
Debug *Rect\var4
Debug Rect\Get_var4()
Debug *Rect\rectname
*Rect2.Rect1_= Rect2
Debug *Rect2\var1
Debug Rect2\Get_var1()
Debug *Rect2\var4
Debug Rect2\Get_var4()
Debug *Rect2\rectname
*Form2.Form_=Form2
Debug *Form2\var2
Debug Form2\Get_var2()

Debug""
Debug ">> Destruction Test"
Free_Rect1(Rect)
Free_Rect1(Rect2)
Free_Form(Form2)
;Rect\Draw(); --> Impossible car l'objet Rect n'existe plus! / Impossible because Rect objet doesn't exist!

Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

Thanks a lot to everybody.

@Leo: Good improvement, leading to a more homogeneous code.
I will up-date the tutorial in this way… (including certainly an explicit representation of the constructor variable arguments which I had pushed aside until now.)

Precisely concerning linkedlist, the tutorial section called “Allocation” is planned to deal with them soon, just because it is another mean to perform instanciation
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

Leo,
Just a little remark on your example,
Rigorously, an object from the class Form should not be created because it is an abstract class.
Be careful that Form2/Get_var1() is not defines…
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Awesome.

Thank you!

dell_jockey wrote:Good work! In my opinion, this should be packaged as a PDF and bundled with every PB download!
Agreed. Or chm'd ordoc'ed or something and zipped.

Very nice indeed!
@}--`--,-- A rose by any other name ..
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i'm not a native english speaker so i tend to stumble over some of the phrasing... is it just me?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply