Δευτέρα 13 Ιουλίου 2020

Pool Pattern (OOP)

This is the Pool Pattern. Reusable class make the Pool object. This object make a limited number of objects. An object from the Pool combined with a user object, and a copy of it placed in a poolback list with key the name of resource. When the object not need any more from process we put back, but not with the user object. So we get the object from poolback list, copy only the members which the resource type object has. Here we want the state to be copy back.

We have an array A() which we place objects (combined from pool), then we do something with them, and then we place it back to pool, keeping only the state of resources. We can use the LastError if we didn't get a resource, or when we pass something which isn't in poolback.
Some times we erase all groups from A() just using Dim A(10)=0 which erase/redim the array passing the 0 for each item.


In ListPool and ListPoolBack we use For This  { } because we want K to be new each time we enter to For object {} structure. K is a resource type so the name$ is final, so if K isn't a new name, the merging can't change the final name$.  We can use pointers and not the For This {}, using K->Eval(M) and K=>ViewState


      Module ListPool {
            m=each(.pool)
            While m
                  k->stackitem(m)
                  k=>ViewState
            End While
            Print
      }
      Module ListPoolBack {
            m=each(.poolBack)
            While m
                  k->Eval(m)
                  k=>ViewState
            End While
            Print
      }


So this is the Pool pattern example:


Class MyData {
Private:      
      X=10, Y=40, Z=500
Public:
      Module View {
            Print format$("X={0}, Y={1}, Z={2}", .X, .Y, .Z)
      }
Class:
      Module Mydata (.X, .Y, .Z) {
      }
}
Class MyData1 {
Private:      
      X=10, Y=40
Public:
      Module View {
            Print format$("X={0}, Y={1}", .X, .Y)
      }
Class:
      Module Mydata1 (.X, .Y) {
      }
}
Class Reusable {
Private:
      Pool=Stack
      PoolBack=List
      MyError=false
Public:
      Group LastError {
            Value {
                  link parent MyError to M
                  =M
            }
      }
      GetOne=Lambda a$="a"  ->{
            .MyError<=False
            Group Other {
            }
            Read other as group
            if len(.pool)=0 then
                  If len(.PoolBack)<6 then
                        Group Resource {
                        Type: Resource
                        Private
                              state=0
                        Public:
                              final name$=a$
                              Module final Copystate (m as Resource) {
                                    .state<=m.state+1
                              }
                              Module final ViewState {
                                    Print format$("({0},{1})",.name$, .state),
                              }
                        }
                        a$=chr$(asc(a$)+1)
                  Else
                        .MyError<=True
                        break
                  end if
            else
                  Stack .pool {Read Resource}
            end if
            Append .PoolBack, Resource.name$:=Resource
            =Resource with other
      }
      Module PutOne (p as Resource){
            .MyError<=False
            if exist(.PoolBack, p.name$) Then
                  p1=eval(.PoolBack)
                  p1.Copystate p
                  Stack .pool {
                        Data p1
                  }
                  Delete .PoolBack, p1.name$
            else
                  .MyError<=True
            end if
      }
      Module ListPool {
            m=each(.pool)
            While m
                  For this {
                        k=stackitem(m)
                        k.ViewState
                  }
            End While
            Print
      }
      Module ListPoolBack {
            m=each(.poolBack)
            While m
                  For this {
                        k=Eval(m)
                        k.ViewState
                  }
            End While
            Print
      }
}
Pool=Reusable()
Group Nothing {
      Type: Nothing
}
Dim A(10)
For i=0 to 9
      A(i)=Pool.GetOne()
      if Pool.LastError then Print "No resources available from ";i : exit For
next i
For i=5 to 0
      Pool.PutOne A(i)
Next I
Dim A(10)=0
Test()
Test()
End
Sub Test()
A(0)=Pool.GetOne(MyData(100,300,500))
A(1)=Pool.GetOne(Nothing)
A(2)=Pool.GetOne(MyData())
A(3)=Pool.GetOne(MyData1(20,20))
Pool.ListPoolBack
For i=0 to 3 {
      For A(i) {
            Print .name$
            If this is type MyData or this is type MyData1 Then
                   .View
             Else.if this is type Nothing Then
                   Print "Nothing in A(";i;")"
             End if
      }
}
For i=0 to 3
      Pool.PutOne A(i)
Next I
Dim A(10)=0
Pool.ListPool
End Sub



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.