Poo class abstraite
Publié : jeu. 07/déc./2017 20:03
Un des gros avantage de la poo est l'utilisation de classes abstraites. Ce type de classe n'a pas de raison d'être sans héritage mais cela aide grandement l'ajout ou mise à jour de fonctionnalités.

Voici une petit code d'un logiciel de dessin simplifié démontrant l'utilisation d'une classe abstraite en Pb.
Ce code est développé en 3 couches "MVC" model; view; controller
DOWLOAD
voici par exemple le code de la classs abstraite
Et le code d'une classe enfant

Voici une petit code d'un logiciel de dessin simplifié démontrant l'utilisation d'une classe abstraite en Pb.
Ce code est développé en 3 couches "MVC" model; view; controller

voici par exemple le code de la classs abstraite
Code : Tout sélectionner
; ****************************************************************************************************
; 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
Code : Tout sélectionner
; ****************************************************************************************************
; 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