3 manières différentes de développé style objet

Informations pour bien débuter en PureBasic
Avatar de l’utilisateur
microdevweb
Messages : 1800
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

3 manières différentes de développé style objet

Message par microdevweb »

Voici 3 manières de développé style objet, perso je préfère la troisième.

Pure procédural

Code : Tout sélectionner

Prototype p_draw(*s)
Structure _Shape
  x.i     ; position X
  y.i     ; position Y
  color.i
  draw.p_draw
EndStructure
Structure _Box Extends _Shape
  w.i ; width
  h.i ; height
EndStructure
Structure _Circle Extends _Shape
  r.i ; radius
EndStructure
Structure Sheet
  Map  *myShapes._Shape()
EndStructure

Global mySheet.Sheet

Procedure draw_Box(*s._Box)
  With *s
    AddPathBox(\x,\y,\w,\h)
    VectorSourceColor(\color)
    FillPath()
  EndWith
EndProcedure

Procedure draw_Circle(*s._Circle)
  With *s
    AddPathCircle(\x,\y,\r)
    VectorSourceColor(\color)
    FillPath()
  EndWith
EndProcedure


Procedure addBox(id,x,y,w,h,color)
  Protected *this._Box = AllocateStructure(_Box)
  With *this
    \w = w
    \h = h
    \x = x
    \y = y
    \color = color
    \draw = @draw_Box()
  EndWith
  AddMapElement(mySheet\myShapes(),Str(id))
  mySheet\myShapes() = *this  
EndProcedure

Procedure addCircle(id,x,y,r,color)
  Protected *this._Circle = AllocateStructure(_Circle)
  With *this
    \x = x
    \y = y
    \r = r
    \color = color
    \draw = @draw_Circle()
  EndWith
  AddMapElement(mySheet\myShapes(),Str(id))
  mySheet\myShapes() = *this  
EndProcedure

Procedure shapeGetX(id)
  If FindMapElement(mySheet\myShapes(),Str(id))
    ProcedureReturn mySheet\myShapes()\x
  EndIf
EndProcedure

Procedure draw()
  With mySheet
    StartVectorDrawing(CanvasVectorOutput(0))
    ForEach \myShapes()
     \myShapes()\draw(\myShapes())
    Next
    StopVectorDrawing()
  EndWith
EndProcedure

Procedure evExit()
  End
EndProcedure

Procedure OpenMainForm()
  OpenWindow(0,0,0,800,600,"Way 1",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  CanvasGadget(0,0,0,800,600)
  addBox(0,10,10,200,100,$FFBF473F)
  addBox(1,400,10,50,60,$FFBF47C9)
  addCircle(2,100,400,20,$FF2147C9)
  addCircle(3,200,400,20,$FF2193C9)
  draw()
  BindEvent(#PB_Event_CloseWindow,@evExit(),0)
EndProcedure

OpenMainForm()

Debug shapeGetX(3)

Repeat 
  WaitWindowEvent()
ForEver
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
microdevweb
Messages : 1800
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: 3 manières différentes de développé style objet

Message par microdevweb »

Avec des pointeurs mais sans interface

Code : Tout sélectionner

Prototype p_draw(*s)
Structure _Shape
  x.i     ; position X
  y.i     ; position Y
  color.i
  draw.p_draw
EndStructure
Structure _Box Extends _Shape
  w.i ; width
  h.i ; height
EndStructure
Structure _Circle Extends _Shape
  r.i ; radius
EndStructure
Structure Sheet
  List  *myShapes._Shape()
EndStructure

Global mySheet.Sheet
Global *box1,*box2,*circle1,*circle2

Procedure draw_Box(*s._Box)
  With *s
    AddPathBox(\x,\y,\w,\h)
    VectorSourceColor(\color)
    FillPath()
  EndWith
EndProcedure

Procedure draw_Circle(*s._Circle)
  With *s
    AddPathCircle(\x,\y,\r)
    VectorSourceColor(\color)
    FillPath()
  EndWith
EndProcedure


Procedure addBox(x,y,w,h,color)
  Protected *this._Box = AllocateStructure(_Box)
  With *this
    \w = w
    \h = h
    \x = x
    \y = y
    \color = color
    \draw = @draw_Box()
  EndWith
  AddElement(mySheet\myShapes())
  mySheet\myShapes() = *this  
  ProcedureReturn *this
EndProcedure

Procedure addCircle(x,y,r,color)
  Protected *this._Circle = AllocateStructure(_Circle)
  With *this
    \x = x
    \y = y
    \r = r
    \color = color
    \draw = @draw_Circle()
  EndWith
  AddElement(mySheet\myShapes())
  mySheet\myShapes() = *this  
  ProcedureReturn *this
EndProcedure

Procedure shapeGetX(*obj._Shape)
  ProcedureReturn *obj\x
EndProcedure

Procedure draw()
  With mySheet
    StartVectorDrawing(CanvasVectorOutput(0))
    ForEach \myShapes()
     \myShapes()\draw(\myShapes())
    Next
    StopVectorDrawing()
  EndWith
EndProcedure

Procedure evExit()
  End
EndProcedure

Procedure OpenMainForm()
  OpenWindow(0,0,0,800,600,"Way 1",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  CanvasGadget(0,0,0,800,600)
  *box1 = addBox(10,10,200,100,$FFBF473F)
  *box2 = addBox(400,10,50,60,$FFBF47C9)
  *circle1 = addCircle(100,400,20,$FF2147C9)
  *circle2 = addCircle(200,400,20,$FF2193C9)
  draw()
  BindEvent(#PB_Event_CloseWindow,@evExit(),0)
EndProcedure

OpenMainForm()

Debug shapeGetX(*box2)

Repeat 
  WaitWindowEvent()
ForEver
Dernière modification par microdevweb le ven. 15/janv./2021 11:37, modifié 1 fois.
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
microdevweb
Messages : 1800
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: 3 manières différentes de développé style objet

Message par microdevweb »

Avec les interfaces

Code : Tout sélectionner

Prototype p_draw(*s)
Structure _Shape
  *methods
  x.i     ; position X
  y.i     ; position Y
  color.i
  draw.p_draw
EndStructure
Structure _Box Extends _Shape
  w.i ; width
  h.i ; height
EndStructure
Structure _Circle Extends _Shape
  r.i ; radius
EndStructure
Structure Sheet
  List  *myShapes._Shape()
EndStructure

Interface Shape
  getPosX()
EndInterface
Interface Box Extends Shape
  
EndInterface
Interface Circle Extends Shape
  
EndInterface

Global mySheet.Sheet
Global.Box *box1,*box2
Global.Circle *circle1,*circle2

Procedure draw_Box(*s._Box)
  With *s
    AddPathBox(\x,\y,\w,\h)
    VectorSourceColor(\color)
    FillPath()
  EndWith
EndProcedure

Procedure draw_Circle(*s._Circle)
  With *s
    AddPathCircle(\x,\y,\r)
    VectorSourceColor(\color)
    FillPath()
  EndWith
EndProcedure



Procedure addBox(x,y,w,h,color)
  Protected *this._Box = AllocateStructure(_Box)
  With *this
    \w = w
    \h = h
    \x = x
    \y = y
    \color = color
    \draw = @draw_Box()
    \methods = ?S
  EndWith
  AddElement(mySheet\myShapes())
  mySheet\myShapes() = *this  
  ProcedureReturn *this
EndProcedure

Procedure addCircle(x,y,r,color)
  Protected *this._Circle = AllocateStructure(_Circle)
  With *this
    \x = x
    \y = y
    \r = r
    \color = color
    \draw = @draw_Circle()
    \methods = ?S
  EndWith
  AddElement(mySheet\myShapes())
  mySheet\myShapes() = *this  
  ProcedureReturn *this
EndProcedure

Procedure shapeGetX(*obj._Shape)
  ProcedureReturn *obj\x
EndProcedure

Procedure draw()
  With mySheet
    StartVectorDrawing(CanvasVectorOutput(0))
    ForEach \myShapes()
     \myShapes()\draw(\myShapes())
    Next
    StopVectorDrawing()
  EndWith
EndProcedure

Procedure evExit()
  End
EndProcedure

Procedure OpenMainForm()
  OpenWindow(0,0,0,800,600,"Way 1",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  CanvasGadget(0,0,0,800,600)
  *box1 = addBox(10,10,200,100,$FFBF473F)
  *box2 = addBox(400,10,50,60,$FFBF47C9)
  *circle1 = addCircle(100,400,20,$FF2147C9)
  *circle2 = addCircle(200,400,20,$FF2193C9)
  draw()
  BindEvent(#PB_Event_CloseWindow,@evExit(),0)
EndProcedure

OpenMainForm()

Debug *box2\getPosX()
Debug *circle1\getPosX()

Repeat 
  WaitWindowEvent()
ForEver

DataSection
  S:
  Data.i @shapeGetX()
  E:
EndDataSection
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: 3 manières différentes de développé style objet

Message par Kwai chang caine »

Tu m'étonnes que tu préfères la 3e....quand on a le niveau 8O
Moi, mon cerveau a bugué tout de suite au début de la 2 et encore :mrgreen:
En tout cas merci de partager ta connaissance 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Répondre