It is currently Thu Jun 21, 2018 7:22 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: OOP abstract class
PostPosted: Fri Dec 08, 2017 12:08 pm 
Offline
User
User
User avatar

Joined: Fri Jun 13, 2014 9:38 am
Posts: 97
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:
; ****************************************************************************************************
; 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:
; ****************************************************************************************************
; 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

_________________
Use Pb 5.60 lst and Windows 10

my mother-language isn't english, in advance excuse my mistakes.


Top
 Profile  
Reply with quote  
 Post subject: Re: OOP abstract class
PostPosted: Fri Dec 08, 2017 1:01 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 5774
Location: Wales, UK
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/viewtopic.php?f=3&t=53235&start=84

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: OOP abstract class
PostPosted: Fri Dec 08, 2017 1:29 pm 
Offline
User
User
User avatar

Joined: Fri Jun 13, 2014 9:38 am
Posts: 97
@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

_________________
Use Pb 5.60 lst and Windows 10

my mother-language isn't english, in advance excuse my mistakes.


Top
 Profile  
Reply with quote  
 Post subject: Re: OOP abstract class
PostPosted: Fri Dec 08, 2017 3:49 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1254
Location: Germany
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.

_________________
My Projects OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer /
PB v3.30 / v5.60 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: OOP abstract class
PostPosted: Fri Dec 08, 2017 10:26 pm 
Offline
User
User
User avatar

Joined: Fri Jun 13, 2014 9:38 am
Posts: 97
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

_________________
Use Pb 5.60 lst and Windows 10

my mother-language isn't english, in advance excuse my mistakes.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye