Τρίτη 14 Ιουλίου 2020

Observer Pattern (OOP)

A subject has a registry list, where we register Observer type objects. Each Observer has a name, a callback function, plus a pointer to a AnyClick plus more pointers to AnyClick (We can use all of the classes which inherits from AnyClick).

Addition of notify method. Each Observer call notify to Subject (using the reference to Subject).


Class Subject {
Private:
      registry=list
Public:
      module registerObserver (observer as *Observer) {
            \\ this type of pointer is a reference one
            \\ ->(this) is a hard pointer, but ->this is a weak reference
            observer=>subject->this
            append .registry, observer=>name$:=observer
      }
      module notify(name$){
             Print "Incoming Event from ";name$
      }
      module Observe {
            m=each(.registry)
            while m {
                  n=eval(m)
                  n=>dosomething
rem                  Print n=>name$
            }
            Print "Observe"
            wait 20
            if inkey$<>" " then Loop
      }
}
Class AnyClick {
      Function readit {
            Error "Abstract"
      }
}
Class KeyClick As AnyClick {
      num=0
      Function Final readit {
            if keypress(.num) then
                  =True
            end if
      }
      Remove {
            Print "Deleted KeyClick ";.num
      }
class:
      module KeyClick (.num) {
      }
}
Class MouseClick as AnyClick {
      num=0
      Function Final readit {
            if Binary.And(mouse, .num)=.num then
                  =True
            end if
      }
      Remove {
            Print "Deleted MouseClick ";.num
      }
class:
      module MouseClick (.num) {
      }
}
Class Observer {
      subject=Pointer(), name$
      click=list, func$
      remove {
            .click<=list
            Print "deleted ";.name$
      }
      module dosomething {
            m=each(.click)
            While m
                  k=eval(m)
                  if k=>readit() then
                        .subject=>notify .name$
                        Print "Raise from ";.name$
                        call .func$
                        break
                  end if
            End While
      }
class:
      module Observer(.name$, .func$, aClick as *AnyClick) {
            Append .click, len(.click):=aClick
            while not empty
                  Read aClick
                  if not aClick is type AnyClick then Error "not proper type"
                  Append .click, len(.click):=aClick
            End While
      }
}
Function Button1 {
      Print "Button 1"
}
Function Button2 {
      Print "Button 2"
}
Function Middle {
      Print "Middle"
}
CallBack1$=Lazy$(&Button1())
CallBack2$=Lazy$(&Button2())
CallBack3$=Lazy$(&Middle())
M=Subject()
M.registerObserver Pointer(Observer("Button1", CallBack1$, Pointer(KeyClick(Asc("A"))), Pointer(MouseClick(1))))
M.registerObserver Pointer(Observer("Button2", CallBack2$, Pointer(MouseClick(2))))
M.registerObserver Pointer(Observer("Middle", CallBack3$, Pointer(MouseClick(4))))
M.observe


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

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

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