Page 1 of 1

OOP abstract class

Posted: Fri Dec 08, 2017 12:08 pm
by microdevweb
Hi guys,

I like very well the "oop" concept, but Pb don't include this functionality.

For making a similar "oop" I don't use the macro, because the "ide" doesn't work very well with the structure obtained by it.

I propose to you a little drawing software, designed on tree layers (model,view,controller).

This software doesn't work really, I just make it for you and showing an example.

But it's explain just the use of an abstract class.

Image

:arrow: DOWLOAD

Abstract class sample

Code: Select all

; ****************************************************************************************************
; PROJECT       : TUTO_POO
; PACKAGE       : TUTO_POO.model
; DESIGNED WITH : PB 5.60
; NAME          : Form ABSTRACT CLASS
; VERSION       : 1
; DATE          :
; AUTHOR        : microdevWeb
; DESCRIPTION   :
; *****************************************************************************************************
DeclareModule Form
  Structure _struct
    *methods
    x.l
    y.l
    selected.b
  EndStructure
  Enumeration 
    #RECTANGLE
    #CIRCLE
  EndEnumeration
  Declare super(x,y,*this._struct,*methods)
EndDeclareModule

Module Form
  ;-* GETTERS
  Procedure getPosX (*this._struct)
    With *this
      ProcedureReturn \x
    EndWith
  EndProcedure
  
  Procedure getPosY (*this._struct)
    With *this
      ProcedureReturn \Y
    EndWith
  EndProcedure
  
  Procedure isSelected (*this._struct)
    With *this
      ProcedureReturn \selected
    EndWith
  EndProcedure
  ;}
  ;-* SETTERS
  Procedure setPosX (*this._struct,posX)
    With *this
      \x = posX
      ProcedureReturn #True
    EndWith
  EndProcedure
  
  Procedure setPosY (*this._struct,posY)
    With *this
      \y = posY
      ProcedureReturn #True
    EndWith
  EndProcedure
  
  Procedure setSelected (*this._struct,state.b)
      With *this
        \selected = state
        ProcedureReturn #True
      EndWith
    EndProcedure
  ;}
  ;-* PRIVATE METHODS
  
  ;}
  ;-* PUBLIC METHODS
  
  ;}
  ;-* CONSTRUCTOR DON'T INSTANCIATE
  Procedure super(x,y,*this._struct,*methods)
    With *this
      Protected nbOctets
      If Not setPosX(*this,x):ProcedureReturn #False : EndIf
      If Not setPosY(*this,y):ProcedureReturn #False : EndIf
      CompilerIf #PB_Compiler_Processor= #PB_Processor_x86
        nbOctets = 4
      CompilerElse
        nbOctets = 8
      CompilerEndIf
      ;we take the maximum of methods
      \methods = AllocateMemory(nbOctets * 12)
      ;we take the number of abstract methods
      MoveMemory(*methods,\methods,nbOctets * 4)
      ;we take the result of max methods - number of abstract methods
      MoveMemory(?METHODS,\methods + nbOctets * 4,nbOctets * 8)
      ProcedureReturn *this
    EndWith
  EndProcedure
  ;}
  DataSection
    METHODS:
    Data.i @getPosX()
    Data.i @getPosY()
    Data.i @isSelected()
    Data.i @setPosX()
    Data.i @setPosY()
    Data.i @setSelected()
  EndDataSection
EndModule
Child class sample

Code: Select all

; ****************************************************************************************************
; PROJECT       : TUTO_POO
; PACKAGE       : TUTO_POO.model
; DESIGNED WITH : PB 5.60
; NAME          : Rectangle
; VERSION       : 1
; DATE          :
; AUTHOR        : microdevWeb
; DESCRIPTION   :
; *****************************************************************************************************
DeclareModule Rectangle
  Structure _struct Extends Form::_struct
    width.l
    height.l
  EndStructure
  Declare new(x,y,width,height)
EndDeclareModule

Module Rectangle
  ;-* GETTERS
  Procedure getRectangleWidth (*this._struct)
    With *this
      ProcedureReturn \width
    EndWith
  EndProcedure
  
  Procedure getRectangleHeight (*this._struct)
    With *this
      ProcedureReturn \height
    EndWith
  EndProcedure
  ;}
  ;-* SETTERS
  Procedure setRectangeWidth (*this._struct,width)
    With *this
      \width = width
      ProcedureReturn #True
    EndWith
  EndProcedure
  
  Procedure setRectangleHeight (*this._struct,height)
    With *this
      \height = height
      ProcedureReturn #True
    EndWith
  EndProcedure
  
  ;}
  ;-* PRIVATE METHODS
  
  ;}
  ;-* ABSTRACT METHODS
    Procedure draw (*this._struct)
      With *this
        ; we take red color for differentiating the rectangles
        If \selected
          VectorSourceColor($FF0000FF)
        Else
          VectorSourceColor($FF8CB4D2)
        EndIf
        AddPathBox(\x,\y,\width,\height)
        FillPath()
      EndWith
    EndProcedure
    
    Procedure mouseIsOnMe (*this._struct,x,y)
      With *this
        If (x > = \x And x <= (\x + \width)) And (y > = \y And y <= (\y + \height))
          ProcedureReturn *this
        EndIf
        ProcedureReturn #False
      EndWith
    EndProcedure
    
    Procedure whoIam (*this._struct)
      With *this
        ProcedureReturn Form::#CIRCLE
      EndWith
    EndProcedure
    
  ;}
  ;-* PUBLIC METHODS
  
  ;}
  ;-* DESTRUCTOR is considered as a abstract method
  Procedure free(*this._struct)
    With *this
      ClearStructure(*this,_struct)
    EndWith
  EndProcedure
  ;}
  ;-* CONSTRUCTOR
  Procedure new(x,y,width,height)
    Protected *this._struct = AllocateStructure(_struct)
    With *this
      If Not Form::super(x,y,*this,?METHODS) : ProcedureReturn #False : EndIf
      If Not setRectangeWidth(*this,width) : ProcedureReturn #False : EndIf
      If Not setRectangleHeight(*this,height) : ProcedureReturn #False : EndIf
      ProcedureReturn *this
    EndWith
  EndProcedure
  ;}
  DataSection
    ; It's very important: placed on the  first places the abstract methods
    METHODS:
    ; ABSTRACT METHODS
    Data.i @draw()
    Data.i @mouseIsOnMe()
    Data.i @whoIam()
    Data.i @free()
    ; CLASSICAL METHODS
    Data.i @getRectangleWidth()
    Data.i @getRectangleHeight()
    Data.i @setRectangeWidth()
    Data.i @setRectangleHeight()
  EndDataSection
EndModule

Re: OOP abstract class

Posted: Fri Dec 08, 2017 1:01 pm
by IdeasVacuum
Hi microdevweb

There is no OOP in PB because it has a Procedural paradigm. Fred has made it very clear that OOP will not be incorporated and why. http://www.purebasic.fr/english/viewtop ... 5&start=84

Re: OOP abstract class

Posted: Fri Dec 08, 2017 1:29 pm
by microdevweb
@IdeasVacuum,

yes i know that, for this raison i use other languages. But i like also pb and this is the raison for that i have designed this code. I have told a pseudo "oop", for use the large concepts

Re: OOP abstract class

Posted: Fri Dec 08, 2017 3:49 pm
by mk-soft
C is not OOP, but supports object-oriented programming.
PB is not OOP, but supports object-oriented programming.

However, it is a complex object oriented programming.
But sometimes it is advantageous to choose this way.

In your example, it's a little torn apart.
I find it better to use 1 module for 1 object and define the interface and internal structures in it.

Re: OOP abstract class

Posted: Fri Dec 08, 2017 10:26 pm
by microdevweb
Yes i do not include the interface into the same module, because i want can défined a class member without preocupate me of the order of the module is incluse