Heureka, I finally did it :)
Thanks to MasterK and his postings on Java and Interfaces on the german boards I remembered something I had some time back in studies that helped me to get implementation hiding into Blitz Max modules
Here is the solution, that only exports TStack from the module.
Modulsource:
Strict
Module datastructure.stack
Private
' Stack Implementation
Type TStack_Element
Field _element:Object
Field _connected:TStack_Element
Function create:TStack_Element( _elem:Object )
' create a TStack_Element
Local temp:TStack_Element = New TStack_Element
If _elem = Null
Throw "No element given"
EndIf
temp._element = _elem
Return temp
End Function
Method Delete()
_element = Null
_connected = Null
End Method
End Type
Type TStack_Imp Extends TStack
' Implementation of stack
Field _top:TStack_Element
Field _count:Int
Function create:TStack_Imp()
Local temp:TStack_Imp = New TStack_Imp
Return temp
End Function
Method push( _elem:Object )
Local temp:TStack_Element
temp = TStack_Element.create( _elem )
temp._connected = _top
_top = temp
_count :+ 1
End Method
Method pop:Object ()
Local ret:Object
If _count > 0
_count :- 1
ret = _top._element
_top = _top._connected
EndIf
Return ret
End Method
Method top:Object ()
Return _top._element
End Method
Method count:Int ()
Return _count
End Method
Method clear()
While count() > 0
pop()
Wend
End Method
End Type
Public
' Module Export
Type TStack
Function create:TStack()
Local temp:TStack = TStack_Imp.create()
Return TStack( temp )
End Function
Method push( _elem:Object ) Abstract
Method pop:Object () Abstract
Method top:Object () Abstract
Method count:Int () Abstract
Method clear() Abstract
End Type
Testsamplesource:
Strict
Local stack:TStack = TStack.create()
Local i:Int
For i = 1 To 2000
stack.push( String("Test "+i) )
Next
Print "elements in stack: " + stack.count()
Print "first element: " + String( stack.pop() )
flushmem
Print "used memory: " + MemAlloced()
stack.clear
FlushMem
Print "used memory: " + MemAlloced()
Print "elements in stack: " + stack.count()
Delay 10000
End
|